Skip to content

Commit bec6e12

Browse files
committed
refactor: Add Auth adaptor for transforming result data to DTO
1 parent 57d2ebd commit bec6e12

File tree

2 files changed

+108
-65
lines changed

2 files changed

+108
-65
lines changed
Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,8 @@
11
"""
22
Common DTOs for auth system used by both Client SDK and Manager.
3-
"""
4-
5-
from __future__ import annotations
63
7-
from .request import (
8-
AuthorizeRequest,
9-
GetRoleRequest,
10-
SignoutRequest,
11-
SignupRequest,
12-
UpdateFullNameRequest,
13-
UpdatePasswordNoAuthRequest,
14-
UpdatePasswordRequest,
15-
UploadSSHKeypairRequest,
16-
)
17-
from .response import (
18-
AuthorizeResponse,
19-
GetRoleResponse,
20-
GetSSHKeypairResponse,
21-
SignoutResponse,
22-
SignupResponse,
23-
SSHKeypairResponse,
24-
UpdateFullNameResponse,
25-
UpdatePasswordNoAuthResponse,
26-
UpdatePasswordResponse,
27-
)
28-
from .types import (
29-
AuthResponse,
30-
AuthResponseType,
31-
AuthSuccessResponse,
32-
AuthTokenType,
33-
RequireTwoFactorAuthResponse,
34-
RequireTwoFactorRegistrationResponse,
35-
TwoFactorType,
36-
parse_auth_response,
37-
)
38-
39-
__all__ = (
40-
# Types
41-
"AuthTokenType",
42-
"AuthResponseType",
43-
"TwoFactorType",
44-
"AuthResponse",
45-
"AuthSuccessResponse",
46-
"RequireTwoFactorRegistrationResponse",
47-
"RequireTwoFactorAuthResponse",
48-
"parse_auth_response",
49-
# Request DTOs
50-
"AuthorizeRequest",
51-
"GetRoleRequest",
52-
"SignupRequest",
53-
"SignoutRequest",
54-
"UpdateFullNameRequest",
55-
"UpdatePasswordRequest",
56-
"UpdatePasswordNoAuthRequest",
57-
"UploadSSHKeypairRequest",
58-
# Response DTOs
59-
"AuthorizeResponse",
60-
"GetRoleResponse",
61-
"SignupResponse",
62-
"SignoutResponse",
63-
"UpdateFullNameResponse",
64-
"UpdatePasswordResponse",
65-
"UpdatePasswordNoAuthResponse",
66-
"GetSSHKeypairResponse",
67-
"SSHKeypairResponse",
68-
)
4+
Import directly from submodules:
5+
- types: AuthTokenType, AuthResponseType, TwoFactorType, AuthResponse, etc.
6+
- request: AuthorizeRequest, SignupRequest, SignoutRequest, etc.
7+
- response: AuthorizeResponse, SignupResponse, SignoutResponse, etc.
8+
"""
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
Adapters to convert auth ActionResult objects to Response DTOs.
3+
"""
4+
5+
from __future__ import annotations
6+
7+
from ai.backend.common.dto.manager.auth.response import (
8+
AuthorizeResponse,
9+
GetRoleResponse,
10+
GetSSHKeypairResponse,
11+
SignupResponse,
12+
SSHKeypairResponse,
13+
UpdatePasswordNoAuthResponse,
14+
)
15+
from ai.backend.common.dto.manager.auth.types import (
16+
AuthResponseType,
17+
AuthSuccessResponse,
18+
)
19+
from ai.backend.manager.services.auth.actions.authorize import AuthorizeActionResult
20+
from ai.backend.manager.services.auth.actions.generate_ssh_keypair import (
21+
GenerateSSHKeypairActionResult,
22+
)
23+
from ai.backend.manager.services.auth.actions.get_role import GetRoleActionResult
24+
from ai.backend.manager.services.auth.actions.get_ssh_keypair import GetSSHKeypairActionResult
25+
from ai.backend.manager.services.auth.actions.signup import SignupActionResult
26+
from ai.backend.manager.services.auth.actions.update_password_no_auth import (
27+
UpdatePasswordNoAuthActionResult,
28+
)
29+
from ai.backend.manager.services.auth.actions.upload_ssh_keypair import UploadSSHKeypairActionResult
30+
31+
__all__ = ("AuthAdapter",)
32+
33+
34+
class AuthAdapter:
35+
"""Adapter for converting auth ActionResult to Response DTO."""
36+
37+
def convert_authorize_result(self, result: AuthorizeActionResult) -> AuthorizeResponse:
38+
"""Convert AuthorizeActionResult to AuthorizeResponse.
39+
40+
Note: Caller must check result.stream_response before calling this method.
41+
This method only handles the keypair authentication case.
42+
"""
43+
if result.authorization_result is None:
44+
raise ValueError("authorization_result is required for keypair authentication")
45+
auth_result = result.authorization_result
46+
return AuthorizeResponse(
47+
data=AuthSuccessResponse(
48+
response_type=AuthResponseType.SUCCESS,
49+
access_key=auth_result.access_key,
50+
secret_key=auth_result.secret_key,
51+
role=auth_result.role,
52+
status=auth_result.status,
53+
)
54+
)
55+
56+
def convert_get_role_result(self, result: GetRoleActionResult) -> GetRoleResponse:
57+
"""Convert GetRoleActionResult to GetRoleResponse."""
58+
return GetRoleResponse(
59+
global_role=result.global_role,
60+
domain_role=result.domain_role,
61+
group_role=result.group_role,
62+
)
63+
64+
def convert_signup_result(self, result: SignupActionResult) -> SignupResponse:
65+
"""Convert SignupActionResult to SignupResponse."""
66+
return SignupResponse(
67+
access_key=result.access_key,
68+
secret_key=result.secret_key,
69+
)
70+
71+
def convert_update_password_no_auth_result(
72+
self, result: UpdatePasswordNoAuthActionResult
73+
) -> UpdatePasswordNoAuthResponse:
74+
"""Convert UpdatePasswordNoAuthActionResult to UpdatePasswordNoAuthResponse."""
75+
return UpdatePasswordNoAuthResponse(
76+
password_changed_at=result.password_changed_at,
77+
)
78+
79+
def convert_get_ssh_keypair_result(
80+
self, result: GetSSHKeypairActionResult
81+
) -> GetSSHKeypairResponse:
82+
"""Convert GetSSHKeypairActionResult to GetSSHKeypairResponse (public key only)."""
83+
return GetSSHKeypairResponse(
84+
ssh_public_key=result.public_key,
85+
)
86+
87+
def convert_generate_ssh_keypair_result(
88+
self, result: GenerateSSHKeypairActionResult
89+
) -> SSHKeypairResponse:
90+
"""Convert GenerateSSHKeypairActionResult to SSHKeypairResponse."""
91+
return SSHKeypairResponse(
92+
ssh_public_key=result.ssh_keypair.ssh_public_key,
93+
ssh_private_key=result.ssh_keypair.ssh_private_key,
94+
)
95+
96+
def convert_upload_ssh_keypair_result(
97+
self, result: UploadSSHKeypairActionResult
98+
) -> SSHKeypairResponse:
99+
"""Convert UploadSSHKeypairActionResult to SSHKeypairResponse."""
100+
return SSHKeypairResponse(
101+
ssh_public_key=result.ssh_keypair.ssh_public_key,
102+
ssh_private_key=result.ssh_keypair.ssh_private_key,
103+
)

0 commit comments

Comments
 (0)