Move Blender ID to extensible OAuth

Also, added support for Google OAuth.
This commit is contained in:
2017-07-27 23:31:26 +02:00
parent e0520e265d
commit 23b856b073
3 changed files with 116 additions and 47 deletions

View File

@@ -1,7 +1,7 @@
import json
from rauth import OAuth2Service
from flask import current_app, url_for, request, redirect, session
from flask import current_app, url_for, request, redirect
class OAuthSignIn(object):
@@ -33,6 +33,53 @@ class OAuthSignIn(object):
return cls.providers[provider_name]
class BlenderIdSignIn(OAuthSignIn):
def __init__(self):
super(BlenderIdSignIn, self).__init__('blender-id')
base_url = current_app.config['OAUTH_CREDENTIALS']['blender-id'].get(
'base_url', 'https://www.blender.org/id/')
self.service = OAuth2Service(
name='blender-id',
client_id=self.consumer_id,
client_secret=self.consumer_secret,
authorize_url='%soauth/authorize' % base_url,
access_token_url='%soauth/token' % base_url,
base_url='%sapi/' % base_url
)
def authorize(self):
return redirect(self.service.get_authorize_url(
scope='email',
response_type='code',
redirect_uri=self.get_callback_url())
)
def callback(self):
def decode_json(payload):
return json.loads(payload.decode('utf-8'))
if 'code' not in request.args:
return None, None, None
oauth_session = self.service.get_auth_session(
data={'code': request.args['code'],
'grant_type': 'authorization_code',
'redirect_uri': self.get_callback_url()},
decoder=decode_json
)
# TODO handle exception for failed oauth or not authorized
me = oauth_session.get('user').json()
# TODO handle case when user chooses not to disclose en email
return (
me['id'],
me.get('email'),
oauth_session.access_token
)
class FacebookSignIn(OAuthSignIn):
def __init__(self):
super(FacebookSignIn, self).__init__('facebook')
@@ -69,4 +116,45 @@ class FacebookSignIn(OAuthSignIn):
return (
me['id'],
me.get('email'),
None
)
class GoogleSignIn(OAuthSignIn):
def __init__(self):
super(GoogleSignIn, self).__init__('google')
self.service = OAuth2Service(
name='google',
client_id=self.consumer_id,
client_secret=self.consumer_secret,
authorize_url='https://accounts.google.com/o/oauth2/auth',
access_token_url='https://accounts.google.com/o/oauth2/token',
base_url='https://www.googleapis.com/oauth2/v1/'
)
def authorize(self):
return redirect(self.service.get_authorize_url(
scope='https://www.googleapis.com/auth/userinfo.email',
response_type='code',
redirect_uri=self.get_callback_url())
)
def callback(self):
def decode_json(payload):
return json.loads(payload.decode('utf-8'))
if 'code' not in request.args:
return None, None, None
oauth_session = self.service.get_auth_session(
data={'code': request.args['code'],
'grant_type': 'authorization_code',
'redirect_uri': self.get_callback_url()},
decoder=decode_json
)
me = oauth_session.get('userinfo').json()
# TODO handle case when user chooses not to disclose en email
return (
me['id'],
me.get('email'),
None
)