| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import Any |
| 4 | |
| 5 | from .base import AuthenticationBase |
| 6 | |
| 7 | |
| 8 | class Database(AuthenticationBase): |
| 9 | """Database & Active Directory / LDAP Authentication. |
| 10 | |
| 11 | Args: |
| 12 | domain (str): Your auth0 domain (e.g: username.auth0.com) |
| 13 | """ |
| 14 | |
| 15 | def signup( |
| 16 | self, |
| 17 | email: str, |
| 18 | password: str, |
| 19 | connection: str, |
| 20 | username: str | None = None, |
| 21 | user_metadata: dict[str, Any] | None = None, |
| 22 | given_name: str | None = None, |
| 23 | family_name: str | None = None, |
| 24 | name: str | None = None, |
| 25 | nickname: str | None = None, |
| 26 | picture: str | None = None, |
| 27 | ) -> dict[str, Any]: |
| 28 | """Signup using email and password. |
| 29 | |
| 30 | Args: |
| 31 | email (str): The user's email address. |
| 32 | |
| 33 | password (str): The user's desired password. |
| 34 | |
| 35 | connection (str): The name of the database connection where this user should be created. |
| 36 | |
| 37 | username (str, optional): The user's username, if required by the database connection. |
| 38 | |
| 39 | user_metadata (dict, optional): Additional key-value information to store for the user. |
| 40 | Some limitations apply, see: https://auth0.com/docs/metadata#metadata-restrictions |
| 41 | |
| 42 | given_name (str, optional): The user's given name(s). |
| 43 | |
| 44 | family_name (str, optional): The user's family name(s). |
| 45 | |
| 46 | name (str, optional): The user's full name. |
| 47 | |
| 48 | nickname (str, optional): The user's nickname. |
| 49 | |
| 50 | picture (str, optional): A URI pointing to the user's picture. |
| 51 | |
| 52 | |
| 53 | See: https://auth0.com/docs/api/authentication#signup |
| 54 | """ |
| 55 | body: dict[str, Any] = { |
| 56 | "client_id": self.client_id, |
| 57 | "email": email, |
| 58 | "password": password, |
| 59 | "connection": connection, |
| 60 | } |
| 61 | if username: |
| 62 | body.update({"username": username}) |
| 63 | if user_metadata: |
| 64 | body.update({"user_metadata": user_metadata}) |
| 65 | if given_name: |
| 66 | body.update({"given_name": given_name}) |
| 67 | if family_name: |
| 68 | body.update({"family_name": family_name}) |
| 69 | if name: |
| 70 | body.update({"name": name}) |
| 71 | if nickname: |
| 72 | body.update({"nickname": nickname}) |
| 73 | if picture: |
| 74 | body.update({"picture": picture}) |
| 75 | |
| 76 | data: dict[str, Any] = self.post( |
| 77 | f"{self.protocol}://{self.domain}/dbconnections/signup", data=body |
| 78 | ) |
| 79 | return data |
| 80 | |
| 81 | def change_password( |
| 82 | self, |
| 83 | email: str, |
| 84 | connection: str, |
| 85 | password: str | None = None, |
| 86 | organization: str | None = None, |
| 87 | ) -> str: |
| 88 | """Asks to change a password for a given user. |
| 89 | |
| 90 | email (str): The user's email address. |
| 91 | |
| 92 | connection (str): The name of the database connection where this user should be created. |
| 93 | |
| 94 | organization (str, optional): The id of the Organization associated with the user. |
| 95 | """ |
| 96 | body = { |
| 97 | "client_id": self.client_id, |
| 98 | "email": email, |
| 99 | "connection": connection, |
| 100 | } |
| 101 | if organization: |
| 102 | body["organization"] = organization |
| 103 | |
| 104 | data: str = self.post( |
| 105 | f"{self.protocol}://{self.domain}/dbconnections/change_password", |
| 106 | data=body, |
| 107 | ) |
| 108 | return data |