-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.py
66 lines (53 loc) · 1.94 KB
/
user.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
import binascii
from starlette.authentication import AuthenticationBackend, AuthenticationError, BaseUser, AuthCredentials
from jmap import errors
from jmap.account import UserAccount
class User(BaseUser):
"""
User is person with credentials.
User can have access to multiple (shared) accounts.
User has one personal account.
"""
def __init__(self, username: str, password: str, loop=None) -> None:
self.username = username
self.accounts = {
username: UserAccount(username, password, loop=loop),
}
self.sessionState = '0'
async def ainit(self):
for account in self.accounts.values():
try:
await account.ainit()
except AttributeError:
continue
def get_account(self, accountId):
try:
return self.accounts[accountId]
except KeyError:
raise errors.accountNotFound()
@property
def is_authenticated(self) -> bool:
return True
@property
def display_name(self) -> str:
return self.username
class BasicAuthBackend(AuthenticationBackend):
def __init__(self):
self.users = {}
async def authenticate(self, request):
if "Authorization" not in request.headers:
return
auth = request.headers["Authorization"]
try:
scheme, credentials = auth.split()
if scheme.lower() != 'basic':
return
decoded = binascii.a2b_base64(credentials).decode("ascii")
except (ValueError, UnicodeDecodeError, binascii.Error) as exc:
raise AuthenticationError('Invalid basic auth credentials')
if decoded not in self.users:
username, _, password = decoded.partition(":")
user = User(username, password)
await user.ainit()
self.users[decoded] = user
return AuthCredentials(["authenticated"]), self.users[decoded]