|
| 1 | +from typing import Protocol, Union, TYPE_CHECKING |
| 2 | +from tableauserverclient.models.oidc_item import SiteOIDCConfiguration |
| 3 | +from tableauserverclient.server.endpoint import Endpoint |
| 4 | +from tableauserverclient.server.request_factory import RequestFactory |
| 5 | +from tableauserverclient.server.endpoint.endpoint import api |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from tableauserverclient.models.site_item import SiteAuthConfiguration |
| 9 | + from tableauserverclient.server.server import Server |
| 10 | + |
| 11 | + |
| 12 | +class IDPAttributes(Protocol): |
| 13 | + idp_configuration_id: str |
| 14 | + |
| 15 | + |
| 16 | +class IDPProperty(Protocol): |
| 17 | + @property |
| 18 | + def idp_configuration_id(self) -> str: ... |
| 19 | + |
| 20 | + |
| 21 | +HasIdpConfigurationID = Union[str, IDPAttributes] |
| 22 | + |
| 23 | + |
| 24 | +class OIDC(Endpoint): |
| 25 | + def __init__(self, server: "Server") -> None: |
| 26 | + self.parent_srv = server |
| 27 | + |
| 28 | + @property |
| 29 | + def baseurl(self) -> str: |
| 30 | + return f"{self.parent_srv.baseurl}/sites/{self.parent_srv.site_id}/site-oidc-configuration" |
| 31 | + |
| 32 | + @api(version="3.24") |
| 33 | + def get(self) -> list["SiteAuthConfiguration"]: |
| 34 | + """ |
| 35 | + Get all OpenID Connect (OIDC) configurations for the currently |
| 36 | + authenticated Tableau Cloud site. To get all of the configuration |
| 37 | + details, use the get_by_id method. |
| 38 | +
|
| 39 | + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_identity_pools.htm#AuthnService_ListAuthConfigurations |
| 40 | +
|
| 41 | + Returns |
| 42 | + ------- |
| 43 | + list[SiteAuthConfiguration] |
| 44 | + """ |
| 45 | + return self.parent_srv.sites.list_auth_configurations() |
| 46 | + |
| 47 | + @api(version="3.24") |
| 48 | + def get_by_id(self, id: Union[str, HasIdpConfigurationID]) -> SiteOIDCConfiguration: |
| 49 | + """ |
| 50 | + Get details about a specific OpenID Connect (OIDC) configuration on the |
| 51 | + current Tableau Cloud site. Only retrieves configurations for the |
| 52 | + currently authenticated site. |
| 53 | +
|
| 54 | + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_openid_connect.htm#get_openid_connect_configuration |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + id : Union[str, HasID] |
| 59 | + The ID of the OIDC configuration to retrieve. Can be either the |
| 60 | + ID string or an object with an id attribute. |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + SiteOIDCConfiguration |
| 65 | + The OIDC configuration for the specified site. |
| 66 | + """ |
| 67 | + target = getattr(id, "idp_configuration_id", id) |
| 68 | + url = f"{self.baseurl}/{target}" |
| 69 | + response = self.get_request(url) |
| 70 | + return SiteOIDCConfiguration.from_response(response.content, self.parent_srv.namespace) |
| 71 | + |
| 72 | + @api(version="3.22") |
| 73 | + def create(self, config_item: SiteOIDCConfiguration) -> SiteOIDCConfiguration: |
| 74 | + """ |
| 75 | + Create the OpenID Connect (OIDC) configuration for the currently |
| 76 | + authenticated Tableau Cloud site. The config_item must have the |
| 77 | + following attributes set, others are optional: |
| 78 | +
|
| 79 | + idp_configuration_name |
| 80 | + client_id |
| 81 | + client_secret |
| 82 | + authorization_endpoint |
| 83 | + token_endpoint |
| 84 | + userinfo_endpoint |
| 85 | + enabled |
| 86 | + jwks_uri |
| 87 | +
|
| 88 | + The secret in the returned config will be masked. |
| 89 | +
|
| 90 | + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_openid_connect.htm#create_openid_connect_configuration |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + config : SiteOIDCConfiguration |
| 95 | + The OIDC configuration to create. |
| 96 | +
|
| 97 | + Returns |
| 98 | + ------- |
| 99 | + SiteOIDCConfiguration |
| 100 | + The created OIDC configuration. |
| 101 | + """ |
| 102 | + url = self.baseurl |
| 103 | + create_req = RequestFactory.OIDC.create_req(config_item) |
| 104 | + response = self.put_request(url, create_req) |
| 105 | + return SiteOIDCConfiguration.from_response(response.content, self.parent_srv.namespace) |
| 106 | + |
| 107 | + @api(version="3.24") |
| 108 | + def delete_configuration(self, config: Union[str, HasIdpConfigurationID]) -> None: |
| 109 | + """ |
| 110 | + Delete the OpenID Connect (OIDC) configuration for the currently |
| 111 | + authenticated Tableau Cloud site. The config parameter can be either |
| 112 | + the ID of the configuration or the configuration object itself. |
| 113 | +
|
| 114 | + **Important**: Before removing the OIDC configuration, make sure that |
| 115 | + users who are set to authenticate with OIDC are set to use a different |
| 116 | + authentication type. Users who are not set with a different |
| 117 | + authentication type before removing the OIDC configuration will not be |
| 118 | + able to sign in to Tableau Cloud. |
| 119 | +
|
| 120 | +
|
| 121 | + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_openid_connect.htm#remove_openid_connect_configuration |
| 122 | +
|
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + config : Union[str, HasID] |
| 126 | + The OIDC configuration to delete. Can be either the ID of the |
| 127 | + configuration or the configuration object itself. |
| 128 | + """ |
| 129 | + |
| 130 | + target = getattr(config, "idp_configuration_id", config) |
| 131 | + |
| 132 | + url = f"{self.parent_srv.baseurl}/sites/{self.parent_srv.site_id}/disable-site-oidc-configuration?idpConfigurationId={target}" |
| 133 | + _ = self.put_request(url) |
| 134 | + return None |
| 135 | + |
| 136 | + @api(version="3.22") |
| 137 | + def update(self, config: SiteOIDCConfiguration) -> SiteOIDCConfiguration: |
| 138 | + """ |
| 139 | + Update the Tableau Cloud site's OpenID Connect (OIDC) configuration. The |
| 140 | + secret in the returned config will be masked. |
| 141 | +
|
| 142 | + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_openid_connect.htm#update_openid_connect_configuration |
| 143 | +
|
| 144 | + Parameters |
| 145 | + ---------- |
| 146 | + config : SiteOIDCConfiguration |
| 147 | + The OIDC configuration to update. Must have the id attribute set. |
| 148 | +
|
| 149 | + Returns |
| 150 | + ------- |
| 151 | + SiteOIDCConfiguration |
| 152 | + The updated OIDC configuration. |
| 153 | + """ |
| 154 | + url = f"{self.baseurl}/{config.idp_configuration_id}" |
| 155 | + update_req = RequestFactory.OIDC.update_req(config) |
| 156 | + response = self.put_request(url, update_req) |
| 157 | + return SiteOIDCConfiguration.from_response(response.content, self.parent_srv.namespace) |
0 commit comments