-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkratos.py
More file actions
344 lines (294 loc) · 11.2 KB
/
kratos.py
File metadata and controls
344 lines (294 loc) · 11.2 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
from email.mime.text import MIMEText
import smtplib
from typing import Union, Any, List, Dict
import os
import requests
import logging
from datetime import datetime, timedelta
from urllib.parse import quote
from controller.user import manager
logging.basicConfig(level=logging.INFO)
logger: logging.Logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
KRATOS_ADMIN_URL = os.getenv("KRATOS_ADMIN_URL")
SMTP_HOST = os.getenv("SMTP_HOST")
SMTP_PORT = os.getenv("SMTP_PORT")
SMTP_USER = os.getenv("SMTP_USER")
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD")
# user_id -> {"identity" -> full identity, "simple" -> {"id": str, "mail": str, "firstName": str, "lastName": str}}
# "collected" -> timestamp
KRATOS_IDENTITY_CACHE: Dict[str, Any] = {}
KRATOS_IDENTITY_CACHE_TIMEOUT = timedelta(minutes=30)
LANGUAGE_INVITE_WITH_CODE = {
"en": "Hello!\n\nClick the link to complete your account setup:\n\n{invite_link}\n\nYour one-time code: {recovery_code}\n\n",
"de": "Hallo!\n\nKlicken Sie auf den Link, um Ihre Kontoeinrichtung abzuschließen:\n\n{invite_link}\n\nIhr Einmal-Code: {recovery_code}\n\n",
}
LANGUAGE_INVITE_CODE_EXPIRATION = {
"en": "Enter the code on the page. The code is valid for 2 days and can only be used once. Contact your system admin if you have issues.",
"de": "Geben Sie den Code auf der Seite ein. Der Code ist 2 Tage gültig und kann nur einmal verwendet werden. Kontaktieren Sie Ihren Systemadministrator bei Problemen.",
}
INVITATION_SUBJECT = "Sie sind zu unserer app eingeladen/You are invited to our app"
def get_cached_values(update_db_users: bool = True) -> Dict[str, Dict[str, Any]]:
global KRATOS_IDENTITY_CACHE
if not KRATOS_IDENTITY_CACHE or len(KRATOS_IDENTITY_CACHE) == 0:
__refresh_identity_cache(update_db_users)
elif (
KRATOS_IDENTITY_CACHE.get("collected", datetime(1975, 1, 1))
+ KRATOS_IDENTITY_CACHE_TIMEOUT
< datetime.now()
):
__refresh_identity_cache(update_db_users)
return KRATOS_IDENTITY_CACHE
def __refresh_identity_cache(update_db_users: bool = True) -> None:
global KRATOS_IDENTITY_CACHE
request = requests.get(f"{KRATOS_ADMIN_URL}/identities")
if request.ok:
collected = datetime.now()
identities = request.json()
# maybe more pages https://www.ory.sh/docs/ecosystem/api-design#pagination
while next_link := __get_link_from_kratos_request(request):
request = requests.get(next_link)
if request.ok:
identities.extend(request.json())
KRATOS_IDENTITY_CACHE = {
identity["id"]: {
"identity": identity,
"simple": __parse_identity_to_simple(identity),
}
for identity in identities
}
KRATOS_IDENTITY_CACHE["collected"] = collected
else:
KRATOS_IDENTITY_CACHE = {}
if update_db_users:
manager.migrate_kratos_users()
def __get_link_from_kratos_request(request: requests.Response) -> str:
# rel=next only if there is more than 1 page
# </admin/identities?page_size=1&page_token=00000000-0000-0000-0000-000000000000>; rel="first",</admin/identities?page_size=1&page_token=08f30706-9919-4776-9018-6a56c4fa8bb9>; rel="next"
link = request.headers.get("Link")
if link:
if 'rel="next"' in link:
parts = link.split("<")
for part in parts:
if 'rel="next"' in part:
return part.split(">")[0].replace("/admin", KRATOS_ADMIN_URL)
return None
def __get_identity(user_id: str, only_simple: bool = True) -> Dict[str, Any]:
if not isinstance(user_id, str):
user_id = str(user_id)
cache = get_cached_values()
if user_id in cache:
if only_simple:
return cache[user_id]["simple"]
return cache[user_id]
if len(user_id) == 36:
# check not new entry outside cache
request = requests.get(f"{KRATOS_ADMIN_URL}/identities/{user_id}")
if request.ok:
identity = request.json()
if identity["id"] == user_id:
KRATOS_IDENTITY_CACHE[user_id] = {
"identity": identity,
"simple": __parse_identity_to_simple(identity),
}
if only_simple:
return KRATOS_IDENTITY_CACHE[user_id]["simple"]
return KRATOS_IDENTITY_CACHE[user_id]
# e.g. if id "GOLD_STAR" is requested => wont be in cache but expects a dummy dict
if only_simple:
return __parse_identity_to_simple({"id": user_id})
return {
"identity": {
"id": user_id,
"traits": {"email": None, "name": {"first": None, "last": None}},
}
}
def __parse_identity_to_simple(identity: Dict[str, Any]) -> Dict[str, str]:
r = {
"id": identity["id"],
"mail": None,
"firstName": None,
"lastName": None,
"is_admin": get_identity_is_admin(identity),
}
if "traits" in identity:
r["mail"] = identity["traits"]["email"]
if "name" in identity["traits"]:
r["firstName"] = identity["traits"]["name"]["first"]
r["lastName"] = identity["traits"]["name"]["last"]
return r
def get_userid_from_mail(user_mail: str) -> str:
values = get_cached_values()
for key in values:
if key == "collected":
continue
if values[key]["simple"]["mail"] == user_mail:
return key
# not in cached values, try search kratos
result = __search_kratos_for_user_mail(user_mail)
if result is None:
print(
f"get_userid_from_mail: no kratos identity found for mail={user_mail}",
flush=True,
)
return None
return result["id"]
def __search_kratos_for_user_mail(user_mail: str) -> str:
request = requests.get(
f"{KRATOS_ADMIN_URL}/identities?preview_credentials_identifier_similar={quote(user_mail)}"
)
if request.ok:
identities = request.json()
for i in identities:
if i["traits"]["email"].lower() == user_mail.lower():
return i
return None
def resolve_user_mail_by_id(user_id: str) -> str:
i = __get_identity(user_id)
if i:
return i["mail"]
return None
def resolve_user_name_by_id(user_id: str) -> Dict[str, str]:
i = __get_identity(user_id, False)
if i:
i = i["identity"]
return i["traits"]["name"] if "name" in i["traits"] else None
return None
def resolve_all_user_ids(
relevant_ids: List[str], as_list: bool = True
) -> Union[Dict[str, Dict[str, str]], List[Dict[str, str]]]:
final = [] if as_list else {}
for id in relevant_ids:
i = __get_identity(id)
if as_list:
final.append(i)
else:
final[id] = i
return final
def expand_user_mail_name(
users: List[Dict[str, str]], user_id_key="id"
) -> List[Dict[str, str]]:
final = []
for user in users:
i = __get_identity(user[user_id_key])
user = {**user, **i}
final.append(user)
return final
def resolve_user_name_and_email_by_id(user_id: str) -> dict:
i = __get_identity(user_id, False)
if i:
i = i["identity"]
if i and "traits" in i and i["traits"]:
return i["traits"]["name"], i["traits"]["email"]
return None
def create_user_kratos(email: str, provider: str = None):
payload_registration = {
"schema_id": "default",
"traits": {"email": email},
}
if provider:
payload_registration["metadata_public"] = {
"registration_scope": {
"provider_id": provider,
"invitation_sso": True,
}
}
response_create = requests.post(
f"{KRATOS_ADMIN_URL}/identities",
json=payload_registration,
)
return response_create.json() if response_create.ok else None
def delete_user_kratos(user_id: str) -> bool:
response_delete = requests.delete(f"{KRATOS_ADMIN_URL}/identities/{user_id}")
if response_delete.ok:
del KRATOS_IDENTITY_CACHE[user_id]
return True
return False
def get_recovery_code(user_id: str) -> Dict[str, Any]:
payload_recovery_code = {
"expires_in": "48h",
"identity_id": user_id,
}
response = requests.post(
f"{KRATOS_ADMIN_URL}/recovery/code", json=payload_recovery_code
)
if not response.ok:
logger.warning(
"Kratos recovery/code failed: status=%s body=%s",
response.status_code,
response.text[:500],
)
return response.json() if response.ok else None
def send_bulk_invite_emails_with_code(
emails: List[str], invite_data: List[Dict[str, str]]
) -> None:
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
if SMTP_USER and SMTP_PASSWORD:
server.ehlo()
server.starttls()
server.login(SMTP_USER, SMTP_PASSWORD)
for to_email, data in zip(emails, invite_data):
invite_link = data["invite_link"]
recovery_code = data["recovery_code"]
body_de = (
LANGUAGE_INVITE_WITH_CODE["de"].format(
invite_link=invite_link, recovery_code=recovery_code
)
+ "\n"
+ LANGUAGE_INVITE_CODE_EXPIRATION["de"]
)
body_en = (
LANGUAGE_INVITE_WITH_CODE["en"].format(
invite_link=invite_link, recovery_code=recovery_code
)
+ "\n"
+ LANGUAGE_INVITE_CODE_EXPIRATION["en"]
)
msg = MIMEText(f"{body_de}\n\n------\n\n{body_en}")
msg["Subject"] = INVITATION_SUBJECT
msg["From"] = "signup@kern.ai"
msg["To"] = to_email
server.send_message(msg)
def check_user_exists(email: str) -> bool:
request = requests.get(
f"{KRATOS_ADMIN_URL}/identities?preview_credentials_identifier_similar={quote(email)}"
)
if request.ok:
identities = request.json()
for i in identities:
if i["traits"]["email"].lower() == email.lower():
return True
return False
def get_admin_users_by_public_metadata() -> List[Dict[str, Any]]:
admins = []
cache = get_cached_values()
for key in cache:
if key == "collected":
continue
identity = cache[key]["identity"]
if (identity.get("metadata_public") or {}).get("role") == "ADMIN" and identity[
"verifiable_addresses"
][0]["verified"]:
admins.append(cache[key]["simple"])
return admins
def get_identity_is_admin(identity: Dict[str, Any]) -> bool:
try:
if (identity.get("metadata_public") or {}).get("role") == "ADMIN" and identity[
"verifiable_addresses"
][0]["verified"]:
return True
if (
identity["traits"]["email"].split("@")[1] == "kern.ai"
and identity["verifiable_addresses"][0]["verified"]
):
return True
return False
except Exception:
print(
f"get_identity_is_admin: faulty identity record - "
f"id={identity.get('id', 'UNKNOWN')}, "
f"traits={identity.get('traits')}, "
f"verifiable_addresses={identity.get('verifiable_addresses')}",
flush=True,
)
return False