Skip to content

Commit a9d4fa6

Browse files
authored
updating python backend quickstart to use pyjwt instead of jose package (#10518)
1 parent 64d62c5 commit a9d4fa6

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

articles/quickstart/backend/python/01-authorization.md

+17-18
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ useCase: quickstart
2626
```python
2727
# /requirements.txt
2828

29-
flask
29+
flask==2.3.3
3030
python-dotenv
31-
python-jose
31+
pyjwt
3232
flask-cors
3333
six
3434
```
@@ -46,7 +46,7 @@ from functools import wraps
4646

4747
from flask import Flask, request, jsonify, _request_ctx_stack
4848
from flask_cors import cross_origin
49-
from jose import jwt
49+
import jwt
5050

5151
AUTH0_DOMAIN = '${account.namespace}'
5252
API_AUDIENCE = YOUR_API_AUDIENCE
@@ -112,33 +112,32 @@ def requires_auth(f):
112112
jsonurl = urlopen("https://"+AUTH0_DOMAIN+"/.well-known/jwks.json")
113113
jwks = json.loads(jsonurl.read())
114114
unverified_header = jwt.get_unverified_header(token)
115-
rsa_key = {}
115+
public_key = None
116116
for key in jwks["keys"]:
117117
if key["kid"] == unverified_header["kid"]:
118-
rsa_key = {
119-
"kty": key["kty"],
120-
"kid": key["kid"],
121-
"use": key["use"],
122-
"n": key["n"],
123-
"e": key["e"]
124-
}
125-
if rsa_key:
118+
public_key = jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(jwk))
119+
if public_key:
126120
try:
127121
payload = jwt.decode(
128122
token,
129-
rsa_key,
123+
public_key,
130124
algorithms=ALGORITHMS,
131125
audience=API_AUDIENCE,
132126
issuer="https://"+AUTH0_DOMAIN+"/"
133127
)
134128
except jwt.ExpiredSignatureError:
135129
raise AuthError({"code": "token_expired",
136130
"description": "token is expired"}, 401)
137-
except jwt.JWTClaimsError:
138-
raise AuthError({"code": "invalid_claims",
131+
except jwt.InvalidAudienceError:
132+
raise AuthError({"code": "invalid_audience",
139133
"description":
140-
"incorrect claims,"
141-
"please check the audience and issuer"}, 401)
134+
"incorrect audience,"
135+
" please check the audience"}, 401)
136+
except jwt.InvalidIssuerError
137+
raise AuthError({"code": "invalid_issuer",
138+
"description":
139+
"incorrect issuer,"
140+
" please check the issuer"}, 401)
142141
except Exception:
143142
raise AuthError({"code": "invalid_header",
144143
"description":
@@ -165,7 +164,7 @@ def requires_scope(required_scope):
165164
required_scope (str): The scope required to access the resource
166165
"""
167166
token = get_token_auth_header()
168-
unverified_claims = jwt.get_unverified_claims(token)
167+
unverified_claims = jwt.decode(token, options={"verify_signature": False})
169168
if unverified_claims.get("scope"):
170169
token_scopes = unverified_claims["scope"].split()
171170
for token_scope in token_scopes:

0 commit comments

Comments
 (0)