|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from descope.auth import Auth |
| 4 | +from descope.management.common import MgmtV1 |
| 5 | + |
| 6 | + |
| 7 | +class RoleMapping: |
| 8 | + def __init__(self, groups: List[str], role_name: str): |
| 9 | + self.groups = groups |
| 10 | + self.role_name = role_name |
| 11 | + |
| 12 | + |
| 13 | +class SSOSettings: |
| 14 | + _auth: Auth |
| 15 | + |
| 16 | + def __init__(self, auth: Auth): |
| 17 | + self._auth = auth |
| 18 | + |
| 19 | + def configure( |
| 20 | + self, |
| 21 | + mgmt_key: str, |
| 22 | + tenant_id: str, |
| 23 | + idp_url: str, |
| 24 | + entity_id: str, |
| 25 | + idp_cert: str, |
| 26 | + redirect_url: str = None, |
| 27 | + ) -> None: |
| 28 | + """ |
| 29 | + Configure SSO setting for a tenant manually. Alternatively, `configure_via_metadata` can be used instead. |
| 30 | +
|
| 31 | + Args: |
| 32 | + mgmt_key (str): A management key generated in the Descope console. All management functions require it. |
| 33 | + tenant_id (str): The tenant ID to be configured |
| 34 | + idp_url (str): The URL for the identity provider. |
| 35 | + entity_id (str): The entity ID (in the IDP). |
| 36 | + idp_cert (str): The certificate provided by the IDP. |
| 37 | + redirect_url (str): An Optional Redirect URL after successful authentication. |
| 38 | +
|
| 39 | + Raise: |
| 40 | + AuthException: raised if configuration operation fails |
| 41 | + """ |
| 42 | + self._auth.do_post( |
| 43 | + MgmtV1.ssoConfigurePath, |
| 44 | + SSOSettings._compose_configure_body( |
| 45 | + tenant_id, idp_url, entity_id, idp_cert, redirect_url |
| 46 | + ), |
| 47 | + pswd=mgmt_key, |
| 48 | + ) |
| 49 | + |
| 50 | + def configure_via_metadata( |
| 51 | + self, |
| 52 | + mgmt_key: str, |
| 53 | + tenant_id: str, |
| 54 | + idp_metadata_url: str, |
| 55 | + ): |
| 56 | + """ |
| 57 | + Configure SSO setting for am IDP metadata URL. Alternatively, `configure` can be used instead. |
| 58 | +
|
| 59 | + Args: |
| 60 | + mgmt_key (str): A management key generated in the Descope console. All management functions require it. |
| 61 | + tenant_id (str): The tenant ID to be configured |
| 62 | + enabled (bool): Is SSO enabled |
| 63 | + idp_metadata_url (str): The URL to fetch SSO settings from. |
| 64 | +
|
| 65 | + Raise: |
| 66 | + AuthException: raised if configuration operation fails |
| 67 | + """ |
| 68 | + self._auth.do_post( |
| 69 | + MgmtV1.ssoMetadataPath, |
| 70 | + SSOSettings._compose_metadata_body(tenant_id, idp_metadata_url), |
| 71 | + pswd=mgmt_key, |
| 72 | + ) |
| 73 | + |
| 74 | + def map_roles( |
| 75 | + self, |
| 76 | + mgmt_key: str, |
| 77 | + tenant_id: str, |
| 78 | + role_mappings: List[RoleMapping], |
| 79 | + ): |
| 80 | + """ |
| 81 | + Configure SSO role mapping from the IDP groups to the Descope roles. |
| 82 | +
|
| 83 | + Args: |
| 84 | + mgmt_key (str): A management key generated in the Descope console. All management functions require it. |
| 85 | + tenant_id (str): The tenant ID to be configured |
| 86 | + role_mappings (List[RoleMapping]): A mapping between IDP groups and Descope roles. |
| 87 | +
|
| 88 | + Raise: |
| 89 | + AuthException: raised if configuration operation fails |
| 90 | + """ |
| 91 | + self._auth.do_post( |
| 92 | + MgmtV1.ssoRoleMappingPath, |
| 93 | + SSOSettings._compose_role_mapping_body(tenant_id, role_mappings), |
| 94 | + pswd=mgmt_key, |
| 95 | + ) |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def _compose_configure_body( |
| 99 | + tenant_id: str, |
| 100 | + idp_url: str, |
| 101 | + entity_id: str, |
| 102 | + idp_cert: str, |
| 103 | + redirect_url: str = None, |
| 104 | + ) -> dict: |
| 105 | + return { |
| 106 | + "tenantId": tenant_id, |
| 107 | + "idpURL": idp_url, |
| 108 | + "entityId": entity_id, |
| 109 | + "idpCert": idp_cert, |
| 110 | + "redirectURL": redirect_url, |
| 111 | + } |
| 112 | + |
| 113 | + @staticmethod |
| 114 | + def _compose_metadata_body( |
| 115 | + tenant_id: str, |
| 116 | + idp_metadata_url: str, |
| 117 | + ) -> dict: |
| 118 | + return { |
| 119 | + "tenantId": tenant_id, |
| 120 | + "idpMetadataURL": idp_metadata_url, |
| 121 | + } |
| 122 | + |
| 123 | + @staticmethod |
| 124 | + def _compose_role_mapping_body( |
| 125 | + tenant_id: str, |
| 126 | + role_mapping: List[RoleMapping], |
| 127 | + ) -> dict: |
| 128 | + return { |
| 129 | + "tenantId": tenant_id, |
| 130 | + "roleMapping": SSOSettings._role_mapping_to_dict(role_mapping), |
| 131 | + } |
| 132 | + |
| 133 | + @staticmethod |
| 134 | + def _role_mapping_to_dict(role_mapping: List[RoleMapping]) -> list: |
| 135 | + role_mapping_list = [] |
| 136 | + for mapping in role_mapping: |
| 137 | + role_mapping_list.append( |
| 138 | + { |
| 139 | + "groups": mapping.groups, |
| 140 | + "roleName": mapping.role_name, |
| 141 | + } |
| 142 | + ) |
| 143 | + return role_mapping_list |
0 commit comments