-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailu.py
54 lines (44 loc) · 2.25 KB
/
mailu.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
import logging
import aiohttp
from pydantic import AnyUrl
from auth_backend.auth_method.outer import ConnectionIssue, OuterAuthMeta
from auth_backend.settings import Settings
logger = logging.getLogger(__name__)
class MailuOuterAuthSettings(Settings):
MAILU_AUTH_BASE_URL: AnyUrl | None = None
MAILU_AUTH_API_KEY: str | None = None
class MailuOuterAuth(OuterAuthMeta):
prefix = '/mailu'
settings = MailuOuterAuthSettings()
@classmethod
async def _is_outer_user_exists(cls, username: str) -> bool:
"""Проверяет наличие пользователя на сервере Mailu"""
logger.debug("_is_outer_user_exists class=%s started", cls.get_name())
async with aiohttp.ClientSession() as session:
async with session.get(
str(cls.settings.MAILU_AUTH_BASE_URL).removesuffix('/') + '/api/v1/user/' + username,
headers={"Authorization": cls.settings.MAILU_AUTH_API_KEY},
) as response:
if not response.ok:
raise ConnectionIssue(response.text)
res: dict[str] = await response.json()
return res.get('email') == username
@classmethod
async def _update_outer_user_password(cls, username: str, password: str):
"""Устанавливает пользователю новый пароль на сервере Mailu"""
logger.debug("_update_outer_user_password class=%s started", cls.get_name())
res = False
async with aiohttp.ClientSession() as session:
async with session.patch(
str(cls.settings.MAILU_AUTH_BASE_URL).removesuffix('/') + '/api/v1/user/' + username,
headers={"Authorization": cls.settings.MAILU_AUTH_API_KEY},
json={'raw_password': password},
) as response:
if not response.ok:
raise ConnectionIssue(response.text)
res: dict[str] = response.ok
logger.debug("_update_outer_user_password class=%s response %s", cls.get_name(), str(response.status))
if res:
logger.info("User %s updated in Mailu", username)
else:
logger.error("User %s can't be updated in Mailu. Error: %s", username, res)