Python library used to integrate with Descope
In order to initiate the AuthClient object you must specify the project ID given by Descope either by:
- Set the
DESCOPE_PROJECT_ID
environment variable. - Set the project_id argument when initialization the AuthClient object.
Install the Descope Python SDK using the following command. Descope Python SDK supports Python 3.6 and above
.. code-block:: python
pip install Descope-Auth
Use (copy-paste) the pre defined samples decorators based on your framework (Flask supported) or the api as describe below
.. code-block:: python
from descope import DeliveryMethod, User, AuthClient
class DeliveryMethod(Enum): WHATSAPP = 1 PHONE = 2 EMAIL = 3
User(username: str, name: str, phone: str, email: str)
AuthClient(PROJECT_ID, PUBLIC_KEY=None)
sign_up_otp(method: DeliveryMethod, identifier: str, user: User) Example: from descope import DeliveryMethod, User, AuthClient user = User("username", "name", "11111111111", "[email protected]") auth_client = AuthClient(PROJECT_ID) auth_client.sign_up_otp(DeliveryMethod.EMAIL, "[email protected]", user)
sign_in_otp(method: DeliveryMethod, identifier: str) Example: from descope import DeliveryMethod, AuthClient auth_client = AuthClient(PROJECT_ID) auth_client.sign_in_otp(DeliveryMethod.EMAIL, "[email protected]")
@descope_signin_otp_by_email
verify_code(method: DeliveryMethod, identifier: str, code: str) --Upon successful verification new session cookies will returned and should be set on the response Or one of the decorators: @descope_verify_code_by_email @descope_verify_code_by_phone @descope_verify_code_by_whatsapp
Example: from descope import DeliveryMethod, AuthClient auth_client = AuthClient(PROJECT_ID) auth_client.verify_code(DeliveryMethod.EMAIL, "1111") Or decorator
APP = Flask(name) @APP.route("/api/verify") @descope_verify_code_by_email def verify(): pass
validate_session_request(signed_token: str, signed_refresh_token: str) Or decorator @descope_validate_auth
Example: from descope import AuthClient auth_client = AuthClient(PROJECT_ID) new_valid_token = auth_client.validate_session_request('session_token', 'refresh_token')
logout(signed_token: str, signed_refresh_token: str) Example: from descope import AuthClient auth_client = AuthClient(PROJECT_ID) auth_client.logout('session_token', 'refresh_token')
.. code-block:: python
AuthException Example: from descope import DeliveryMethod, AuthClient, AuthException try: auth_client = AuthClient(PROJECT_ID) auth_client.sign_in_otp(DeliveryMethod.EMAIL, "[email protected]") except AuthException: #Handle exception
-
Clone repo locally
git clone github.com/descope/python-sdk
-
Install the requirements
pip3 install -r requirements-dev.txt
-
export your project id
export DESCOPE_PROJECT_ID=<insert here>
- Run the example application
python samples/web_sample_app.py
- Application runs on
http:https://localhost:9000
- Now you can perform GET requests to the server api like the following example:
Signup a new user by OTP via email, verify the OTP code and then access private (authenticated) api
.. code-block
/api/signup
Body:
{
"email": "[email protected]",
"user": {
"username": "dummy",
"name": "dummy",
"phone": "11111111111",
"email": "[email protected]"
}
}
/api/verify
Body:
{
"code": "111111",
"email": "[email protected]"
}
** Response will have the new generate session cookies
/api/private
Use the session cookies (otherwise you will get HTTP 401 - Unauthorized)
.. code-block:: python
python -m pytest tests/*