-
Notifications
You must be signed in to change notification settings - Fork 0
/
sso_google_auth.py
323 lines (273 loc) · 10.6 KB
/
sso_google_auth.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
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
import os
import json
import urllib.request
import urllib.parse
import re
import jwt
import traceback
import requests
import hashlib
from jwt import PyJWKClient
def random_sha256() -> str:
return hashlib.sha256(os.urandom(1024)).hexdigest()
class GoogleAuth:
discovery_document_url = os.getenv(
"GOOGLE_DISCOVERY_DOCUMENT_URL",
"https://accounts.google.com/.well-known/openid-configuration",
)
dev_mode = False
_client_id = None
_client_secret = None
def set_creds(self, client_id: str = None, client_secret: str = None):
if not client_id and not client_secret:
self._client_id = os.getenv("GOOGLE_CLIENT_ID")
self._client_secret = os.getenv("GOOGLE_CLIENT_SECRET")
else:
self._client_id = client_id
self._client_secret = client_secret
if not self._client_id or not self._client_secret:
raise Exception("Arguments client_id or client_secret not set")
def __init__(
self,
client_id: str = None,
client_secret: str = None,
use_override_env_var: bool = True,
dev_mode: bool = False,
):
self.set_creds(client_id, client_secret)
self.reset(use_override_env_var, dev_mode)
def reset(self, use_override_env_var: bool = True, dev_mode: bool = False):
self._google_oidc_config = {}
self._google_fetch_is_error = False
self._google_fetch_error_msg = None
self.issuer = None
self.auth_endpoint = None
self.token_endpoint = None
self.jwks_uri = None
self.scopes = ["openid", "email", "profile"]
self._init_config(oev=use_override_env_var)
def get_oidc_config(self) -> dict:
if not self._google_fetch_is_error and not self._google_oidc_config:
try:
with urllib.request.urlopen(
self.discovery_document_url, timeout=3
) as url:
if url:
data = json.load(url)
if (
data
and "issuer" in data
and self.discovery_document_url.startswith(data["issuer"])
):
self._google_oidc_config = data
except Exception as e:
self._google_fetch_error_msg = str(e) + traceback.format_exc()
self._google_fetch_is_error = True
return self._google_oidc_config
def _init_config(self, oev=True):
if oev:
self.issuer = os.getenv("GOOGLE_ISSUER")
self.auth_endpoint = os.getenv("GOOGLE_AUTH_ENDPOINT")
self.token_endpoint = os.getenv("GOOGLE_TOKEN_ENDPOINT")
self.jwks_uri = os.getenv("GOOGLE_JWKS_URI")
if not self.issuer:
self.issuer = self.get_oidc_config().get("issuer")
if not self.auth_endpoint:
self.auth_endpoint = self.get_oidc_config().get("authorization_endpoint")
if not self.token_endpoint:
self.token_endpoint = self.get_oidc_config().get("token_endpoint")
if not self.jwks_uri:
self.jwks_uri = self.get_oidc_config().get("jwks_uri")
def is_ready(self) -> bool:
starts = f"http{'s://' if not self.dev_mode else ''}"
return 3 == [
starts
for x in [self.auth_endpoint, self.token_endpoint, self.jwks_uri]
if x and type(x) == str and x.startswith(starts)
].count(starts)
def get_error(self) -> tuple:
if self.is_ready():
return (False, None)
if self._google_fetch_is_error:
res = (True, self._google_fetch_error_msg)
else:
res = (True, "Incomplete or invalid parameters")
return res
def step_one_get_redirect_url(
self,
callback_url: str = None,
login_hint: str = None,
hd_domain_hint: str = None,
include_nonce: bool = True,
override_nonce: str = None,
override_state: str = None,
override_prompt: str = None,
override_response_type: str = None,
) -> str:
if not self.is_ready():
return {"error": True, "error_message": "Not configured"}
if not callback_url:
return {"error": True, "error_message": "Argument callback_url not set"}
elif "%" not in callback_url or "&" in callback_url:
callback_url = urllib.parse.quote(callback_url)
response_type = override_response_type if override_response_type else "code"
scope_string = urllib.parse.quote(" ".join(self.scopes))
state = override_state if override_state else random_sha256()
nonce = (
override_nonce
if override_nonce
else (random_sha256() if include_nonce else None)
)
prompt = override_prompt if override_prompt else "none"
url = f"""{self.auth_endpoint}?
response_type={response_type}&
client_id={self._client_id}&
scope={scope_string}&
redirect_uri={callback_url}&
state={state}&
{f'nonce={nonce}&' if nonce else ''}
{f'login_hint={login_hint}&' if login_hint else ''}
{f'prompt={prompt}&' if prompt else ''}
{f'hd={hd_domain_hint}&' if hd_domain_hint else ''}"""
url = re.sub(r"\s", "", url).strip("&")
return {
"error": False,
"error_message": None,
"url": url,
"state": state,
"nonce": nonce,
}
def step_two_get_id_token_from_google_url(
self,
url: str = None,
state_to_compare: str = None,
redirect_uri: str = None,
) -> dict:
if not url:
return {"error": True, "error_message": "Argument 'url' not set or empty"}
parsed_url = None
try:
parsed_url = urllib.parse.urlparse(url)
except Exception as e:
return {"error": True, "error_message": str(e) + traceback.format_exc()}
if not parsed_url:
return {"error": True, "error_message": "URL parsing issue"}
params = {}
querystrings = {}
fragments = {}
if parsed_url.query:
querystrings = urllib.parse.parse_qs(parsed_url.query)
if querystrings:
params.update(
{
qx: ",".join(querystrings[qx])
if type(querystrings[qx]) == list
else str(querystrings[qx])
for qx in querystrings
}
)
if parsed_url.fragment:
fragments = urllib.parse.parse_qs(parsed_url.fragment)
if fragments:
params.update(
{
fx: ",".join(fragments[fx])
if type(fragments[fx]) == list
else str(fragments[fx])
for fx in fragments
}
)
if params.get("error") or params.get("error_subtype"):
em = [
"Google error response",
"querystring" if querystrings else "fragment",
params.get("error_subtype"),
params.get("error"),
]
return {"error": True, "error_message": ": ".join(filter(None, em))}
returned_state = params.get("state")
if state_to_compare or returned_state:
if state_to_compare != returned_state:
return {"error": True, "error_message": "state mismatch"}
returned_hd = params.get("hd")
returned_id_token = params.get("id_token")
returned_code = params.get("code")
validated_id_token = None
if not returned_code and not returned_id_token:
return {
"error": True,
"error_message": "missing code or id_token in Google's response",
}
elif returned_code and not returned_id_token:
err, s3res = self.get_id_token_from_auth_code(returned_code, redirect_uri)
if err:
return {"error": True, "error_message": s3res}
else:
returned_id_token = s3res.get("id_token")
if returned_id_token:
err, s4res = self.verify_and_decode_id_token(returned_id_token)
if err:
return {
"error": True,
"error_message": s4res,
}
else:
validated_id_token = s4res
if validated_id_token:
return {
"error": False,
"error_message": None,
"id_token": validated_id_token,
}
return {"error": True, "error_message": None}
def get_id_token_from_auth_code(
self,
code: str = None,
redirect_uri: str = None,
):
# return tuple containing if error and result
# (True == error | False == no error, data)
http_resp = None
data = {}
try:
head = {"Content-Type": "application/x-www-form-urlencoded"}
body = {
"code": code,
"client_id": self._client_id,
"client_secret": self._client_secret,
"grant_type": "authorization_code",
}
if redirect_uri:
body["redirect_uri"] = redirect_uri
http_resp = requests.post(self.token_endpoint, body, head)
data = (
http_resp.json()
if http_resp.text and http_resp.text.startswith("{")
else {}
)
if data and "error" in data:
raise Exception(
f"Google token_endpoint ({self.token_endpoint}) response: {data}"
)
except Exception as e:
return (True, e)
if data:
return (False, data)
return (True, None)
def verify_and_decode_id_token(self, id_token: str = None) -> tuple:
# return tuple containing if error and result
# (True == error | False == no error, data)
data = {}
try:
jwks_client = PyJWKClient(self.jwks_uri)
signing_key = jwks_client.get_signing_key_from_jwt(id_token)
data = jwt.decode(
id_token,
signing_key.key,
algorithms=["RS256"],
audience=self._client_id,
options={"verify_exp": True},
)
except Exception as e:
return (True, str(e) + traceback.format_exc())
return (data.get("iss") is None, data)