pycrossauth 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- crossauth_backend/__init__.py +59 -0
- crossauth_backend/auth.py +132 -0
- crossauth_backend/common/__init__.py +2 -0
- crossauth_backend/common/error.py +498 -0
- crossauth_backend/common/interfaces.py +287 -0
- crossauth_backend/common/jwt.py +41 -0
- crossauth_backend/common/logger.py +202 -0
- crossauth_backend/cookieauth.py +575 -0
- crossauth_backend/crypto.py +457 -0
- crossauth_backend/oauth/__init__.py +2 -0
- crossauth_backend/oauth/client.py +1063 -0
- crossauth_backend/oauth/clientmanager.py +157 -0
- crossauth_backend/oauth/resserver.py +34 -0
- crossauth_backend/oauth/tokenconsumer.py +412 -0
- crossauth_backend/oauth/wellknown.py +108 -0
- crossauth_backend/session.py +450 -0
- crossauth_backend/storage.py +626 -0
- crossauth_backend/storageimpl/__init__.py +2 -0
- crossauth_backend/storageimpl/inmemorystorage.py +132 -0
- crossauth_backend/storageimpl/sqlalchemystorage.py +294 -0
- crossauth_backend/utils.py +150 -0
- crossauth_fastapi/__init__.py +23 -0
- crossauth_fastapi/fastapioauthclient.py +2185 -0
- crossauth_fastapi/fastapiresserver.py +300 -0
- crossauth_fastapi/fastapiserver.py +437 -0
- crossauth_fastapi/fastapiserverbase.py +62 -0
- crossauth_fastapi/fastapisession.py +654 -0
- crossauth_fastapi/fastapisessionadapter.py +94 -0
- pycrossauth-0.0.2.dist-info/LICENSE +203 -0
- pycrossauth-0.0.2.dist-info/METADATA +257 -0
- pycrossauth-0.0.2.dist-info/NOTICE +5 -0
- pycrossauth-0.0.2.dist-info/RECORD +34 -0
- pycrossauth-0.0.2.dist-info/WHEEL +5 -0
- pycrossauth-0.0.2.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
# Copyright (c) 2024 Matthew Baker. All rights reserved. Licenced under the Apache Licence 2.0. See LICENSE file
|
|
2
|
+
from crossauth_backend.common.error import ErrorCode, CrossauthError
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import TypedDict, List, Union, Optional, Any, Mapping, NotRequired, Dict
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
import json
|
|
7
|
+
from nulltype import NullType
|
|
8
|
+
from crossauth_backend.common.interfaces import User, UserSecrets, \
|
|
9
|
+
UserInputFields, PartialUser, UserSecretsInputFields, PartialUserSecrets, \
|
|
10
|
+
Key, PartialKey, \
|
|
11
|
+
OAuthClient
|
|
12
|
+
from crossauth_backend.common.error import CrossauthError, ErrorCode
|
|
13
|
+
from crossauth_backend.utils import set_parameter, ParamType
|
|
14
|
+
|
|
15
|
+
#############################
|
|
16
|
+
## UserStorage
|
|
17
|
+
|
|
18
|
+
class UserStorageGetOptions(TypedDict, total=False):
|
|
19
|
+
"""
|
|
20
|
+
Passed to get methods :class: UserStorage.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
skipEmailVerifiedCheck : bool
|
|
24
|
+
"""
|
|
25
|
+
If true, a valid user will be returned even if state is set to `awaitingemailverification`
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
skipActiveCheck : bool
|
|
29
|
+
"""
|
|
30
|
+
If true, a valid user will be returned even if state is not set to `active`
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
normalizeUsername : bool
|
|
34
|
+
"""
|
|
35
|
+
If true, usernames will be matched as lowercase and with diacritics removed.
|
|
36
|
+
Default true,
|
|
37
|
+
|
|
38
|
+
Note: this doesn't apply to the ID column
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
normalizeEmail : bool
|
|
42
|
+
"""
|
|
43
|
+
If true, email addresses (in the email column not in the username column)
|
|
44
|
+
will be matched as lowercase and with diacritics removed.
|
|
45
|
+
Default true.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
class UserStorageOptions(TypedDict, total=False):
|
|
49
|
+
"""
|
|
50
|
+
Options passed to :class: UserStorage constructor
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
userEditableFields : List[str]
|
|
54
|
+
"""
|
|
55
|
+
Fields that users are allowed to edit. Any fields passed to a create or
|
|
56
|
+
update call that are not in this list will be ignored.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
adminEditableFields : List[str]
|
|
60
|
+
"""
|
|
61
|
+
Fields that admins are allowed to edit (in addition to `userEditableFields`)
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
class UserAndSecrets(TypedDict):
|
|
65
|
+
user : User
|
|
66
|
+
secrets: UserSecrets
|
|
67
|
+
|
|
68
|
+
class UserStorage(ABC):
|
|
69
|
+
"""
|
|
70
|
+
Base class for place where user details are stored.
|
|
71
|
+
|
|
72
|
+
This class is subclassed for various types of user storage,
|
|
73
|
+
e.g. PrismaUserStorage is for storing username and password in a database table,
|
|
74
|
+
managed by the Prisma ORM.
|
|
75
|
+
|
|
76
|
+
Username and email searches should be case insensitive, as should their
|
|
77
|
+
unique constraints. ID searches need not be case insensitive.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def user_editable_fields(self):
|
|
82
|
+
return self._user_editable_fields
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def admin_editable_fields(self):
|
|
86
|
+
return self._admin_editable_fields
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def normalize_username(self):
|
|
90
|
+
return self._normalize_username
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def normalize_email(self):
|
|
94
|
+
return self._normalize_email
|
|
95
|
+
|
|
96
|
+
def __init__(self, options: UserStorageOptions = {}):
|
|
97
|
+
"""
|
|
98
|
+
Constructor
|
|
99
|
+
|
|
100
|
+
:param UserStorageOptions options: See :class:`UserStorageOptions`
|
|
101
|
+
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
self._user_editable_fields: List[str] = []
|
|
105
|
+
self._admin_editable_fields: List[str] = []
|
|
106
|
+
self._normalize_username: bool = True
|
|
107
|
+
self._normalize_email: bool = True
|
|
108
|
+
|
|
109
|
+
set_parameter("user_editable_fields", ParamType.String, self, options, "USER_EDITABLE_FIELDS", protected=True)
|
|
110
|
+
set_parameter("admin_editable_fields", ParamType.String, self, options, "USER_EDITABLE_FIELDS", protected=True)
|
|
111
|
+
set_parameter("normalize_username", ParamType.String, self, options, "USER_EDITABLE_FIELDS", protected=True)
|
|
112
|
+
set_parameter("normalize_email", ParamType.String, self, options, "USER_EDITABLE_FIELDS", protected=True)
|
|
113
|
+
|
|
114
|
+
@abstractmethod
|
|
115
|
+
async def get_user_by_username(self, username: str, options: UserStorageGetOptions = {}) -> UserAndSecrets:
|
|
116
|
+
"""
|
|
117
|
+
Returns user matching the given username, or throws an exception.
|
|
118
|
+
|
|
119
|
+
If `normalize_username` is true, the username should be matched normalized and
|
|
120
|
+
lowercased (using normalize())
|
|
121
|
+
|
|
122
|
+
:param str username: the username to return the user of
|
|
123
|
+
:param UserStorageGetOptions options: optionally turn off checks. Used internally
|
|
124
|
+
:raises :class:`crossauth_backend.CrossauthError`: with :class:`ErrorCode` either UserNotExist or Connection
|
|
125
|
+
"""
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
@abstractmethod
|
|
129
|
+
async def get_user_by_id(self, id: Union[str, int], options: UserStorageGetOptions = {}) -> UserAndSecrets:
|
|
130
|
+
"""
|
|
131
|
+
Returns user matching the given user id, or throws an exception.
|
|
132
|
+
|
|
133
|
+
Note that implementations are free to define what the user ID is. It can be a number or string,
|
|
134
|
+
or can simply be `username`.
|
|
135
|
+
|
|
136
|
+
:param str|int id: the user id to return the user of
|
|
137
|
+
:param UserStorageGetOptions options: optionally turn off checks. Used internally
|
|
138
|
+
:raises :class:`crossauth_backend.CrossauthError` with :class:`ErrorCode` either UserNotExist or Connection
|
|
139
|
+
"""
|
|
140
|
+
pass
|
|
141
|
+
|
|
142
|
+
@abstractmethod
|
|
143
|
+
async def get_user_by_email(self, email: str, options: UserStorageGetOptions = {}) -> UserAndSecrets:
|
|
144
|
+
"""
|
|
145
|
+
Returns user matching the given email address, or throws an exception.
|
|
146
|
+
|
|
147
|
+
If `normalize_email` is true, email should be matched normalized and lowercased (using normalize())
|
|
148
|
+
If the email field doesn't exist, username is assumed to be the email column
|
|
149
|
+
|
|
150
|
+
:param email: the email address to return the user of
|
|
151
|
+
:param options: optionally turn off checks. Used internally
|
|
152
|
+
:raises crossauth_backend.CrossauthError: with ErrorCode either UserNotExist or Connection
|
|
153
|
+
"""
|
|
154
|
+
pass
|
|
155
|
+
|
|
156
|
+
@abstractmethod
|
|
157
|
+
async def get_user_by(self, field: str, value: str, options: UserStorageGetOptions = {}) -> UserAndSecrets:
|
|
158
|
+
"""
|
|
159
|
+
Returns user matching the given field, or throws an exception.
|
|
160
|
+
|
|
161
|
+
This does no normalisation. Currently it is only used for the OAuth
|
|
162
|
+
client if you set user_creation_type to "merge", "embed" or "custom".
|
|
163
|
+
|
|
164
|
+
:param str field: the field to match
|
|
165
|
+
:param str value: the value to match
|
|
166
|
+
:param UserStorageGetOptions options: optionally turn off checks. Used internally
|
|
167
|
+
:raises :class:`crossauth_backend.CrossauthError`: with :class:`ErrorCode` either UserNotExist or Connection
|
|
168
|
+
"""
|
|
169
|
+
pass
|
|
170
|
+
|
|
171
|
+
async def create_user(self, user: UserInputFields, secrets: Optional[UserSecretsInputFields] = None) -> User:
|
|
172
|
+
"""
|
|
173
|
+
Creates a user with the given details and secrets.
|
|
174
|
+
|
|
175
|
+
:param crossauth_backend.UserInputFields user: will be put in the User table
|
|
176
|
+
:param crossauth_backend.UserSecretsInputFields|None secrets: will be put in the UserSecrets table
|
|
177
|
+
|
|
178
|
+
:return: the new user as a User object
|
|
179
|
+
|
|
180
|
+
:raises :class:`crossauth_backend.CrossauthError` with :class:`ErrorCode` Configuration
|
|
181
|
+
"""
|
|
182
|
+
raise CrossauthError(ErrorCode.Configuration)
|
|
183
|
+
|
|
184
|
+
@abstractmethod
|
|
185
|
+
async def update_user(self, user: PartialUser, secrets: Optional[PartialUserSecrets] = None) -> None:
|
|
186
|
+
"""
|
|
187
|
+
Updates an existing user with the given details and secrets.
|
|
188
|
+
|
|
189
|
+
If the given user exists in the database, update it with the passed values.
|
|
190
|
+
If it doesn't exist, throw a CrossauthError with ErrorCode InvalidKey.
|
|
191
|
+
|
|
192
|
+
:param crossauth_backend.PartialUser user: The 'id' field must be set, but all others are optional.
|
|
193
|
+
Any parameter not set (or None) will not be updated.
|
|
194
|
+
If you want to set something to None in the database, pass
|
|
195
|
+
the value as None, not undefined.
|
|
196
|
+
:param crossauth_backend.PartialUserSecrets|None secrets: Optional secrets to update
|
|
197
|
+
|
|
198
|
+
"""
|
|
199
|
+
pass
|
|
200
|
+
|
|
201
|
+
@abstractmethod
|
|
202
|
+
async def delete_user_by_username(self, username: str) -> None:
|
|
203
|
+
"""
|
|
204
|
+
If the storage supports this, delete the named user from storage.
|
|
205
|
+
|
|
206
|
+
:param str username: username to delete
|
|
207
|
+
|
|
208
|
+
"""
|
|
209
|
+
pass
|
|
210
|
+
|
|
211
|
+
@abstractmethod
|
|
212
|
+
async def delete_user_by_id(self, id: str|int) -> None:
|
|
213
|
+
"""
|
|
214
|
+
If the storage supports this, delete the user with the given ID from storage.
|
|
215
|
+
|
|
216
|
+
:param str|int id: id of user to delete
|
|
217
|
+
|
|
218
|
+
"""
|
|
219
|
+
pass
|
|
220
|
+
|
|
221
|
+
@abstractmethod
|
|
222
|
+
async def get_users(self, skip: Optional[int] = None, take: Optional[int] = None) -> List[User]:
|
|
223
|
+
"""
|
|
224
|
+
Returns all users in the storage, in a fixed order defined by
|
|
225
|
+
the storage (e.g. alphabetical by username)
|
|
226
|
+
|
|
227
|
+
:param int|None skip: skip this number of records from the start of the set
|
|
228
|
+
:param int|None take: only return at most this number of records
|
|
229
|
+
|
|
230
|
+
:return: an array of User objects
|
|
231
|
+
"""
|
|
232
|
+
pass
|
|
233
|
+
|
|
234
|
+
@staticmethod
|
|
235
|
+
def normalize(string: str) -> str:
|
|
236
|
+
"""
|
|
237
|
+
By default, usernames and emails are stored in lowercase, normalized format.
|
|
238
|
+
This function returns that normalization.
|
|
239
|
+
|
|
240
|
+
:param str string: the string to normalize
|
|
241
|
+
|
|
242
|
+
:return: the normalized string, in lowercase with diacritics removed
|
|
243
|
+
"""
|
|
244
|
+
import unicodedata
|
|
245
|
+
return ''.join(c for c in unicodedata.normalize('NFD', string) if unicodedata.category(c) != 'Mn').lower()
|
|
246
|
+
|
|
247
|
+
class KeyDataEntry(TypedDict):
|
|
248
|
+
data_name : str
|
|
249
|
+
value : NotRequired[Any]
|
|
250
|
+
|
|
251
|
+
###########################################
|
|
252
|
+
## KeyStorage
|
|
253
|
+
|
|
254
|
+
class KeyStorage(ABC):
|
|
255
|
+
"""
|
|
256
|
+
Base class for storing session and API keys.
|
|
257
|
+
|
|
258
|
+
This class is subclassed for various types of session key storage. For example,
|
|
259
|
+
PrismaKeyStorage is for storing sessions in a database table, managed by the Prisma ORM.
|
|
260
|
+
"""
|
|
261
|
+
|
|
262
|
+
@abstractmethod
|
|
263
|
+
async def get_key(self, key: str) -> Key:
|
|
264
|
+
"""
|
|
265
|
+
Returns the matching key in the session storage or raises an exception if it doesn't exist.
|
|
266
|
+
|
|
267
|
+
:param ster key: The key to look up, as it will appear in this storage
|
|
268
|
+
(typically unsigned, hashed)
|
|
269
|
+
|
|
270
|
+
:return: The matching Key record.
|
|
271
|
+
"""
|
|
272
|
+
pass
|
|
273
|
+
|
|
274
|
+
@abstractmethod
|
|
275
|
+
async def save_key(self, userid: Optional[Union[str, int]],
|
|
276
|
+
value: str,
|
|
277
|
+
date_created: datetime,
|
|
278
|
+
expires: Optional[datetime] = None,
|
|
279
|
+
data: Optional[str] = None,
|
|
280
|
+
extra_fields: Optional[Mapping[str, Any]] = None) -> None:
|
|
281
|
+
"""
|
|
282
|
+
Saves a session key in the session storage (e.g., database).
|
|
283
|
+
|
|
284
|
+
:param int|str|None userid: The ID of the user. This matches the primary key in the
|
|
285
|
+
UserStorage implementation.
|
|
286
|
+
:param str value: The key value to store.
|
|
287
|
+
:param datetime date_created: The date/time the key was created.
|
|
288
|
+
:param datetime|None expires: The date/time the key expires.
|
|
289
|
+
:padam str|None data: An optional value, specific to the type of key, e.g., new
|
|
290
|
+
email for email change tokens
|
|
291
|
+
:param Mapping[str, Any]|None extra_fields: These will also be saved in the key record
|
|
292
|
+
|
|
293
|
+
"""
|
|
294
|
+
pass
|
|
295
|
+
|
|
296
|
+
@abstractmethod
|
|
297
|
+
async def update_key(self, key: PartialKey) -> None:
|
|
298
|
+
"""
|
|
299
|
+
If the given session key exists in the database, update it with the
|
|
300
|
+
passed values. If it doesn't exist, raise a CrossauthError with
|
|
301
|
+
ErrorCode 'InvalidKey'.
|
|
302
|
+
|
|
303
|
+
:param crossauth_backend.PartialKey key: The fields defined in this will be updated. 'id' must
|
|
304
|
+
be present and it will not be updated.
|
|
305
|
+
"""
|
|
306
|
+
pass
|
|
307
|
+
|
|
308
|
+
@abstractmethod
|
|
309
|
+
async def delete_key(self, value: str) -> None:
|
|
310
|
+
"""
|
|
311
|
+
Deletes a key from storage (e.g., the database).
|
|
312
|
+
|
|
313
|
+
:param str value: The key to delete
|
|
314
|
+
|
|
315
|
+
"""
|
|
316
|
+
pass
|
|
317
|
+
|
|
318
|
+
@abstractmethod
|
|
319
|
+
async def delete_all_for_user(self, userid: str|int|None,
|
|
320
|
+
prefix: str, except_key: Optional[str] = None) -> None:
|
|
321
|
+
"""
|
|
322
|
+
Deletes all keys from storage for the given user ID
|
|
323
|
+
|
|
324
|
+
:param int|str|None userid: User ID to delete keys for
|
|
325
|
+
:param str prefix: Only keys starting with this prefix will be deleted
|
|
326
|
+
:param str|None except_key: If defined, the key with this value will not be deleted
|
|
327
|
+
|
|
328
|
+
"""
|
|
329
|
+
pass
|
|
330
|
+
|
|
331
|
+
@abstractmethod
|
|
332
|
+
async def delete_matching(self, key: PartialKey) -> None:
|
|
333
|
+
"""
|
|
334
|
+
Deletes all matching the given specs
|
|
335
|
+
|
|
336
|
+
:param crossauth_backend.PartialKey key: Any key matching all defined values in this object will
|
|
337
|
+
be deleted
|
|
338
|
+
"""
|
|
339
|
+
pass
|
|
340
|
+
|
|
341
|
+
@abstractmethod
|
|
342
|
+
async def get_all_for_user(self, userid: str|int|None = None) -> List[Key]:
|
|
343
|
+
"""
|
|
344
|
+
Return all keys matching the given user ID
|
|
345
|
+
|
|
346
|
+
:param str|int|None userid: User to return keys for
|
|
347
|
+
|
|
348
|
+
:return: List[Key]: An array of keys
|
|
349
|
+
"""
|
|
350
|
+
pass
|
|
351
|
+
|
|
352
|
+
@abstractmethod
|
|
353
|
+
async def update_data(self, key_name: str, data_name: str, value: Any|None) -> None:
|
|
354
|
+
"""
|
|
355
|
+
The 'data' field in a key entry is a JSON string. This method should
|
|
356
|
+
atomically update a field in it.
|
|
357
|
+
|
|
358
|
+
:param str key_name: The name of the key to update, as it appears in the table.
|
|
359
|
+
:param str data_name: The field name to update. This can contain dots, e.g.,
|
|
360
|
+
'part1.part2', which means 'part2' within 'part1' is updated.
|
|
361
|
+
:param Any|None value: The new value.
|
|
362
|
+
|
|
363
|
+
"""
|
|
364
|
+
pass
|
|
365
|
+
|
|
366
|
+
@abstractmethod
|
|
367
|
+
async def update_many_data(self, key_name: str,
|
|
368
|
+
data_array: List[KeyDataEntry]) -> None:
|
|
369
|
+
"""
|
|
370
|
+
Same as 'update_data' but updates several keys.
|
|
371
|
+
|
|
372
|
+
Ensure it is done as a single transaction.
|
|
373
|
+
|
|
374
|
+
:param str key_name: The key to update
|
|
375
|
+
:param List[crossauth_backend.KeyDataEntry data_array: dataName and value pairs
|
|
376
|
+
|
|
377
|
+
"""
|
|
378
|
+
pass
|
|
379
|
+
|
|
380
|
+
@abstractmethod
|
|
381
|
+
async def delete_data(self, key_name: str, data_name: str) -> None:
|
|
382
|
+
"""
|
|
383
|
+
The 'data' field in a key entry is a JSON string. This method should
|
|
384
|
+
atomically delete a field in it.
|
|
385
|
+
|
|
386
|
+
:param str key_name: The name of the key to update, as it appears in the table.
|
|
387
|
+
:param str data_name: The field name to delete. This can contain dots, e.g.,
|
|
388
|
+
'part1.part2', which means 'part2' within 'part1' is deleted.
|
|
389
|
+
"""
|
|
390
|
+
pass
|
|
391
|
+
|
|
392
|
+
@staticmethod
|
|
393
|
+
def decode_data(data: Optional[str]) -> Dict[str, Any]:
|
|
394
|
+
"""
|
|
395
|
+
Returns an object decoded from the data field as a JSON string
|
|
396
|
+
|
|
397
|
+
:param str|None data: The JSON string to decode
|
|
398
|
+
|
|
399
|
+
:return: The parsed JSON object
|
|
400
|
+
|
|
401
|
+
:raises json.JSONDecodeError: If data is not a valid JSON string
|
|
402
|
+
"""
|
|
403
|
+
if data is None or data == "":
|
|
404
|
+
return {}
|
|
405
|
+
return json.loads(data)
|
|
406
|
+
|
|
407
|
+
@staticmethod
|
|
408
|
+
def encode_data(data: Optional[Dict[str, Any]] = None) -> str:
|
|
409
|
+
"""
|
|
410
|
+
Returns a JSON string encoded from the given object
|
|
411
|
+
|
|
412
|
+
:param Dict[str, Any]|None data: The object to encode
|
|
413
|
+
|
|
414
|
+
:return: A JSON string
|
|
415
|
+
"""
|
|
416
|
+
if not data:
|
|
417
|
+
return "{}"
|
|
418
|
+
return json.dumps(data)
|
|
419
|
+
|
|
420
|
+
def _update_data_internal(self, data: Dict[str, Any], data_name: str, value: Any) -> Optional[Dict[str, Any]]:
|
|
421
|
+
"""
|
|
422
|
+
Helper function for implementing 'update_data'
|
|
423
|
+
|
|
424
|
+
:param Dict[str, Any] data: Parsed data string extracted from the key.
|
|
425
|
+
:param str data_name: Name of field to update (may contain dots)
|
|
426
|
+
:param Any value: The value to set it to
|
|
427
|
+
|
|
428
|
+
:return: New data object if changes were made, None otherwise
|
|
429
|
+
"""
|
|
430
|
+
if "." in data_name:
|
|
431
|
+
parts = data_name.split(".")
|
|
432
|
+
data1 = data
|
|
433
|
+
for i in range(len(parts) - 1):
|
|
434
|
+
if data1 is None:
|
|
435
|
+
break
|
|
436
|
+
data1 = data1.get(parts[i])
|
|
437
|
+
if data1 is not None:
|
|
438
|
+
data1[parts[-1]] = value
|
|
439
|
+
return data
|
|
440
|
+
else:
|
|
441
|
+
data[data_name] = value
|
|
442
|
+
return data
|
|
443
|
+
|
|
444
|
+
def _delete_data_internal(self, data: Dict[str, Any], data_name: str) -> bool:
|
|
445
|
+
"""
|
|
446
|
+
Helper function for implementing 'delete_data'
|
|
447
|
+
|
|
448
|
+
:param Dict[str, Any] data: Parsed data string extracted from the key. Results will be
|
|
449
|
+
written back to this
|
|
450
|
+
:param str data_name: Name of field to delete (may contain dots)
|
|
451
|
+
|
|
452
|
+
:return: True if modifications were made, False otherwise
|
|
453
|
+
"""
|
|
454
|
+
if "." in data_name:
|
|
455
|
+
parts = data_name.split(".")
|
|
456
|
+
data1 = data
|
|
457
|
+
for i in range(len(parts) - 1):
|
|
458
|
+
if data1 is None:
|
|
459
|
+
break
|
|
460
|
+
data1 = data1.get(parts[i])
|
|
461
|
+
if data1 is not None and parts[-1] in data1:
|
|
462
|
+
del data1[parts[-1]]
|
|
463
|
+
return True
|
|
464
|
+
return False
|
|
465
|
+
else:
|
|
466
|
+
if data_name in data:
|
|
467
|
+
del data[data_name]
|
|
468
|
+
return True
|
|
469
|
+
return False
|
|
470
|
+
|
|
471
|
+
####################################
|
|
472
|
+
## OAuthClient
|
|
473
|
+
|
|
474
|
+
class OAuthClientStorageOptions(TypedDict, total=False):
|
|
475
|
+
pass
|
|
476
|
+
|
|
477
|
+
class OAuthClientStorage(ABC):
|
|
478
|
+
"""
|
|
479
|
+
Base class for storing OAuth clients.
|
|
480
|
+
|
|
481
|
+
This class is subclassed for various types of client storage. For example, PrismaOAuthClientStorage
|
|
482
|
+
is for storing clients in a database table, managed by the Prisma ORM.
|
|
483
|
+
"""
|
|
484
|
+
|
|
485
|
+
def __init__(self, options: OAuthClientStorageOptions = {}):
|
|
486
|
+
"""
|
|
487
|
+
Constructor
|
|
488
|
+
|
|
489
|
+
:param crossauth_backend.OAuthClientStorageOptions options: see OAuthClientStorageOptions
|
|
490
|
+
|
|
491
|
+
"""
|
|
492
|
+
pass
|
|
493
|
+
|
|
494
|
+
@abstractmethod
|
|
495
|
+
async def get_client_by_id(self, client_id: str) -> OAuthClient:
|
|
496
|
+
"""
|
|
497
|
+
Returns the matching client by its auto-generated id in the storage or
|
|
498
|
+
throws an exception if it doesn't exist.
|
|
499
|
+
|
|
500
|
+
:param str client_id: the client_id to look up
|
|
501
|
+
|
|
502
|
+
:return: he matching OAuthClient object.
|
|
503
|
+
"""
|
|
504
|
+
pass
|
|
505
|
+
|
|
506
|
+
@abstractmethod
|
|
507
|
+
async def get_client_by_name(self, name: str, userid: str|int|None = None) -> List[OAuthClient]:
|
|
508
|
+
"""
|
|
509
|
+
Returns the matching client in the storage by friendly name or
|
|
510
|
+
throws an exception if it doesn't exist.
|
|
511
|
+
|
|
512
|
+
:param str name: the client name to look up
|
|
513
|
+
:param str|int|None userid: if defined, only return clients belonging to this user.
|
|
514
|
+
if None, return only clients with a null userid.
|
|
515
|
+
if not provided, return all clients with this name.
|
|
516
|
+
|
|
517
|
+
:return: A list of OAuthClient objects.
|
|
518
|
+
|
|
519
|
+
:raises :class:`crossauth_backend.CrossauthError`: with :class:`ErrorCode` of 'InvalidSessionId' if a match was not found in session storage.
|
|
520
|
+
"""
|
|
521
|
+
pass
|
|
522
|
+
|
|
523
|
+
@abstractmethod
|
|
524
|
+
async def get_clients(self, skip: Optional[int] = None, take: Optional[int] = None, userid: str|int|None = None) -> List[OAuthClient]:
|
|
525
|
+
"""
|
|
526
|
+
Returns all clients in alphabetical order of client name.
|
|
527
|
+
|
|
528
|
+
:param int|None skip: skip this number of records from the start in alphabetical order
|
|
529
|
+
:param int|None take: return at most this number of records
|
|
530
|
+
:param str|int|None userid: if defined, only return clients belonging to this user.
|
|
531
|
+
if None, return only clients with a null userid.
|
|
532
|
+
if not provided, return all clients.
|
|
533
|
+
|
|
534
|
+
:return: A list of OAuthClient objects.
|
|
535
|
+
"""
|
|
536
|
+
pass
|
|
537
|
+
|
|
538
|
+
@abstractmethod
|
|
539
|
+
async def create_client(self, client: OAuthClient) -> OAuthClient:
|
|
540
|
+
"""
|
|
541
|
+
Creates and returns a new client with random ID and optionally secret.
|
|
542
|
+
|
|
543
|
+
Saves in the database.
|
|
544
|
+
|
|
545
|
+
:param crossauth_backend.OAuthClient client: the client to save.
|
|
546
|
+
|
|
547
|
+
:return: The new client.
|
|
548
|
+
"""
|
|
549
|
+
pass
|
|
550
|
+
|
|
551
|
+
@abstractmethod
|
|
552
|
+
async def update_client(self, client: OAuthClient) -> None:
|
|
553
|
+
"""
|
|
554
|
+
If the given session key exists in the database, update it with the
|
|
555
|
+
passed values. If it doesn't exist, throw a CrossauthError with
|
|
556
|
+
'InvalidClient'.
|
|
557
|
+
|
|
558
|
+
:param crossauth_backend.OAuthClient client: all fields to update (client_id must be set but will not be updated)
|
|
559
|
+
|
|
560
|
+
:raises :class:`crossauth_backend.CrossauthError`: with 'InvalidClient' if the client doesn't exist.
|
|
561
|
+
"""
|
|
562
|
+
pass
|
|
563
|
+
|
|
564
|
+
@abstractmethod
|
|
565
|
+
async def delete_client(self, client_id: str) -> None:
|
|
566
|
+
"""
|
|
567
|
+
Deletes a key from storage.
|
|
568
|
+
|
|
569
|
+
:param str client_id: the client to delete
|
|
570
|
+
|
|
571
|
+
"""
|
|
572
|
+
pass
|
|
573
|
+
|
|
574
|
+
###########################################
|
|
575
|
+
## OAuthAuthorizationStorage
|
|
576
|
+
|
|
577
|
+
class OAuthAuthorizationStorageOptions(TypedDict, total=False):
|
|
578
|
+
pass
|
|
579
|
+
|
|
580
|
+
from abc import ABC, abstractmethod
|
|
581
|
+
from typing import List, Union, Optional
|
|
582
|
+
|
|
583
|
+
class OAuthAuthorizationStorage(ABC):
|
|
584
|
+
"""
|
|
585
|
+
Base class for storing scopes that have been authorized by a user
|
|
586
|
+
(or for client credentials, for a client).
|
|
587
|
+
|
|
588
|
+
This class is subclassed for various types of storage. For example,
|
|
589
|
+
PrismaOAuthAuthorizationStorage is for storing in a database table,
|
|
590
|
+
managed by the Prisma ORM.
|
|
591
|
+
"""
|
|
592
|
+
|
|
593
|
+
def __init__(self, options: OAuthAuthorizationStorageOptions = {}):
|
|
594
|
+
"""
|
|
595
|
+
Constructor
|
|
596
|
+
|
|
597
|
+
:param OAuthAuthorizationStorageOptions options: see :class:`OAuthAuthorizationStorageOptions`
|
|
598
|
+
|
|
599
|
+
"""
|
|
600
|
+
pass
|
|
601
|
+
|
|
602
|
+
@abstractmethod
|
|
603
|
+
async def get_authorizations(self, client_id: str, userid: str|int|None = None) -> List[Optional[str]]:
|
|
604
|
+
"""
|
|
605
|
+
Returns the matching all scopes authorized for the given client and optionally user.
|
|
606
|
+
|
|
607
|
+
:param str client_id: the client_id to look up
|
|
608
|
+
:param int|str|None userid: the userid to look up, None for a client authorization not user authorization
|
|
609
|
+
|
|
610
|
+
:return: The authorized scopes as a list.
|
|
611
|
+
"""
|
|
612
|
+
pass
|
|
613
|
+
|
|
614
|
+
@abstractmethod
|
|
615
|
+
async def update_authorizations(self, client_id: str, userid: str|int|NullType, authorizations: List[str|NullType]) -> None:
|
|
616
|
+
"""
|
|
617
|
+
Saves a new set of authorizations for the given client and optionally user.
|
|
618
|
+
|
|
619
|
+
Deletes the old ones.
|
|
620
|
+
|
|
621
|
+
:param str client_id: the client_id to look up
|
|
622
|
+
:param str|int|None userid: the userid to look up, None for a client authorization not user authorization
|
|
623
|
+
:param List[str|None]authorizations: new set of authorized scopes, which may be empty
|
|
624
|
+
|
|
625
|
+
"""
|
|
626
|
+
pass
|