diff --git a/app.py b/app.py index 7e09043..acc2073 100644 --- a/app.py +++ b/app.py @@ -57,7 +57,6 @@ def test_data(): print('test url: http://127.0.0.1:5000/oauth/authorize?scope=profile&response_type=code&client_id={}'.format(client_id)) - if __name__ == '__main__': manager.run() diff --git a/website/__init__.py b/website/__init__.py index 3401d06..0b4061c 100644 --- a/website/__init__.py +++ b/website/__init__.py @@ -1,15 +1,12 @@ from flask import Flask from flask_migrate import Migrate from flask_login import LoginManager -from flask_sqlalchemy import SQLAlchemy -from .routes import bp from .models import db, User -from .oauth2 import config_oauth -import config +from .auth.oauth2 import config_oauth migrate = Migrate() login_manager = LoginManager() -login_manager.login_view = 'home.login' +login_manager.login_view = 'auth.login' def create_app(cfg): @@ -24,7 +21,10 @@ def setup_app(app): config_oauth(app) migrate.init_app(app, db) login_manager.init_app(app) - app.register_blueprint(bp, url_prefix='') + from .auth import auth + from .home import home + app.register_blueprint(auth, url_prefix='') + app.register_blueprint(home, url_prefix='') @login_manager.user_loader diff --git a/website/auth/__init__.py b/website/auth/__init__.py new file mode 100644 index 0000000..46199ad --- /dev/null +++ b/website/auth/__init__.py @@ -0,0 +1,4 @@ +from flask import Blueprint + +auth = Blueprint('auth', __name__, template_folder='templates') +from . import routes diff --git a/website/forms.py b/website/auth/forms.py similarity index 100% rename from website/forms.py rename to website/auth/forms.py diff --git a/website/oauth2.py b/website/auth/oauth2.py similarity index 97% rename from website/oauth2.py rename to website/auth/oauth2.py index b8bdca8..31f765d 100644 --- a/website/oauth2.py +++ b/website/auth/oauth2.py @@ -11,8 +11,8 @@ from authlib.integrations.sqla_oauth2 import ( from authlib.oauth2.rfc6749 import grants from authlib.oauth2.rfc7636 import CodeChallenge from werkzeug.security import gen_salt -from .models import db, User -from .models import OAuth2Client, OAuth2AuthorizationCode, OAuth2Token +from ..models import db, User +from ..models import OAuth2Client, OAuth2AuthorizationCode, OAuth2Token class AuthorizationCodeGrant(grants.AuthorizationCodeGrant): diff --git a/website/routes.py b/website/auth/routes.py similarity index 60% rename from website/routes.py rename to website/auth/routes.py index fd5c87b..6b8b9a3 100644 --- a/website/routes.py +++ b/website/auth/routes.py @@ -1,62 +1,48 @@ -from flask import Blueprint, Flask, request, render_template, redirect, url_for, flash, jsonify, make_response, session +from . import auth +from flask import request, render_template, redirect, url_for, flash, jsonify from flask_login import login_required, login_user, current_user, logout_user from authlib.integrations.flask_oauth2 import current_token from authlib.oauth2 import OAuth2Error -from .models import User, OAuth2Client, db -from .forms import LoginForm, ConfirmAccessForm +from ..models import User, db from .oauth2 import authorization, require_oauth -from werkzeug.exceptions import abort - -bp = Blueprint('home', __name__) +from .forms import LoginForm, ConfirmAccessForm -@bp.route('/') -def home(): - user = None - if current_user.is_authenticated: - clients = current_user.clients - user = current_user.name - else: - clients = [] - - return render_template('home.html', user=user, clients=clients) - - -@bp.route('/login/', methods=['post', 'get']) +@auth.route('/login/', methods=['post', 'get']) def login(): if current_user.is_authenticated: - return redirect(url_for('.home')) + return redirect(url_for('home.index')) form = LoginForm() if form.validate_on_submit(): user = db.session.query(User).filter(User.username == form.username.data).first() if user and user.check_password(form.password.data): login_user(user, remember=form.remember.data) - nextpage = request.args.get('next', url_for('.home')) + nextpage = request.args.get('next', url_for('home.index')) return redirect(nextpage) else: flash("Invalid username/password", 'error') return render_template('login.html', form=form) -@bp.route('/logout/') +@auth.route('/logout/') @login_required def logout(): logout_user() flash("You have been logged out.") - return redirect(url_for('.home')) + return redirect(url_for('home.index')) -@bp.route('/oauth/token', methods=['POST']) +@auth.route('/oauth/token', methods=['POST']) def issue_token(): return authorization.create_token_response() -@bp.route('/oauth/revoke', methods=['POST']) +@auth.route('/oauth/revoke', methods=['POST']) def revoke_token(): return authorization.create_endpoint_response('revocation') -@bp.route('/oauth/authorize', methods=['GET', 'POST']) +@auth.route('/oauth/authorize', methods=['GET', 'POST']) @login_required def authorize(): user = current_user @@ -75,11 +61,3 @@ def authorize(): grant_user = user return authorization.create_authorization_response(grant_user=grant_user) - - -@bp.route('/api/me') -@require_oauth('profile') -def api_me(): - user = current_token.user - return jsonify(id=user.id, username=user.username) - diff --git a/website/templates/authorize.html b/website/auth/templates/authorize.html similarity index 100% rename from website/templates/authorize.html rename to website/auth/templates/authorize.html diff --git a/website/templates/login.html b/website/auth/templates/login.html similarity index 100% rename from website/templates/login.html rename to website/auth/templates/login.html diff --git a/website/home/__init__.py b/website/home/__init__.py new file mode 100644 index 0000000..53310f5 --- /dev/null +++ b/website/home/__init__.py @@ -0,0 +1,4 @@ +from flask import Blueprint + +home = Blueprint('home', __name__, template_folder='templates') +from . import routes diff --git a/website/home/routes.py b/website/home/routes.py new file mode 100644 index 0000000..17c1f73 --- /dev/null +++ b/website/home/routes.py @@ -0,0 +1,25 @@ +from . import home +from flask import Blueprint, render_template, jsonify +from flask_login import current_user +from authlib.integrations.flask_oauth2 import current_token +from ..auth.oauth2 import require_oauth + + +@home.route('/') +def index(): + user = None + if current_user.is_authenticated: + clients = current_user.clients + user = current_user.name + else: + clients = [] + + return render_template('home.html', user=user, clients=clients) + + +@home.route('/api/me') +@require_oauth('profile') +def api_me(): + user = current_token.user + return jsonify(id=user.id, username=user.username) + diff --git a/website/templates/home.html b/website/home/templates/home.html similarity index 83% rename from website/templates/home.html rename to website/home/templates/home.html index 04ac14d..07f8a25 100644 --- a/website/templates/home.html +++ b/website/home/templates/home.html @@ -12,7 +12,7 @@ {% if user %} -
Please, Login
+Please, Login
{% endif %}