-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
frameworks.rst
584 lines (436 loc) · 18.5 KB
/
frameworks.rst
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
.. _frameworks_clients:
Web OAuth Clients
=================
.. module:: authlib.integrations
:noindex:
This documentation covers OAuth 1.0 and OAuth 2.0 integrations for
Python Web Frameworks like:
* Django: The web framework for perfectionists with deadlines
* Flask: The Python micro framework for building web applications
* Starlette: The little ASGI framework that shines
Authlib shares a common API design among these web frameworks. Instead
of introducing them one by one, this documentation contains the common
usage for them all.
We start with creating a registry with the ``OAuth`` class::
# for Flask framework
from authlib.integrations.flask_client import OAuth
# for Django framework
from authlib.integrations.django_client import OAuth
# for Starlette framework
from authlib.integrations.starlette_client import OAuth
oauth = OAuth()
There are little differences among each framework, you can read their
documentation later:
1. :class:`flask_client.OAuth` for :ref:`flask_client`
2. :class:`django_client.OAuth` for :ref:`django_client`
3. :class:`starlette_client.OAuth` for :ref:`starlette_client`
The common use case for OAuth is authentication, e.g. let your users log in
with Twitter, GitHub, Google etc.
Log In with OAuth 1.0
---------------------
For instance, Twitter is an OAuth 1.0 service, you want your users to log in
your website with Twitter.
The first step is register a remote application on the ``OAuth`` registry via
``oauth.register`` method::
oauth.register(
name='twitter',
client_id='{{ your-twitter-consumer-key }}',
client_secret='{{ your-twitter-consumer-secret }}',
request_token_url='https://api.twitter.com/oauth/request_token',
request_token_params=None,
access_token_url='https://api.twitter.com/oauth/access_token',
access_token_params=None,
authorize_url='https://api.twitter.com/oauth/authenticate',
authorize_params=None,
api_base_url='https://api.twitter.com/1.1/',
client_kwargs=None,
)
The first parameter in ``register`` method is the **name** of the remote
application. You can access the remote application with::
twitter = oauth.create_client('twitter')
# or simply with
twitter = oauth.twitter
The configuration of those parameters can be loaded from the framework
configuration. Each framework has its own config system, read the framework
specified documentation later.
For instance, if ``client_id`` and ``client_secret`` can be loaded via
configuration, we can simply register the remote app with::
oauth.register(
name='twitter',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authenticate',
api_base_url='https://api.twitter.com/1.1/',
)
The ``client_kwargs`` is a dict configuration to pass extra parameters to
:ref:`oauth_1_session`. If you are using ``RSA-SHA1`` signature method::
client_kwargs = {
'signature_method': 'RSA-SHA1',
'signature_type': 'HEADER',
'rsa_key': 'Your-RSA-Key'
}
Saving Temporary Credential
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usually, the framework integration has already implemented this part through
the framework session system. All you need to do is enable session for the
chosen framework.
Routes for Authorization
~~~~~~~~~~~~~~~~~~~~~~~~
After configuring the ``OAuth`` registry and the remote application, the
rest steps are much simpler. The only required parts are routes:
1. redirect to 3rd party provider (Twitter) for authentication
2. redirect back to your website to fetch access token and profile
Here is the example for Twitter login::
def login(request):
twitter = oauth.create_client('twitter')
redirect_uri = 'https://example.com/authorize'
return twitter.authorize_redirect(request, redirect_uri)
def authorize(request):
twitter = oauth.create_client('twitter')
token = twitter.authorize_access_token(request)
resp = twitter.get('account/verify_credentials.json')
resp.raise_for_status()
profile = resp.json()
# do something with the token and profile
return '...'
After user confirmed on Twitter authorization page, it will redirect
back to your website ``authorize`` page. In this route, you can get your
user's twitter profile information, you can store the user information
in your database, mark your user as logged in and etc.
Using OAuth 2.0 to Log In
-------------------------
For instance, GitHub is an OAuth 2.0 service, you want your users to log in
your website with GitHub.
The first step is register a remote application on the ``OAuth`` registry via
``oauth.register`` method::
oauth.register(
name='github',
client_id='{{ your-github-client-id }}',
client_secret='{{ your-github-client-secret }}',
access_token_url='https://github.com/login/oauth/access_token',
access_token_params=None,
authorize_url='https://github.com/login/oauth/authorize',
authorize_params=None,
api_base_url='https://api.github.com/',
client_kwargs={'scope': 'user:email'},
)
The first parameter in ``register`` method is the **name** of the remote
application. You can access the remote application with::
github = oauth.create_client('github')
# or simply with
github = oauth.github
The configuration of those parameters can be loaded from the framework
configuration. Each framework has its own config system, read the framework
specified documentation later.
The ``client_kwargs`` is a dict configuration to pass extra parameters to
:ref:`oauth_2_session`, you can pass extra parameters like::
client_kwargs = {
'scope': 'profile',
'token_endpoint_auth_method': 'client_secret_basic',
'token_placement': 'header',
}
There are several ``token_endpoint_auth_method``, get a deep inside the
:ref:`client_auth_methods`.
.. note::
Authlib is using ``request_token_url`` to detect if the client is an
OAuth 1.0 or OAuth 2.0 client. In OAuth 2.0, there is no ``request_token_url``.
Routes for Authorization
~~~~~~~~~~~~~~~~~~~~~~~~
After configuring the ``OAuth`` registry and the remote application, the
rest steps are much simpler. The only required parts are routes:
1. redirect to 3rd party provider (GitHub) for authentication
2. redirect back to your website to fetch access token and profile
Here is the example for GitHub login::
def login(request):
github = oauth.create_client('github')
redirect_uri = 'https://example.com/authorize'
return github.authorize_redirect(request, redirect_uri)
def authorize(request):
token = oauth.github.authorize_access_token(request)
resp = oauth.github.get('user', token=token)
resp.raise_for_status()
profile = resp.json()
# do something with the token and profile
return '...'
After user confirmed on GitHub authorization page, it will redirect
back to your website ``authorize``. In this route, you can get your
user's GitHub profile information, you can store the user information
in your database, mark your user as logged in and etc.
.. note::
You may find that our documentation for OAuth 1.0 and OAuth 2.0 are
the same. They are designed to share the same API, so that you use
the same code for both OAuth 1.0 and OAuth 2.0.
The ONLY difference is the configuration. OAuth 1.0 contains
``request_token_url`` and ``request_token_params`` while OAuth 2.0
not. Also, the ``client_kwargs`` are different.
Client Authentication Methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When fetching access token, the authorization server will require a client
authentication, Authlib provides **three default methods** defined by RFC7591:
- ``client_secret_basic``
- ``client_secret_post``
- ``none``
But if the remote provider does not support these three methods, we need to
register our own authentication methods, like :ref:`oauth2_client_auth`::
from authlib.oauth2.rfc7523 import ClientSecretJWT
oauth.register(
'name',
...
client_auth_methods=[
ClientSecretJWT(token_endpoint), # client_secret_jwt
]
)
.. versionadded:: v0.15
Starting from v0.15, developers can add custom authentication methods
directly to token endpoint::
oauth.register(
'name',
...
token_endpoint_auth_method=ClientSecretJWT(token_endpoint),
)
Accessing OAuth Resources
-------------------------
.. note::
If your application ONLY needs login via 3rd party services like
Twitter, Google, Facebook and GitHub to login, you DON'T need to
create the token database.
There are also chances that you need to access your user's 3rd party
OAuth provider resources. For instance, you want to display the logged
in user's twitter time line and GitHub repositories. You will use
**access token** to fetch the resources::
def get_twitter_tweets(request):
token = OAuth1Token.find(
name='twitter',
user=request.user
)
# API URL: https://api.twitter.com/1.1/statuses/user_timeline.json
resp = oauth.twitter.get('statuses/user_timeline.json', token=token.to_token())
resp.raise_for_status()
return resp.json()
def get_github_repositories(request):
token = OAuth2Token.find(
name='github',
user=request.user
)
# API URL: https://api.github.com/user/repos
resp = oauth.github.get('user/repos', token=token.to_token())
resp.raise_for_status()
return resp.json()
In this case, we need a place to store the access token in order to use
it later. Usually we will save the token into database. In the previous
**Routes for Authorization** ``authorize`` part, we can save the token into
database.
Design Database
~~~~~~~~~~~~~~~
It is possible to share one database table for both OAuth 1.0 token and
OAuth 2.0 token. It is also good to use different database tables for
OAuth 1.0 and OAuth 2.0.
In the above example, we are using two tables. Here are some hints on
how to design the database::
class OAuth1Token(Model):
name = String(length=40)
oauth_token = String(length=200)
oauth_token_secret = String(length=200)
user = ForeignKey(User)
def to_token(self):
return dict(
oauth_token=self.access_token,
oauth_token_secret=self.alt_token,
)
class OAuth2Token(Model):
name = String(length=40)
token_type = String(length=40)
access_token = String(length=200)
refresh_token = String(length=200)
expires_at = PositiveIntegerField()
user = ForeignKey(User)
def to_token(self):
return dict(
access_token=self.access_token,
token_type=self.token_type,
refresh_token=self.refresh_token,
expires_at=self.expires_at,
)
And then we can save user's access token into database when user was redirected
back to our ``authorize`` page.
Fetch User OAuth Token
~~~~~~~~~~~~~~~~~~~~~~
You can always pass a ``token`` parameter to the remote application request
methods, like::
token = OAuth1Token.find(name='twitter', user=request.user)
oauth.twitter.get(url, token=token)
oauth.twitter.post(url, token=token)
oauth.twitter.put(url, token=token)
oauth.twitter.delete(url, token=token)
token = OAuth2Token.find(name='github', user=request.user)
oauth.github.get(url, token=token)
oauth.github.post(url, token=token)
oauth.github.put(url, token=token)
oauth.github.delete(url, token=token)
However, it is not a good practice to query the token database in every request
function. Authlib provides a way to fetch current user's token automatically for
you, just ``register`` with ``fetch_token`` function::
def fetch_twitter_token(request):
token = OAuth1Token.find(
name='twitter',
user=request.user
)
return token.to_token()
def fetch_github_token(request):
token = OAuth2Token.find(
name='github',
user=request.user
)
return token.to_token()
# we can registry this ``fetch_token`` with oauth.register
oauth.register(
'twitter',
# ...
fetch_token=fetch_twitter_token,
)
oauth.register(
'github',
# ...
fetch_token=fetch_github_token,
)
Not good enough. In this way, you have to write ``fetch_token`` for every
remote application. There is also a shared way to fetch token::
def fetch_token(name, request):
if name in OAUTH1_SERVICES:
model = OAuth1Token
else:
model = OAuth2Token
token = model.find(
name=name,
user=request.user
)
return token.to_token()
# initialize OAuth registry with this fetch_token function
oauth = OAuth(fetch_token=fetch_token)
Now, developers don't have to pass a ``token`` in the HTTP requests,
instead, they can pass the ``request``::
def get_twitter_tweets(request):
resp = oauth.twitter.get('statuses/user_timeline.json', request=request)
resp.raise_for_status()
return resp.json()
.. note:: Flask is different, you don't need to pass the ``request`` either.
OAuth 2.0 Enhancement
---------------------
OAuth 1.0 is a protocol, while OAuth 2.0 is a framework. There are so many
features in OAuth 2.0 than OAuth 1.0. This section is designed for
OAuth 2.0 specially.
Auto Update Token
~~~~~~~~~~~~~~~~~
In OAuth 1.0, access token never expires. But in OAuth 2.0, token MAY expire. If
there is a ``refresh_token`` value, Authlib will auto update the access token if
it is expired.
We do this by passing a ``update_token`` function to ``OAuth`` registry::
def update_token(name, token, refresh_token=None, access_token=None):
if refresh_token:
item = OAuth2Token.find(name=name, refresh_token=refresh_token)
elif access_token:
item = OAuth2Token.find(name=name, access_token=access_token)
else:
return
# update old token
item.access_token = token['access_token']
item.refresh_token = token.get('refresh_token')
item.expires_at = token['expires_at']
item.save()
oauth = OAuth(update_token=update_token)
In this way, OAuth 2.0 integration will update expired token automatically. There is
also a **signal** way to update token. Checkout the frameworks documentation.
OAuth 2.0 Code Challenge
~~~~~~~~~~~~~~~~~~~~~~~~
Adding ``code_challenge`` provided by :ref:`specs/rfc7636` is simple. You
register your remote app with a ``code_challenge_method`` in ``client_kwargs``::
oauth.register(
'example',
client_id='Example Client ID',
client_secret='Example Client Secret',
access_token_url='https://example.com/oauth/access_token',
authorize_url='https://example.com/oauth/authorize',
api_base_url='https://api.example.com/',
client_kwargs={'code_challenge_method': 'S256'},
)
Note, the only supported ``code_challenge_method`` is ``S256``.
Compliance Fix for OAuth 2.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For non standard OAuth 2.0 service, you can pass a ``compliance_fix`` when
``.register``. For example, Slack has a compliance problem, we can construct
a method to fix the requests session::
def slack_compliance_fix(session):
def _fix(resp):
resp.raise_for_status()
token = resp.json()
# slack returns no token_type
token['token_type'] = 'Bearer'
resp._content = to_unicode(json.dumps(token)).encode('utf-8')
return resp
session.register_compliance_hook('access_token_response', _fix)
Then pass this ``slack_compliance_fix`` into ``.register`` parameters::
oauth.register(
'slack',
client_id='...',
client_secret='...',
...,
compliance_fix=slack_compliance_fix,
...
)
Find all the available compliance hooks at :ref:`compliance_fix_oauth2`.
OpenID Connect & UserInfo
-------------------------
When logging in with OpenID Connect, "access_token" is not what developers
want. Instead, what developers want is **user info**, Authlib wrap it with
:class:`~authlib.oidc.core.UserInfo`.
There are two ways to fetch **userinfo** from 3rd party providers. If the
provider supports OpenID Connect, we can get the user info from the returned
``id_token``.
userinfo_endpoint
~~~~~~~~~~~~~~~~~
Passing a ``userinfo_endpoint`` when ``.register`` remote client::
oauth.register(
'google',
client_id='...',
client_secret='...',
userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo',
)
And later, when the client has obtained the access token, we can call::
def authorize(request):
token = oauth.google.authorize_access_token(request)
user = oauth.google.userinfo(token=token)
return '...'
Parsing ``id_token``
~~~~~~~~~~~~~~~~~~~~
For OpenID Connect provider, when ``.authorize_access_token``, the provider
will include a ``id_token`` in the response. This ``id_token`` contains the
``UserInfo`` we need so that we don't have to fetch userinfo endpoint again.
The ``id_token`` is a JWT, with Authlib :ref:`jwt_guide`, we can decode it
easily. Frameworks integrations will handle it automatically if configurations
are correct.
A simple solution is to provide the OpenID Connect Discovery Endpoint::
oauth.register(
'google',
client_id='...',
client_secret='...',
server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
client_kwargs={'scope': 'openid email profile'},
)
The discovery endpoint provides all the information we need so that we don't
have to add ``authorize_url`` and ``access_token_url``.
Check out our client example: https://github.com/authlib/demo-oauth-client
But if there is no discovery endpoint, developers MUST add all the missing information
themselves::
* authorize_url
* access_token_url
* jwks_uri
This ``jwks_uri`` is the URL to get provider's public JWKs. Developers MAY also
provide the value of ``jwks`` instead of ``jwks_uri``::
oauth.register(
'google',
client_id='...',
client_secret='...',
access_token_url='https://example.com/oauth/access_token',
authorize_url='https://example.com/oauth/authorize',
jwks={"keys": [...]}
)