forked from miguelgrinberg/flask-oauth-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.py
141 lines (116 loc) · 4.63 KB
/
oauth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import json
from authlib.client import OAuthClient
from authlib.client.errors import OAuthException
from flask import current_app, url_for, jsonify
class AuthException(Exception):
pass
class AuthClient(object):
providers = None
def __init__(self, provider_name):
self.provider_name = provider_name
credentials = current_app.config['OAUTH_CREDENTIALS'][provider_name]
self.client_key = credentials['key']
self.client_secret = credentials['secret']
def authorization_url(self):
pass
def fetch(self, args):
pass
def _callback_url(self):
# TODO:
return url_for(
'oauth_callback',
provider=self.provider_name,
_external=True
)
@classmethod
def get_provider(self, provider_name):
if self.providers is None:
self.providers = {}
for provider_class in self.__subclasses__():
provider = provider_class()
self.providers[provider.provider_name] = provider
return self.providers.get(provider_name)
class GitHub(AuthClient):
def __init__(self):
super(GitHub, self).__init__('github')
self.client = OAuthClient(
client_key=self.client_key,
client_secret=self.client_secret,
api_base_url='https://api.github.com',
access_token_url='https://github.com/login/oauth/access_token',
authorize_url='https://github.com/login/oauth/authorize',
client_kwargs={'scope': 'user:email'},
)
def authorization_url(self):
url, state = self.client.generate_authorize_redirect(
callback_uri=self._callback_url()
)
return jsonify({'url': url})
def fetch(self, args):
if 'code' not in args:
return None, None
self.client.fetch_access_token(code=args['code'])
user = self.client.get('user').json()
return 'github${}'.format(user['id']), user.get('email')
class Facebook(AuthClient):
def __init__(self):
super(Facebook, self).__init__('facebook')
self.client = OAuthClient(
client_key=self.client_key,
client_secret=self.client_secret,
api_base_url='https://graph.facebook.com/v2.11',
access_token_url='https://graph.facebook.com/v2.11/oauth/access_token',
access_token_params={'method': 'GET'},
authorize_url='https://www.facebook.com/v2.11/dialog/oauth',
client_kwargs={'scope': 'email'},
)
def authorization_url(self):
url, state = self.client.generate_authorize_redirect(
callback_uri=self._callback_url()
)
return jsonify({'url': url})
def fetch(self, args):
if 'code' not in args:
return None, None
try:
self.client.fetch_access_token(
code=args['code'],
callback_uri=self._callback_url()
)
user = self.client.get('me?fields=id,email').json()
except OAuthException as e:
raise AuthException(e.message.get('message'))
return 'facbook${}'.format(user['id']), user.get('email')
#class TwitterSignIn(OAuthSignIn):
# def __init__(self):
# super(TwitterSignIn, self).__init__('twitter')
# self.service = OAuth1Service(
# name='twitter',
# consumer_key=self.consumer_id,
# consumer_secret=self.consumer_secret,
# request_token_url='https://api.twitter.com/oauth/request_token',
# authorize_url='https://api.twitter.com/oauth/authorize',
# access_token_url='https://api.twitter.com/oauth/access_token',
# base_url='https://api.twitter.com/1.1/'
# )
#
# def authorize(self):
# request_token = self.service.get_request_token(
# params={'oauth_callback': self.get_callback_url()}
# )
# session['request_token'] = request_token
# return redirect(self.service.get_authorize_url(request_token[0]))
#
# def callback(self):
# request_token = session.pop('request_token')
# if 'oauth_verifier' not in request.args:
# return None, None, None
# oauth_session = self.service.get_auth_session(
# request_token[0],
# request_token[1],
# data={'oauth_verifier': request.args['oauth_verifier']}
# )
# me = oauth_session.get('account/verify_credentials.json').json()
# social_id = 'twitter$' + str(me.get('id'))
# username = me.get('screen_name')
# return social_id, username, None # Twitter does not provide email