File: guardian.py

package info (click to toggle)
auth0-python 4.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,280 kB
  • sloc: python: 8,933; makefile: 15; sh: 2
file content (173 lines) | stat: -rw-r--r-- 5,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from __future__ import annotations

from typing import Any

from ..rest import RestClient, RestClientOptions
from ..types import TimeoutType


class Guardian:
    """Auth0 guardian endpoints

    Args:
        domain (str): Your Auth0 domain, e.g: 'username.auth0.com'

        token (str): Management API v2 Token

        telemetry (bool, optional): Enable or disable Telemetry
            (defaults to True)

        timeout (float or tuple, optional): Change the requests
            connect and read timeout. Pass a tuple to specify
            both values separately or a float to set both to it.
            (defaults to 5.0 for both)

        protocol (str, optional): Protocol to use when making requests.
            (defaults to "https")

        rest_options (RestClientOptions): Pass an instance of
            RestClientOptions to configure additional RestClient
            options, such as rate-limit retries.
            (defaults to None)
    """

    def __init__(
        self,
        domain: str,
        token: str,
        telemetry: bool = True,
        timeout: TimeoutType = 5.0,
        protocol: str = "https",
        rest_options: RestClientOptions | None = None,
    ) -> None:
        self.domain = domain
        self.protocol = protocol
        self.client = RestClient(
            jwt=token, telemetry=telemetry, timeout=timeout, options=rest_options
        )

    def _url(self, id: str | None = None) -> str:
        url = f"{self.protocol}://{self.domain}/api/v2/guardian"
        if id is not None:
            return f"{url}/{id}"
        return url

    def all_factors(self) -> list[dict[str, Any]]:
        """Retrieves all factors. Useful to check factor enablement and
             trial status.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/get_factors
        """

        return self.client.get(self._url("factors"))

    def update_factor(self, name: str, body: dict[str, Any]) -> dict[str, Any]:
        """Update Guardian factor.
        Useful to enable / disable factor.

        Args:
            name (str): Either push-notification or sms.

            body (dict): Attributes to modify.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/put_factors_by_name
        """
        url = self._url(f"factors/{name}")
        return self.client.put(url, data=body)

    def update_templates(self, body: dict[str, Any]) -> dict[str, Any]:
        """Update enrollment and verification SMS templates.

        Useful to send custom messages on sms enrollment and verification.

        Args:
            body (dict): Attributes to modify.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/put_templates
        """

        return self.client.put(self._url("factors/sms/templates"), data=body)

    def get_templates(self) -> dict[str, Any]:
        """Get enrollment and verification templates.

        Retrieve both templates. Useful to check if a different template than
            default was set.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/get_templates
        """

        return self.client.get(self._url("factors/sms/templates"))

    def get_enrollment(self, id: str) -> dict[str, Any]:
        """Retrieves an enrollment.
        Useful to check its type and related metadata.

        Args:
           id (str): The id of the device account to update.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/get_enrollments_by_id
        """
        url = self._url(f"enrollments/{id}")
        return self.client.get(url)

    def delete_enrollment(self, id: str) -> Any:
        """Deletes an enrollment.

        Useful when you want to force re-enroll.

        Args:
           id (str): The id of the device account to update.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/delete_enrollments_by_id
        """
        url = self._url(f"enrollments/{id}")
        return self.client.delete(url)

    def create_enrollment_ticket(self, body: dict[str, Any]) -> dict[str, Any]:
        """Creates an enrollment ticket for user_id

        A useful way to send an email to a user, with a link that lead to
            start the enrollment process.

        Args:
            body (dict): Details of the user to send the ticket to.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/post_ticket
        """
        return self.client.post(self._url("enrollments/ticket"), data=body)

    def get_factor_providers(self, factor_name: str, name: str) -> dict[str, Any]:
        """Get Guardian SNS or SMS factor providers.

        Returns provider configuration.

        Args:
           factor_name (str): Either push-notification or sms.

           name (str): Name of the provider.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/get_sns
             https://auth0.com/docs/api/management/v2#!/Guardian/get_twilio
        """
        url = self._url(f"factors/{factor_name}/providers/{name}")
        return self.client.get(url)

    def update_factor_providers(
        self, factor_name: str, name: str, body: dict[str, Any]
    ) -> dict[str, Any]:
        """Get Guardian factor providers.

        Returns provider configuration.

        Args:
           factor_name (str): Either push-notification or sms.

           name (str): Name of the provider.

           body (dict): Details of the factor provider.

        See: https://auth0.com/docs/api/management/v2#!/Guardian/put_twilio
        """
        url = self._url(f"factors/{factor_name}/providers/{name}")
        return self.client.put(url, data=body)