-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathauthentication.py
More file actions
347 lines (301 loc) · 12.1 KB
/
authentication.py
File metadata and controls
347 lines (301 loc) · 12.1 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from __future__ import annotations
import base64
import os
import threading
import time
import webbrowser
from abc import ABC, abstractmethod
from enum import Enum
from functools import cached_property
from http import HTTPStatus
from pathlib import Path
from typing import Any, cast
import httpx
import jwt
import requests
from pydantic import BaseModel, TypeAdapter, computed_field
from requests.auth import AuthBase
from blueapi.config import OIDCConfig, TiledConfig
from blueapi.service.model import Cache
DEFAULT_CACHE_DIR = "~/.cache/"
SCOPES = "openid"
class CacheManager(ABC):
@abstractmethod
def can_access_cache(self) -> bool: ...
@abstractmethod
def save_cache(self, cache: Cache) -> None: ...
@abstractmethod
def load_cache(self) -> Cache: ...
@abstractmethod
def delete_cache(self) -> None: ...
class SessionCacheManager(CacheManager):
def __init__(self, token_path: Path | None) -> None:
self._token_path: Path = (
token_path if token_path else self._default_token_cache_path()
)
@cached_property
def _file_path(self) -> str:
return os.path.expanduser(self._token_path)
def save_cache(self, cache: Cache) -> None:
self.delete_cache()
with open(self._file_path, "xb") as token_file:
token_file.write(base64.b64encode(cache.model_dump_json().encode("utf-8")))
os.chmod(self._file_path, 0o600)
def load_cache(self) -> Cache:
with open(self._file_path, "rb") as cache_file:
return TypeAdapter(Cache).validate_json(
base64.b64decode(cache_file.read()).decode("utf-8")
)
def delete_cache(self) -> None:
Path(self._file_path).unlink(missing_ok=True)
@staticmethod
def _default_token_cache_path() -> Path:
"""
Return the default cache file path.
"""
cache_path = os.environ.get("XDG_CACHE_HOME", DEFAULT_CACHE_DIR)
return Path(cache_path).expanduser() / "blueapi_cache"
def can_access_cache(self) -> bool:
assert self._token_path
try:
self._token_path.write_text("")
except IsADirectoryError:
print("Invalid path: a directory path was provided instead of a file path")
return False
except PermissionError:
print(f"Permission denied: Cannot write to {self._token_path.absolute()}")
return False
return True
class SessionManager:
def __init__(self, server_config: OIDCConfig, cache_manager: CacheManager) -> None:
self._server_config = server_config
self._cache_manager: CacheManager = cache_manager
@classmethod
def from_cache(cls, auth_token_path: Path | None) -> SessionManager:
cache_manager = SessionCacheManager(auth_token_path)
cache = cache_manager.load_cache()
return SessionManager(
server_config=cache.oidc_config, cache_manager=cache_manager
)
def delete_cache(self) -> None:
self._cache_manager.delete_cache()
def get_valid_access_token(self) -> str:
"""
Retrieves a valid access token.
Returns:
str: A valid access token if successful.
"": If the operation fails (no valid token could be fetched or refreshed)
"""
try:
cache = self._cache_manager.load_cache()
self.decode_jwt(cache.access_token)
return cache.access_token
except jwt.ExpiredSignatureError:
cache = self._cache_manager.load_cache()
return self._refresh_auth_token(cache.refresh_token)
except Exception:
self.delete_cache()
return ""
@cached_property
def client(self):
return jwt.PyJWKClient(self._server_config.jwks_uri)
def decode_jwt(self, json_web_token: str):
signing_key = self.client.get_signing_key_from_jwt(json_web_token)
return jwt.decode(
json_web_token,
signing_key.key,
algorithms=self._server_config.id_token_signing_alg_values_supported,
verify=True,
audience=self._server_config.client_audience,
issuer=self._server_config.issuer,
)
def logout(self) -> None:
cache = self._cache_manager.load_cache()
self.delete_cache()
try:
response = requests.get(
self._server_config.end_session_endpoint,
params={
"id_token_hint": cache.id_token,
"client_id": self._server_config.client_id,
},
)
response.raise_for_status()
print("Logged out")
except Exception as e:
print(
"An unexpected error occurred while attempting "
f"to log out from the server.{e}"
)
def _refresh_auth_token(self, refresh_token: str) -> str:
response = requests.post(
self._server_config.token_endpoint,
data={
"client_id": self._server_config.client_id,
"grant_type": "refresh_token",
"refresh_token": refresh_token,
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
if response.status_code == HTTPStatus.OK:
token = response.json()
self._cache_manager.save_cache(
Cache(
oidc_config=self._server_config,
refresh_token=token["refresh_token"],
id_token=token["id_token"],
access_token=token["access_token"],
),
)
return token["access_token"]
else:
self.delete_cache()
return ""
def poll_for_token(
self, device_code: str, polling_interval: float, expires_in: float
) -> dict[str, Any]:
expiry_time: float = time.time() + expires_in
while time.time() < expiry_time:
response = requests.post(
self._server_config.token_endpoint,
data={
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": device_code,
"client_id": self._server_config.client_id,
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
if response.status_code == HTTPStatus.OK:
return response.json()
time.sleep(polling_interval)
raise TimeoutError("Polling timed out")
def start_device_flow(self):
assert self._cache_manager.can_access_cache()
print("Logging in")
response: requests.Response = requests.post(
self._server_config.device_authorization_endpoint,
data={
"client_id": self._server_config.client_id,
"scope": SCOPES,
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
response.raise_for_status()
response_json: dict[str, Any] = response.json()
device_code = cast(str, response_json.get("device_code"))
interval = cast(float, response_json.get("interval"))
expires_in = cast(float, response_json.get("expires_in"))
webbrowser.open_new_tab(response_json["verification_uri_complete"])
print(
f"Please login from this URL:- {response_json['verification_uri_complete']}"
)
auth_token_json: dict[str, Any] = self.poll_for_token(
device_code, interval, expires_in
)
self._cache_manager.save_cache(
Cache(
oidc_config=self._server_config,
refresh_token=auth_token_json["refresh_token"],
id_token=auth_token_json["id_token"],
access_token=auth_token_json["access_token"],
)
)
print("Logged in and cached new token")
class JWTAuth(AuthBase):
def __init__(self, session_manager: SessionManager | None):
self.token: str = (
session_manager.get_valid_access_token() if session_manager else ""
)
def __call__(self, request):
if self.token:
request.headers["Authorization"] = f"Bearer {self.token}"
return request
class TokenType(str, Enum):
refresh_token = "refresh_token"
access_token = "access_token"
class Token(BaseModel):
token: str
expires_at: float | None
@computed_field
@property
def expired(self) -> bool:
if self.expires_at is None:
# Assume token is valid
return False
return time.time() > self.expires_at
def _get_token_expires_at(
self, token_dict: dict[str, Any], token_type: TokenType
) -> int | None:
expires_at = None
if token_type == TokenType.access_token:
if "expires_in" in token_dict:
expires_at = int(time.time()) + int(token_dict["expires_in"])
elif token_type == TokenType.refresh_token:
if "refresh_expires_in" in token_dict:
expires_at = int(time.time()) + int(token_dict["refresh_expires_in"])
return expires_at
def __init__(self, token_dict: dict[str, Any], token_type: TokenType):
token = token_dict.get(token_type)
if token is None:
raise ValueError(f"Not able to find {token_type} in response")
super().__init__(
token=token, expires_at=self._get_token_expires_at(token_dict, token_type)
)
def __str__(self) -> str:
return str(self.token)
class TiledAuth(httpx.Auth):
def __init__(self, tiled_config: TiledConfig, blueapi_jwt_token: str):
self._tiled_config = tiled_config
self._blueapi_jwt_token = blueapi_jwt_token
self._sync_lock = threading.RLock()
self._access_token: Token | None = None
self._refresh_token: Token | None = None
def exchange_access_token(self):
client_secret = self._tiled_config.token_exchange_secret.get_secret_value()
request_data = {
"client_id": self._tiled_config.requester_client_id,
"client_secret": client_secret,
"subject_token": self._blueapi_jwt_token,
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token",
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"requested_token_type": "urn:ietf:params:oauth:token-type:refresh_token",
}
with self._sync_lock:
response = requests.post(
self._tiled_config.token_url,
data=request_data,
)
response.raise_for_status()
self.sync_tokens(response.json())
def refresh_token(self):
if self._refresh_token is None:
raise Exception("Cannot refresh session as no refresh token available")
with self._sync_lock:
client_secret = self._tiled_config.token_exchange_secret.get_secret_value()
response = requests.post(
self._tiled_config.token_url,
data={
"client_id": self._tiled_config.requester_client_id,
"client_secret": client_secret,
"grant_type": "refresh_token",
"refresh_token": str(self._refresh_token),
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
response.raise_for_status()
self.sync_tokens(response.json())
def sync_tokens(self, response):
self._access_token = Token(response, TokenType.access_token)
self._refresh_token = Token(response, TokenType.refresh_token)
def sync_auth_flow(self, request):
if self._access_token is not None and self._access_token.expired is not True:
request.headers["Authorization"] = f"Bearer {self._access_token}"
yield request
elif self._access_token is None:
self.exchange_access_token()
request.headers["Authorization"] = f"Bearer {self._access_token}"
yield request
else:
self.refresh_token()
request.headers["Authorization"] = f"Bearer {self._access_token}"
yield request