|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from auth0.rest import RestClient |
| 4 | +from auth0.rest import RestClientOptions |
| 5 | +from auth0.types import TimeoutType |
| 6 | + |
| 7 | + |
| 8 | +class Sessions: |
| 9 | + """Auth0 users endpoints |
| 10 | +
|
| 11 | + Args: |
| 12 | + domain (str): Your Auth0 domain, e.g: 'username.auth0.com' |
| 13 | +
|
| 14 | + token (str): Management API v2 Token |
| 15 | +
|
| 16 | + telemetry (bool, optional): Enable or disable Telemetry |
| 17 | + (defaults to True) |
| 18 | +
|
| 19 | + timeout (float or tuple, optional): Change the requests |
| 20 | + connect and read timeout. Pass a tuple to specify |
| 21 | + both values separately or a float to set both to it. |
| 22 | + (defaults to 5.0 for both) |
| 23 | +
|
| 24 | + protocol (str, optional): Protocol to use when making requests. |
| 25 | + (defaults to "https") |
| 26 | +
|
| 27 | + rest_options (RestClientOptions): Pass an instance of |
| 28 | + RestClientOptions to configure additional RestClient |
| 29 | + options, such as rate-limit retries. |
| 30 | + (defaults to None) |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__( |
| 34 | + self, |
| 35 | + domain: str, |
| 36 | + token: str, |
| 37 | + telemetry: bool = True, |
| 38 | + timeout: TimeoutType = 5.0, |
| 39 | + protocol: str = "https", |
| 40 | + rest_options: RestClientOptions | None = None, |
| 41 | + ) -> None: |
| 42 | + self.domain = domain |
| 43 | + self.protocol = protocol |
| 44 | + self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout, options=rest_options) |
| 45 | + |
| 46 | + def _url(self, id: str | None = None) -> str: |
| 47 | + url = f"{self.protocol}://{self.domain}/api/v2/sessions" |
| 48 | + if id is not None: |
| 49 | + return f"{url}/{id}" |
| 50 | + return url |
| 51 | + |
| 52 | + def get(self, id: str) -> dict[str, Any]: |
| 53 | + """Get a session. |
| 54 | +
|
| 55 | + Args: |
| 56 | + id (str): The id of the session to retrieve. |
| 57 | +
|
| 58 | + See: https://auth0.com/docs/api/management/v2#!/Sessions/get-session |
| 59 | + """ |
| 60 | + |
| 61 | + return self.client.get(self._url(id)) |
| 62 | + |
| 63 | + def delete(self, id: str) -> None: |
| 64 | + """Delete a session. |
| 65 | +
|
| 66 | + Args: |
| 67 | + id (str): The id of the session to delete. |
| 68 | +
|
| 69 | + See: https://auth0.com/docs/api/management/v2#!/Sessions/delete-session |
| 70 | + """ |
| 71 | + |
| 72 | + return self.client.delete(self._url(id)) |
| 73 | + |
| 74 | + def revoke(self, id: str) -> None: |
| 75 | + """Revokes a session by ID and all associated refresh tokens.. |
| 76 | +
|
| 77 | + Args: |
| 78 | + id (str): The id of the session to revoke. |
| 79 | +
|
| 80 | + See: https://auth0.com/docs/api/management/v2#!/Sessions/revoke-session |
| 81 | + """ |
| 82 | + |
| 83 | + url = self._url(f"{id}/sessions") |
| 84 | + return self.client.post(url) |
0 commit comments