realms-wiki/realms/modules/auth/oauth/views.py

31 lines
890 B
Python
Raw Normal View History

2015-10-15 01:36:22 +03:00
from flask import Blueprint, url_for, request, flash, redirect, session
2015-10-15 07:08:56 +03:00
from .models import User
2015-10-14 06:52:30 +03:00
blueprint = Blueprint('auth.oauth', __name__)
def oauth_failed(next_url):
2015-10-15 01:36:22 +03:00
flash('You denied the request to sign in.')
2015-10-14 06:52:30 +03:00
return redirect(next_url)
2015-10-15 07:08:56 +03:00
@blueprint.route("/login/oauth/<provider>")
def oauth_login(provider):
return User.get_app(provider).authorize(callback=url_for('oauth_callback', provider=provider,
2015-10-15 01:36:22 +03:00
next=request.args.get('next') or request.referrer or None))
2015-10-14 06:52:30 +03:00
2015-10-15 07:08:56 +03:00
@blueprint.route('/login/oauth/<provider>/callback')
def oauth_callback(provider):
2015-10-14 06:52:30 +03:00
next_url = request.args.get('next') or url_for('index')
2015-10-15 07:08:56 +03:00
resp = User.get_app(provider).authorized_response()
2015-10-14 06:52:30 +03:00
if resp is None:
return oauth_failed(next_url)
2015-10-15 07:08:56 +03:00
session[provider + '_token'] = (
2015-10-14 06:52:30 +03:00
resp['oauth_token'],
resp['oauth_token_secret']
)
2015-10-15 01:36:22 +03:00
return redirect(next_url)