@@ -26,9 +26,9 @@ useCase: quickstart
26
26
``` python
27
27
# /requirements.txt
28
28
29
- flask
29
+ flask== 2.3 .3
30
30
python- dotenv
31
- python - jose
31
+ pyjwt
32
32
flask- cors
33
33
six
34
34
```
@@ -46,7 +46,7 @@ from functools import wraps
46
46
47
47
from flask import Flask, request, jsonify, _request_ctx_stack
48
48
from flask_cors import cross_origin
49
- from jose import jwt
49
+ import jwt
50
50
51
51
AUTH0_DOMAIN = ' ${account.namespace} '
52
52
API_AUDIENCE = YOUR_API_AUDIENCE
@@ -112,33 +112,32 @@ def requires_auth(f):
112
112
jsonurl = urlopen(" https://" + AUTH0_DOMAIN + " /.well-known/jwks.json" )
113
113
jwks = json.loads(jsonurl.read())
114
114
unverified_header = jwt.get_unverified_header(token)
115
- rsa_key = {}
115
+ public_key = None
116
116
for key in jwks[" keys" ]:
117
117
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:
126
120
try :
127
121
payload = jwt.decode(
128
122
token,
129
- rsa_key ,
123
+ public_key ,
130
124
algorithms = ALGORITHMS ,
131
125
audience = API_AUDIENCE ,
132
126
issuer = " https://" + AUTH0_DOMAIN + " /"
133
127
)
134
128
except jwt.ExpiredSignatureError:
135
129
raise AuthError({" code" : " token_expired" ,
136
130
" 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 " ,
139
133
" 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 )
142
141
except Exception :
143
142
raise AuthError({" code" : " invalid_header" ,
144
143
" description" :
@@ -165,7 +164,7 @@ def requires_scope(required_scope):
165
164
required_scope (str): The scope required to access the resource
166
165
"""
167
166
token = get_token_auth_header()
168
- unverified_claims = jwt.get_unverified_claims (token)
167
+ unverified_claims = jwt.decode (token, options = { " verify_signature " : False } )
169
168
if unverified_claims.get(" scope" ):
170
169
token_scopes = unverified_claims[" scope" ].split()
171
170
for token_scope in token_scopes:
0 commit comments