1
|
|
|
""" |
2
|
|
|
byceps.services.authentication.service |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from typing import Optional |
10
|
|
|
|
11
|
1 |
|
from ...typing import UserID |
12
|
|
|
|
13
|
1 |
|
from ..user import service as user_service |
14
|
1 |
|
from ..user.transfer.models import User |
15
|
|
|
|
16
|
1 |
|
from .exceptions import AuthenticationFailed |
17
|
1 |
|
from .password import service as password_service |
18
|
|
|
|
19
|
|
|
|
20
|
1 |
|
def authenticate(screen_name_or_email_address: str, password: str) -> User: |
21
|
|
|
"""Try to authenticate the user. |
22
|
|
|
|
23
|
|
|
Return the user object on success, or raise an exception on failure. |
24
|
|
|
""" |
25
|
|
|
# Look up user by screen name or email address. |
26
|
1 |
|
user_id = _find_user_id_by_screen_name_or_email_address( |
27
|
|
|
screen_name_or_email_address |
28
|
|
|
) |
29
|
1 |
|
if user_id is None: |
30
|
|
|
# Screen name/email address is unknown. |
31
|
1 |
|
raise AuthenticationFailed() |
32
|
|
|
|
33
|
|
|
# Ensure account is initialized, not suspended, and not deleted. |
34
|
1 |
|
user = user_service.find_active_user(user_id) |
35
|
1 |
|
if user is None: |
36
|
|
|
# Should not happen as the user has been looked up before. |
37
|
1 |
|
raise AuthenticationFailed() |
38
|
|
|
|
39
|
|
|
# Verify credentials. |
40
|
1 |
|
if not password_service.is_password_valid_for_user(user.id, password): |
41
|
|
|
# Password does not match. |
42
|
1 |
|
raise AuthenticationFailed() |
43
|
|
|
|
44
|
1 |
|
return user |
45
|
|
|
|
46
|
|
|
|
47
|
1 |
|
def _find_user_id_by_screen_name_or_email_address( |
48
|
|
|
screen_name_or_email_address: str, |
49
|
|
|
) -> Optional[UserID]: |
50
|
1 |
|
if '@' in screen_name_or_email_address: |
51
|
1 |
|
user = user_service.find_user_by_email_address( |
52
|
|
|
screen_name_or_email_address |
53
|
|
|
) |
54
|
|
|
else: |
55
|
1 |
|
user = user_service.find_user_by_screen_name( |
56
|
|
|
screen_name_or_email_address, case_insensitive=True |
57
|
|
|
) |
58
|
|
|
|
59
|
1 |
|
if user is None: |
60
|
1 |
|
return None |
61
|
|
|
|
62
|
|
|
return user.id |
63
|
|
|
|