Coverage for fastid / auth / router.py: 100%
14 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-10-18 16:37 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-10-18 16:37 +0000
1from typing import Annotated, Any
3from fastapi import APIRouter, BackgroundTasks, Depends, Form, status
4from starlette.responses import Response
6from fastid.auth.dependencies import AuthDep, UserDep, cookie_transport, vt_transport
7from fastid.auth.exceptions import NotSupportedGrantError
8from fastid.auth.grants import (
9 AuthorizationCodeGrant,
10 ClientCredentialsGrant,
11 PasswordGrant,
12 RefreshTokenGrant,
13)
14from fastid.auth.schemas import (
15 OAuth2AuthorizationCodeRequest,
16 OAuth2ClientCredentialsRequest,
17 OAuth2Grant,
18 OAuth2PasswordRequest,
19 OAuth2RefreshTokenRequest,
20 OAuth2TokenRequest,
21 SubscriberType,
22 TokenResponse,
23 UserCreate,
24 UserDTO,
25)
26from fastid.notify.dependencies import NotifyDep
27from fastid.notify.schemas import PushNotificationRequest
28from fastid.webhooks.dependencies import WebhooksDep
29from fastid.webhooks.models import WebhookType
30from fastid.webhooks.schemas import SendWebhookRequest
32router = APIRouter(tags=["Auth"])
35@router.post("/register", status_code=status.HTTP_201_CREATED, response_model=UserDTO)
36async def register(
37 service: AuthDep,
38 notify: NotifyDep,
39 webhooks: WebhooksDep,
40 dto: UserCreate,
41 background: BackgroundTasks,
42) -> Any:
43 user = await service.register(dto)
44 notification = PushNotificationRequest(template="welcome")
45 background.add_task(notify.push, user, notification) # pragma: nocover
46 webhook = SendWebhookRequest(
47 type=WebhookType.user_registration, payload=UserDTO.model_validate(user).model_dump(mode="json")
48 )
49 background.add_task(webhooks.send, webhook) # pragma: nocover
50 return user # pragma: nocover
53@router.post(
54 "/login",
55 status_code=status.HTTP_200_OK,
56 response_model=TokenResponse,
57)
58async def login(
59 form: Annotated[OAuth2PasswordRequest, Form()],
60 password_grant: Annotated[PasswordGrant, Depends()],
61 webhooks: WebhooksDep,
62 background: BackgroundTasks,
63) -> Any:
64 match form.grant_type:
65 case OAuth2Grant.password:
66 auth_data = await password_grant.authorize(OAuth2PasswordRequest.model_validate(form))
67 case _:
68 raise NotSupportedGrantError
69 assert auth_data.user is not None
70 webhook = SendWebhookRequest(type=WebhookType.user_login, payload={"user": auth_data.user.model_dump(mode="json")})
71 background.add_task(webhooks.send, webhook) # pragma: nocover
72 return cookie_transport.get_login_response(auth_data.token)
75@router.post(
76 "/token",
77 status_code=status.HTTP_200_OK,
78 response_model=TokenResponse,
79)
80async def authorize( # noqa: PLR0913
81 form: Annotated[OAuth2TokenRequest, Form()],
82 password_grant: Annotated[PasswordGrant, Depends()],
83 authorization_code_grant: Annotated[AuthorizationCodeGrant, Depends()],
84 refresh_token_grant: Annotated[RefreshTokenGrant, Depends()],
85 client_credentials_grant: Annotated[ClientCredentialsGrant, Depends()],
86 webhooks: WebhooksDep,
87 background: BackgroundTasks,
88) -> Any:
89 match form.grant_type:
90 case OAuth2Grant.password:
91 auth_data = await password_grant.authorize(OAuth2PasswordRequest.model_validate(form))
92 case OAuth2Grant.authorization_code:
93 auth_data = await authorization_code_grant.authorize(OAuth2AuthorizationCodeRequest.model_validate(form))
94 case OAuth2Grant.refresh_token:
95 auth_data = await refresh_token_grant.authorize(OAuth2RefreshTokenRequest.model_validate(form))
96 case OAuth2Grant.client_credentials:
97 auth_data = await client_credentials_grant.authorize(OAuth2ClientCredentialsRequest.model_validate(form))
98 case _:
99 raise NotSupportedGrantError
100 if auth_data.sub_type == SubscriberType.user and auth_data.user is not None:
101 webhook = SendWebhookRequest(
102 type=WebhookType.user_login, payload={"user": auth_data.user.model_dump(mode="json")}
103 )
104 background.add_task(webhooks.send, webhook) # pragma: nocover
105 return auth_data.token
108@router.get("/userinfo", response_model=UserDTO, status_code=status.HTTP_200_OK)
109def me(user: UserDep) -> Any:
110 return user
113@router.get(
114 "/logout",
115 status_code=status.HTTP_200_OK,
116)
117def logout() -> Any:
118 response = Response()
119 response = cookie_transport.delete_token(response)
120 return vt_transport.delete_token(response)