diff --git a/app/__init__.py b/app/__init__.py index 326654c..805bf37 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,7 +3,7 @@ from flask import Flask, request, session, redirect, url_for from flask_login import LoginManager from flask_sqlalchemy import SQLAlchemy as SA from flask_migrate import Migrate - +from flask_oauthlib.client import OAuth # subclass SQLAlchemy to enable pool_pre_ping class SQLAlchemy(SA): @@ -26,89 +26,13 @@ logging = logger('powerdns-admin', app.config['LOG_LEVEL'], app.config['LOG_FILE login_manager = LoginManager() login_manager.init_app(app) -db = SQLAlchemy(app) -migrate = Migrate(app, db) # used for flask-migrate - -def enable_github_oauth(GITHUB_ENABLE): - if not GITHUB_ENABLE: - return None, None - from flask_oauthlib.client import OAuth - oauth = OAuth(app) - github = oauth.remote_app( - 'github', - consumer_key=app.config['GITHUB_OAUTH_KEY'], - consumer_secret=app.config['GITHUB_OAUTH_SECRET'], - request_token_params={'scope': app.config['GITHUB_OAUTH_SCOPE']}, - base_url=app.config['GITHUB_OAUTH_URL'], # API URL - request_token_url=None, - access_token_method='POST', - access_token_url=app.config['GITHUB_OAUTH_TOKEN'], - authorize_url=app.config['GITHUB_OAUTH_AUTHORIZE'] - ) - - @app.route('/user/authorized') - def authorized(): - session['github_oauthredir'] = url_for('.authorized', _external=True) - resp = github.authorized_response() - if resp is None: - return 'Access denied: reason=%s error=%s' % ( - request.args['error'], - request.args['error_description'] - ) - session['github_token'] = (resp['access_token'], '') - return redirect(url_for('.login')) - - @github.tokengetter - def get_github_oauth_token(): - return session.get('github_token') - - return oauth, github - - -oauth, github = enable_github_oauth(app.config.get('GITHUB_OAUTH_ENABLE')) - - -def enable_google_oauth(GOOGLE_ENABLE): - if not GOOGLE_ENABLE: - return None - from flask_oauthlib.client import OAuth - oauth = OAuth(app) - - google = oauth.remote_app( - 'google', - consumer_key=app.config['GOOGLE_OAUTH_CLIENT_ID'], - consumer_secret=app.config['GOOGLE_OAUTH_CLIENT_SECRET'], - request_token_params=app.config['GOOGLE_TOKEN_PARAMS'], - base_url=app.config['GOOGLE_BASE_URL'], - request_token_url=None, - access_token_method='POST', - access_token_url=app.config['GOOGLE_TOKEN_URL'], - authorize_url=app.config['GOOGLE_AUTHORIZE_URL'], - ) - - @app.route('/user/authorized') - def authorized(): - resp = google.authorized_response() - if resp is None: - return 'Access denied: reason=%s error=%s' % ( - request.args['error_reason'], - request.args['error_description'] - ) - session['google_token'] = (resp['access_token'], '') - return redirect(url_for('.login')) - - @google.tokengetter - def get_google_oauth_token(): - return session.get('google_token') - - return google - - -google = enable_google_oauth(app.config.get('GOOGLE_OAUTH_ENABLE')) - -from app import views, models +db = SQLAlchemy(app) # database +migrate = Migrate(app, db) # flask-migrate +oauth = OAuth(app) # oauth if app.config.get('SAML_ENABLED') and app.config.get('SAML_ENCRYPT'): from app.lib import certutil if not certutil.check_certificate(): certutil.create_self_signed_cert() + +from app import models, views diff --git a/app/models.py b/app/models.py index 41a4a13..9b3ab9b 100644 --- a/app/models.py +++ b/app/models.py @@ -1805,7 +1805,6 @@ class Setting(db.Model): 'google_oauth_enabled': False, 'google_oauth_client_id':'', 'google_oauth_client_secret':'', - 'google_redirect_uri': '/user/authorized', 'google_token_url': 'https://accounts.google.com/o/oauth2/token', 'google_token_params': {'scope': 'email profile'}, 'google_authorize_url':'https://accounts.google.com/o/oauth2/auth', diff --git a/app/oauth.py b/app/oauth.py new file mode 100644 index 0000000..9c01b8b --- /dev/null +++ b/app/oauth.py @@ -0,0 +1,79 @@ +from ast import literal_eval +from flask import request, session, redirect, url_for +from flask_oauthlib.client import OAuth + +from app import app, oauth +from app.models import Setting + +# TODO: +# - Replace Flask-OAuthlib by authlib +# - Fix flask-migrate issue with calling Setting() class during downgrade / upgrade +# - Fix github/google enabling (Currently need to reload the flask app) + +def github_oauth(): + if not Setting().get('github_oauth_enabled'): + return None + + github = oauth.remote_app( + 'github', + consumer_key = Setting().get('github_oauth_key'), + consumer_secret = Setting().get('github_oauth_secret'), + request_token_params = {'scope': Setting().get('github_oauth_scope')}, + base_url = Setting().get('github_oauth_api_url'), + request_token_url = None, + access_token_method = 'POST', + access_token_url = Setting().get('github_oauth_token_url'), + authorize_url = Setting().get('github_oauth_authorize_url') + ) + + @app.route('/github/authorized') + def github_authorized(): + session['github_oauthredir'] = url_for('.github_authorized', _external=True) + resp = github.authorized_response() + if resp is None: + return 'Access denied: reason=%s error=%s' % ( + request.args['error'], + request.args['error_description'] + ) + session['github_token'] = (resp['access_token'], '') + return redirect(url_for('.login')) + + @github.tokengetter + def get_github_oauth_token(): + return session.get('github_token') + + return github + + +def google_oauth(): + if not Setting().get('google_oauth_enabled'): + return None + + google = oauth.remote_app( + 'google', + consumer_key=Setting().get('google_oauth_client_id'), + consumer_secret=Setting().get('google_oauth_client_secret'), + request_token_params=literal_eval(Setting().get('google_token_params')), + base_url=Setting().get('google_base_url'), + request_token_url=None, + access_token_method='POST', + access_token_url=Setting().get('google_token_url'), + authorize_url=Setting().get('google_authorize_url'), + ) + + @app.route('/google/authorized') + def google_authorized(): + resp = google.authorized_response() + if resp is None: + return 'Access denied: reason=%s error=%s' % ( + request.args['error_reason'], + request.args['error_description'] + ) + session['google_token'] = (resp['access_token'], '') + return redirect(url_for('.login')) + + @google.tokengetter + def get_google_oauth_token(): + return session.get('google_token') + + return google diff --git a/app/templates/admin_setting_authentication.html b/app/templates/admin_setting_authentication.html index 32e9b05..34dbe81 100644 --- a/app/templates/admin_setting_authentication.html +++ b/app/templates/admin_setting_authentication.html @@ -159,10 +159,6 @@