home & auth blueprints

This commit is contained in:
Dmitry Belyaev 2020-07-06 16:35:20 +03:00
parent 11be74c2ad
commit 3c095fe08a
11 changed files with 55 additions and 45 deletions

1
app.py
View File

@ -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()

View File

@ -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

4
website/auth/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from flask import Blueprint
auth = Blueprint('auth', __name__, template_folder='templates')
from . import routes

View File

@ -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):

View File

@ -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)

4
website/home/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from flask import Blueprint
home = Blueprint('home', __name__, template_folder='templates')
from . import routes

25
website/home/routes.py Normal file
View File

@ -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)

View File

@ -12,7 +12,7 @@
{% if user %}
<style>pre{white-space:wrap}</style>
<div>Logged in as <strong>{{user.name}}</strong> (<a href="{{ url_for('.logout') }}">Log Out</a>)</div>
<div>Logged in as <strong>{{user.name}}</strong> (<a href="{{ url_for('auth.logout') }}">Log Out</a>)</div>
<br/><div><h3>Clients:</h3>
{% for client in clients %}
@ -25,7 +25,7 @@
<br/></div>
{% else %}
<br><p>Please, <a href="{{ url_for('.login') }}">Login</a></p>
<br><p>Please, <a href="{{ url_for('auth.login') }}">Login</a></p>
{% endif %}
</body>