2019-12-02 03:32:03 +00:00
|
|
|
from flask import request, session, redirect, url_for, current_app
|
|
|
|
|
|
|
|
from .base import authlib_oauth_client
|
|
|
|
from ..models.setting import Setting
|
|
|
|
|
|
|
|
|
|
|
|
def google_oauth():
|
|
|
|
if not Setting().get('google_oauth_enabled'):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def fetch_google_token():
|
|
|
|
return session.get('google_token')
|
|
|
|
|
|
|
|
def update_token(token):
|
|
|
|
session['google_token'] = token
|
|
|
|
return token
|
|
|
|
|
2023-04-02 13:19:05 +00:00
|
|
|
authlib_params = {
|
|
|
|
'client_id': Setting().get('google_oauth_client_id'),
|
|
|
|
'client_secret': Setting().get('google_oauth_client_secret'),
|
|
|
|
'api_base_url': Setting().get('google_base_url'),
|
|
|
|
'request_token_url': None,
|
|
|
|
'access_token_url': Setting().get('google_token_url'),
|
|
|
|
'authorize_url': Setting().get('google_authorize_url'),
|
|
|
|
'client_kwargs': {'scope': Setting().get('google_oauth_scope')},
|
|
|
|
'fetch_token': fetch_google_token,
|
|
|
|
'update_token': update_token
|
|
|
|
}
|
|
|
|
|
|
|
|
server_metadata_url = Setting().get('google_oauth_metadata_url')
|
|
|
|
|
|
|
|
if isinstance(server_metadata_url, str) and len(server_metadata_url.strip()) > 0:
|
|
|
|
authlib_params['server_metadata_url'] = server_metadata_url
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
google = authlib_oauth_client.register(
|
|
|
|
'google',
|
2023-04-02 13:19:05 +00:00
|
|
|
**authlib_params
|
|
|
|
)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
@current_app.route('/google/authorized')
|
|
|
|
def google_authorized():
|
|
|
|
session['google_oauthredir'] = url_for(
|
|
|
|
'.google_authorized', _external=True)
|
|
|
|
token = google.authorize_access_token()
|
|
|
|
if token is None:
|
|
|
|
return 'Access denied: reason=%s error=%s' % (
|
|
|
|
request.args['error_reason'],
|
|
|
|
request.args['error_description']
|
|
|
|
)
|
|
|
|
session['google_token'] = (token)
|
|
|
|
return redirect(url_for('index.login'))
|
|
|
|
|
|
|
|
return google
|
|
|
|
|