|
1 | | -# passwordless-python |
| 1 | +# Passwordless Python SDK |
| 2 | + |
| 3 | +The official [Bitwarden Passwordless.dev](https://passwordless.dev/) Python library, for Python 3+. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install with `python -m pip install passwordless`. |
| 8 | + |
| 9 | +### Dependencies |
| 10 | + |
| 11 | +- [Requests][requests] for HTTP API |
| 12 | +- [marshmallow][marshmallow] for JSON (de)serialization |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +Follow the [Get started guide][api-docs]. |
| 17 | + |
| 18 | +### Create `PasswordlessClient` instance: |
| 19 | + |
| 20 | +```python |
| 21 | +from passwordless import ( |
| 22 | + PasswordlessClient, |
| 23 | + PasswordlessClientBuilder, |
| 24 | + PasswordlessOptions, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class PasswordlessPythonSdkExample: |
| 29 | + client: PasswordlessClient |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + options = PasswordlessOptions("your_api_secret") |
| 33 | + |
| 34 | + self.client = PasswordlessClientBuilder(options).build() |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +### Register a passkey |
| 39 | + |
| 40 | +```python |
| 41 | +import uuid |
| 42 | +from passwordless import PasswordlessClient, RegisterToken, RegisteredToken |
| 43 | + |
| 44 | + |
| 45 | +class PasswordlessPythonSdkExample: |
| 46 | + client: PasswordlessClient |
| 47 | + |
| 48 | + def get_register_token(self, alias: str) -> str: |
| 49 | + # Get existing userid from session or create a new user. |
| 50 | + user_id = str(uuid.uuid4()) |
| 51 | + |
| 52 | + # Options to give the Api |
| 53 | + register_token = RegisterToken( |
| 54 | + user_id=user_id, # your user id |
| 55 | + username=alias, # e.g. user email, is shown in browser ui |
| 56 | + aliases=[alias] # Optional: Link this userid to an alias (e.g. email) |
| 57 | + ) |
| 58 | + |
| 59 | + response: RegisteredToken = self.client.register_token(register_token) |
| 60 | + |
| 61 | + # return this token |
| 62 | + return response.token |
| 63 | +``` |
| 64 | + |
| 65 | +### Verify user |
| 66 | + |
| 67 | +```python |
| 68 | +from passwordless import PasswordlessClient, VerifySignIn, VerifiedUser |
| 69 | + |
| 70 | + |
| 71 | +class PasswordlessPythonSdkExample: |
| 72 | + client: PasswordlessClient |
| 73 | + |
| 74 | + def verify_sign_in_token(self, token: str) -> VerifiedUser: |
| 75 | + verify_sign_in = VerifySignIn(token) |
| 76 | + |
| 77 | + # Sign the user in, set a cookie, etc, |
| 78 | + return self.client.sign_in(verify_sign_in) |
| 79 | +``` |
| 80 | + |
| 81 | +### Customization |
| 82 | + |
| 83 | +Customize `PasswordlessOptions` by providing `api_secret` with your Application's Api Secret. |
| 84 | +You can also change the `api_url` if you prefer to self-host. |
| 85 | + |
| 86 | +Customize `PasswordlessClientBuilder` by providing `session` [requests Session][requests] instance. |
| 87 | + |
| 88 | +### Examples |
| 89 | + |
| 90 | +See [Passwordless Python Example](examples/flask) for Flash Web application. |
| 91 | + |
| 92 | +## Documentation |
| 93 | + |
| 94 | +For a comprehensive list of examples, check out the [API documentation][api-docs]. |
| 95 | + |
| 96 | +## Contributing |
| 97 | + |
| 98 | +This library is compatible with Python 3 and requires minimum Python 3.8 installed. |
| 99 | +Install [Poetry][poetry] if not already installed. |
| 100 | + |
| 101 | +Activate shell: `poetry shell` |
| 102 | + |
| 103 | +Install dependencies: `poetry install --with dev,test` |
| 104 | + |
| 105 | +Build: `poetry build` |
| 106 | + |
| 107 | +[api-docs]:https://docs.passwordless.dev/guide/get-started.html |
| 108 | + |
| 109 | +[poetry]:https://python-poetry.org/docs/#installation |
| 110 | + |
| 111 | +[requests]:https://requests.readthedocs.io/en/latest/ |
| 112 | + |
| 113 | +[marshmallow]:https://marshmallow.readthedocs.io/en/stable/ |
0 commit comments