Skip to content

Commit 9e6f581

Browse files
committed
Add PKCE sample in docs
Changed default behavior of having client_id i params when secret does not exist
1 parent fcba340 commit 9e6f581

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ v1.4.0 (TBD)
1212
- Add support for Python 3.8-3.12
1313
- Remove support of Python 2.x, <3.7
1414
- Migrated to Github Action
15+
- Add PKCE support
1516

1617

1718
v1.3.1 (21 January 2022)

docs/examples/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Examples
1010
github
1111
google
1212
linkedin
13+
native_spa_pkce_auth0
1314
outlook
1415
spotify
1516
tumblr
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
client_id = 'your_client_id'
3+
4+
authorization_base_url = "https://dev-foobar.eu.auth0.com/authorize"
5+
token_url = "https://dev-foobar.eu.auth0.com/oauth/token"
6+
scope = ["openid"]
7+
8+
from requests_oauthlib import OAuth2Session
9+
redirect_uri = 'http://localhost:8080/callback'
10+
11+
session = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri, pkce="S256")
12+
authorization_url, state = session.authorization_url(authorization_base_url,access_type="offline")
13+
14+
print("Please go here and authorize:")
15+
print(authorization_url)
16+
17+
redirect_response = input('Paste the full redirect URL here: ')
18+
19+
token = session.fetch_token(token_url, authorization_response=redirect_response)
20+
print(token)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Native or SPA Tutorial with PKCE in Auth0
2+
=========================================
3+
4+
Setup a new web project in the Auth0 Dashboard, (application type: Native application or Single Page Web Application)_
5+
6+
Note this sample is valid for any Identity Providers supporting OAuth2.0 Authorization Code with PKCE.
7+
8+
When you have obtained a ``client_id``, and registered
9+
a callback URL then you can try out the command line interactive example below.
10+
11+
.. literalinclude:: native_spa_pkce_auth0.py
12+
:language: python

requests_oauthlib/oauth2_session.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ def fetch_token(
330330

331331
# otherwise we may need to create an auth header
332332
else:
333+
# In case client_secret is none, it means we are in public clients
334+
# so OAuth2 AS should read client_id into params instead of Basic Auth.
335+
if client_secret is None:
336+
include_client_id = True
337+
333338
# since we don't have an auth header, we MAY need to create one
334339
# it is possible that we want to send the `client_id` in the body
335340
# if so, `include_client_id` should be set to True

0 commit comments

Comments
 (0)