2018-08-19 08:29:50 +00:00
|
|
|
from flask import request, session, redirect, url_for
|
|
|
|
|
2018-10-22 01:33:46 +00:00
|
|
|
from app import app, authlib_oauth_client
|
2018-08-19 08:29:50 +00:00
|
|
|
from app.models import Setting
|
|
|
|
|
|
|
|
# TODO:
|
|
|
|
# - Fix github/google enabling (Currently need to reload the flask app)
|
|
|
|
|
|
|
|
def github_oauth():
|
|
|
|
if not Setting().get('github_oauth_enabled'):
|
|
|
|
return None
|
|
|
|
|
2018-10-22 01:33:46 +00:00
|
|
|
def fetch_github_token():
|
|
|
|
return session.get('github_token')
|
|
|
|
|
|
|
|
github = authlib_oauth_client.register(
|
2018-08-19 08:29:50 +00:00
|
|
|
'github',
|
2018-10-22 01:33:46 +00:00
|
|
|
client_id = Setting().get('github_oauth_key'),
|
|
|
|
client_secret = Setting().get('github_oauth_secret'),
|
2018-08-19 08:29:50 +00:00
|
|
|
request_token_params = {'scope': Setting().get('github_oauth_scope')},
|
2018-10-22 01:33:46 +00:00
|
|
|
api_base_url = Setting().get('github_oauth_api_url'),
|
2018-08-19 08:29:50 +00:00
|
|
|
request_token_url = None,
|
|
|
|
access_token_url = Setting().get('github_oauth_token_url'),
|
2018-10-22 01:33:46 +00:00
|
|
|
authorize_url = Setting().get('github_oauth_authorize_url'),
|
|
|
|
client_kwargs={'scope': Setting().get('github_oauth_scope')},
|
|
|
|
fetch_token=fetch_github_token,
|
2018-08-19 08:29:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@app.route('/github/authorized')
|
|
|
|
def github_authorized():
|
|
|
|
session['github_oauthredir'] = url_for('.github_authorized', _external=True)
|
2018-10-22 01:33:46 +00:00
|
|
|
token = github.authorize_access_token()
|
|
|
|
if token is None:
|
2018-08-19 08:29:50 +00:00
|
|
|
return 'Access denied: reason=%s error=%s' % (
|
|
|
|
request.args['error'],
|
|
|
|
request.args['error_description']
|
|
|
|
)
|
2018-10-22 01:33:46 +00:00
|
|
|
session['github_token'] = (token)
|
2018-08-19 08:29:50 +00:00
|
|
|
return redirect(url_for('.login'))
|
|
|
|
|
|
|
|
return github
|
|
|
|
|
|
|
|
|
|
|
|
def google_oauth():
|
|
|
|
if not Setting().get('google_oauth_enabled'):
|
|
|
|
return None
|
|
|
|
|
2018-10-22 01:33:46 +00:00
|
|
|
def fetch_google_token():
|
|
|
|
return session.get('google_token')
|
|
|
|
|
|
|
|
google = authlib_oauth_client.register(
|
2018-08-19 08:29:50 +00:00
|
|
|
'google',
|
2018-10-22 01:33:46 +00:00
|
|
|
client_id=Setting().get('google_oauth_client_id'),
|
|
|
|
client_secret=Setting().get('google_oauth_client_secret'),
|
|
|
|
api_base_url=Setting().get('google_base_url'),
|
2018-08-19 08:29:50 +00:00
|
|
|
request_token_url=None,
|
|
|
|
access_token_url=Setting().get('google_token_url'),
|
|
|
|
authorize_url=Setting().get('google_authorize_url'),
|
2018-10-22 01:33:46 +00:00
|
|
|
client_kwargs={'scope': Setting().get('google_oauth_scope')},
|
|
|
|
fetch_token=fetch_google_token,
|
2018-08-19 08:29:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@app.route('/google/authorized')
|
|
|
|
def google_authorized():
|
2018-10-22 01:33:46 +00:00
|
|
|
session['google_oauthredir'] = url_for('.google_authorized', _external=True)
|
|
|
|
token = google.authorize_access_token()
|
|
|
|
if token is None:
|
2018-08-19 08:29:50 +00:00
|
|
|
return 'Access denied: reason=%s error=%s' % (
|
|
|
|
request.args['error_reason'],
|
|
|
|
request.args['error_description']
|
|
|
|
)
|
2018-10-22 01:33:46 +00:00
|
|
|
session['google_token'] = (token)
|
2018-08-19 08:29:50 +00:00
|
|
|
return redirect(url_for('.login'))
|
|
|
|
|
|
|
|
return google
|
2018-10-21 22:38:12 +00:00
|
|
|
|
|
|
|
def oidc_oauth():
|
|
|
|
if not Setting().get('oidc_oauth_enabled'):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def fetch_oidc_token():
|
|
|
|
return session.get('oidc_token')
|
|
|
|
|
|
|
|
oidc = authlib_oauth_client.register(
|
|
|
|
'oidc',
|
|
|
|
client_id = Setting().get('oidc_oauth_key'),
|
|
|
|
client_secret = Setting().get('oidc_oauth_secret'),
|
|
|
|
api_base_url = Setting().get('oidc_oauth_api_url'),
|
|
|
|
request_token_url = None,
|
|
|
|
access_token_url = Setting().get('oidc_oauth_token_url'),
|
|
|
|
authorize_url = Setting().get('oidc_oauth_authorize_url'),
|
|
|
|
client_kwargs={'scope': Setting().get('oidc_oauth_scope')},
|
|
|
|
fetch_token=fetch_oidc_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
@app.route('/oidc/authorized')
|
|
|
|
def oidc_authorized():
|
|
|
|
session['oidc_oauthredir'] = url_for('.oidc_authorized', _external=True)
|
|
|
|
token = oidc.authorize_access_token()
|
|
|
|
if token is None:
|
|
|
|
return 'Access denied: reason=%s error=%s' % (
|
|
|
|
request.args['error'],
|
|
|
|
request.args['error_description']
|
|
|
|
)
|
|
|
|
session['oidc_token'] = (token)
|
|
|
|
return redirect(url_for('.login'))
|
|
|
|
|
|
|
|
return oidc
|