|
| 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 import ( |
| 8 | + AuthorizeResponse, |
| 9 | + AuthResponseType, |
| 10 | + AuthSuccessResponse, |
| 11 | + GetRoleResponse, |
| 12 | + GetSSHKeypairResponse, |
| 13 | + SignupResponse, |
| 14 | + SSHKeypairResponse, |
| 15 | + UpdatePasswordNoAuthResponse, |
| 16 | +) |
| 17 | +from ai.backend.manager.services.auth.actions.authorize import AuthorizeActionResult |
| 18 | +from ai.backend.manager.services.auth.actions.generate_ssh_keypair import ( |
| 19 | + GenerateSSHKeypairActionResult, |
| 20 | +) |
| 21 | +from ai.backend.manager.services.auth.actions.get_role import GetRoleActionResult |
| 22 | +from ai.backend.manager.services.auth.actions.get_ssh_keypair import GetSSHKeypairActionResult |
| 23 | +from ai.backend.manager.services.auth.actions.signup import SignupActionResult |
| 24 | +from ai.backend.manager.services.auth.actions.update_password_no_auth import ( |
| 25 | + UpdatePasswordNoAuthActionResult, |
| 26 | +) |
| 27 | +from ai.backend.manager.services.auth.actions.upload_ssh_keypair import UploadSSHKeypairActionResult |
| 28 | + |
| 29 | +__all__ = ("AuthAdapter",) |
| 30 | + |
| 31 | + |
| 32 | +class AuthAdapter: |
| 33 | + """Adapter for converting auth ActionResult to Response DTO.""" |
| 34 | + |
| 35 | + def convert_authorize_result(self, result: AuthorizeActionResult) -> AuthorizeResponse: |
| 36 | + """Convert AuthorizeActionResult to AuthorizeResponse. |
| 37 | +
|
| 38 | + Note: Caller must check result.stream_response before calling this method. |
| 39 | + This method only handles the keypair authentication case. |
| 40 | + """ |
| 41 | + if result.authorization_result is None: |
| 42 | + raise ValueError("authorization_result is required for keypair authentication") |
| 43 | + auth_result = result.authorization_result |
| 44 | + return AuthorizeResponse( |
| 45 | + data=AuthSuccessResponse( |
| 46 | + response_type=AuthResponseType.SUCCESS, |
| 47 | + access_key=auth_result.access_key, |
| 48 | + secret_key=auth_result.secret_key, |
| 49 | + role=auth_result.role, |
| 50 | + status=auth_result.status, |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | + def convert_get_role_result(self, result: GetRoleActionResult) -> GetRoleResponse: |
| 55 | + """Convert GetRoleActionResult to GetRoleResponse.""" |
| 56 | + return GetRoleResponse( |
| 57 | + global_role=result.global_role, |
| 58 | + domain_role=result.domain_role, |
| 59 | + group_role=result.group_role, |
| 60 | + ) |
| 61 | + |
| 62 | + def convert_signup_result(self, result: SignupActionResult) -> SignupResponse: |
| 63 | + """Convert SignupActionResult to SignupResponse.""" |
| 64 | + return SignupResponse( |
| 65 | + access_key=result.access_key, |
| 66 | + secret_key=result.secret_key, |
| 67 | + ) |
| 68 | + |
| 69 | + def convert_update_password_no_auth_result( |
| 70 | + self, result: UpdatePasswordNoAuthActionResult |
| 71 | + ) -> UpdatePasswordNoAuthResponse: |
| 72 | + """Convert UpdatePasswordNoAuthActionResult to UpdatePasswordNoAuthResponse.""" |
| 73 | + return UpdatePasswordNoAuthResponse( |
| 74 | + password_changed_at=result.password_changed_at, |
| 75 | + ) |
| 76 | + |
| 77 | + def convert_get_ssh_keypair_result( |
| 78 | + self, result: GetSSHKeypairActionResult |
| 79 | + ) -> GetSSHKeypairResponse: |
| 80 | + """Convert GetSSHKeypairActionResult to GetSSHKeypairResponse (public key only).""" |
| 81 | + return GetSSHKeypairResponse( |
| 82 | + ssh_public_key=result.public_key, |
| 83 | + ) |
| 84 | + |
| 85 | + def convert_generate_ssh_keypair_result( |
| 86 | + self, result: GenerateSSHKeypairActionResult |
| 87 | + ) -> SSHKeypairResponse: |
| 88 | + """Convert GenerateSSHKeypairActionResult to SSHKeypairResponse.""" |
| 89 | + return SSHKeypairResponse( |
| 90 | + ssh_public_key=result.ssh_keypair.ssh_public_key, |
| 91 | + ssh_private_key=result.ssh_keypair.ssh_private_key, |
| 92 | + ) |
| 93 | + |
| 94 | + def convert_upload_ssh_keypair_result( |
| 95 | + self, result: UploadSSHKeypairActionResult |
| 96 | + ) -> SSHKeypairResponse: |
| 97 | + """Convert UploadSSHKeypairActionResult to SSHKeypairResponse.""" |
| 98 | + return SSHKeypairResponse( |
| 99 | + ssh_public_key=result.ssh_keypair.ssh_public_key, |
| 100 | + ssh_private_key=result.ssh_keypair.ssh_private_key, |
| 101 | + ) |
0 commit comments