-
Notifications
You must be signed in to change notification settings - Fork 1
/
authenticate.py
236 lines (207 loc) · 8.39 KB
/
authenticate.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
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
import constants
import json
from authlib.client import OAuth2Session
from authlib.flask.client import OAuth
from dotenv import load_dotenv, find_dotenv
from functools import wraps
from flask import session
from flask import redirect
from flask import render_template
from jose import JWTError, jwt
from os import environ as env
import six
from six.moves.urllib.request import urlopen
from werkzeug.exceptions import Unauthorized, Forbidden
# from errors import *
# Format error response and append status code.
class AuthError(Exception):
def __init__(self, error, status_code):
self.error = error
self.status_code = status_code
class Auth:
ENV_FILE = find_dotenv()
if ENV_FILE:
load_dotenv(ENV_FILE)
AUTH0_CALLBACK_URL = env.get(constants.AUTH0_CALLBACK_URL)
AUTH0_CLIENT_ID = env.get(constants.AUTH0_CLIENT_ID)
AUTH0_CLIENT_SECRET = env.get(constants.AUTH0_CLIENT_SECRET)
AUTH0_DOMAIN = env.get(constants.AUTH0_DOMAIN)
AUTH0_BASE_URL = "https://" + AUTH0_DOMAIN
AUTH0_AUDIENCE = env.get(constants.AUTH0_AUDIENCE)
SECRET_KEY = env.get(constants.SECRET_KEY)
MYSQL_USERNAME = env.get(constants.MYSQL_USERNAME)
MYSQL_PASSWORD = env.get(constants.MYSQL_PASSWORD)
MYSQL_IP = env.get(constants.MYSQL_IP)
MYSQL_DB = env.get(constants.MYSQL_DB)
SCOPE = (
"openid profile "
) # All scopes authorized for the user are auto-added (without explicit request) by the rule
# See SPA+API: Auth0 Configuration for more details
# https://auth0.com/docs/architecture-scenarios/spa-api/part-2
ISSUER = "https://" + AUTH0_DOMAIN + "/"
# Needs API setup https://auth0.com/docs/quickstart/backend/python#validate-access-tokens
def __init__(self, app):
self.app = app
self.auth0 = OAuth(app).register(
"auth0",
client_id=Auth.AUTH0_CLIENT_ID,
client_secret=Auth.AUTH0_CLIENT_SECRET,
api_base_url=Auth.AUTH0_BASE_URL,
access_token_url=Auth.AUTH0_BASE_URL + "/oauth/token",
authorize_url=Auth.AUTH0_BASE_URL + "/authorize",
client_kwargs={"scope": Auth.SCOPE},
)
def get_access_level(self):
scopes = self.__get_scopes()
access_level = [x.split("access:")[1] for x in scopes if x.find("access:") >= 0]
return [
"stable"
] + access_level # all authenticated users have "stable" access level
def __get_scopes(self):
token_scopes = []
token = Auth.__authenticate_token()
unverified_claims = jwt.get_unverified_claims(token["access_token"])
if unverified_claims.get("scope"):
token_scopes = unverified_claims["scope"].split()
return token_scopes
@staticmethod
def __fetch_mgmnt_api_token():
__session = OAuth2Session(
Auth.AUTH0_CLIENT_ID, Auth.AUTH0_CLIENT_SECRET
) # need a new session to get the token for Mgmnt API
token = __session.fetch_access_token(
Auth.AUTH0_BASE_URL + "/oauth/token",
grant_type="client_credentials",
audience=Auth.AUTH0_BASE_URL + "/api/v2/",
)
return token["access_token"] # ok to use it without validation
@staticmethod
def __decode_token(token):
jsonurl = urlopen("https://" + Auth.AUTH0_DOMAIN + "/.well-known/jwks.json")
jwks = json.loads(jsonurl.read())
try:
unverified_header = jwt.get_unverified_header(token)
except Exception as e:
raise (e)
if unverified_header["alg"] == "HS256":
raise Exception(
{
"code": "invalid_header",
"description": "Invalid header. "
"Use an RS256 signed JWT Access Token",
},
401,
)
rsa_key = {}
for key in jwks["keys"]:
if key["kid"] == unverified_header["kid"]:
rsa_key = {
"kty": key["kty"],
"kid": key["kid"],
"use": key["use"],
"n": key["n"],
"e": key["e"],
}
if rsa_key:
try:
# this verifies everything.
payload = jwt.decode(
token,
rsa_key,
algorithms=[constants.JWT_ALGORITHM],
issuer=Auth.ISSUER,
audience=Auth.AUTH0_AUDIENCE,
options=constants.JWT_VERIFY_DEFAULTS,
)
except jwt.ExpiredSignatureError:
raise Exception(
{"code": "token_expired", "description": "token is expired"}, 401
)
except jwt.JWTClaimsError:
raise Exception(
{
"code": "invalid_claims",
"description": "incorrect claims,"
" please check the audience and issuer",
},
401,
)
except JWTError as e:
six.raise_from(Unauthorized, e)
except Exception as e:
raise Exception(
{
"code": "invalid_header",
"description": "Unable to parse authentication"
" token or unable to decode.",
},
401,
)
else:
raise Exception(
{
"code": "invalid_header",
"description": "Unable to find appropriate key",
},
401,
)
return payload
@staticmethod
def requires_scope(required_scope):
"""Determines if the required scope is present in the access token
Args:
required_scope (str): The scope required to access the resource
"""
def require_scope(f):
@wraps(f)
def decorated(*args, **kwargs):
token = Auth.__authenticate_token()
if token is None:
return redirect("/login") # if not even authenticated,
unverified_claims = jwt.get_unverified_claims(token["access_token"])
if unverified_claims.get("scope"):
token_scopes = unverified_claims["scope"].split()
for token_scope in token_scopes:
if token_scope == required_scope:
return f(*args, **kwargs)
# raise Exception({"code": "Unauthorized", "description": "You don't have access to this resource"},403)
# render_template("404.html", error = str(e))
six.raise_from(Forbidden, Exception(403))
return decorated
return require_scope
@staticmethod
def __authenticate_token():
if constants.JWT_PAYLOAD not in session:
return None
token = session[constants.TOKEN_KEY]
Auth.__decode_token(token["access_token"])
return token
@staticmethod
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
token = Auth.__authenticate_token()
if token is None:
return redirect("/login")
# _request_ctx_stack.top.current_user = token_decoded #not sure about this one - seems unnecessary.
return f(*args, **kwargs)
return decorated
@staticmethod
def requires_admin(f):
@wraps(f)
def decorated(*args, **kwargs):
token = session[constants.TOKEN_KEY]["access_token"]
unverified_claims = jwt.get_unverified_claims(token)
if unverified_claims.get("scope"):
token_scopes = unverified_claims["scope"].split()
for token_scope in token_scopes:
if token_scope == "access:admin":
mgmnt_token = session.get(constants.MGMNT_API_TOKEN, None)
if mgmnt_token is None:
session[
constants.MGMNT_API_TOKEN
] = Auth.__fetch_mgmnt_api_token()
return f(*args, **kwargs)
# raise AuthError({"code": "Forbidden", "description": "You don't have access to this resource"},403)
six.raise_from(Forbidden, Exception(403))
return decorated