Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7699205
initial python sdk code
May 19, 2022
b83a0da
fix github isort action
May 19, 2022
902e9fe
fix github isort action wip2
guyp-descope May 19, 2022
043ffbb
fix github isort action wip3
guyp-descope May 19, 2022
78e5a94
fix github unittest action wip4
guyp-descope May 19, 2022
03941d0
fix github unittest action wip5
guyp-descope May 19, 2022
9afaf77
fix github unittest action wip6
guyp-descope May 19, 2022
757c24d
fix github unittest action wip7
guyp-descope May 19, 2022
2b9f5b3
add github python code coverage action wip8
guyp-descope May 19, 2022
252ca25
add github python code coverage action wip9
guyp-descope May 19, 2022
30b1c74
add github python code coverage action wip10
guyp-descope May 19, 2022
3d07f9a
add github python code coverage action wip11
guyp-descope May 19, 2022
1a50cee
fixes some bugs and added functionality of fetching public key
guyp-descope May 19, 2022
18efbc8
Add unittest for better coverage
guyp-descope May 22, 2022
c9811bc
remove the pyproject.toml file (not in used)
guyp-descope May 22, 2022
bdf01c4
add gitleaks Action
guyp-descope May 22, 2022
3f45417
add gitleaks to workflow, some PR fixes
guyp-descope May 22, 2022
5946440
add github checkout action
guyp-descope May 22, 2022
2d22181
fix github action
guyp-descope May 22, 2022
0ce81e1
fix github action
guyp-descope May 22, 2022
d65b343
fix PR issues
guyp-descope May 22, 2022
31ebac5
add support for multiple public keys, add mutex for thread safe
guyp-descope May 22, 2022
be52ea7
replace email regex with python email validator package
guyp-descope May 23, 2022
d377caf
add new package to requirements file
guyp-descope May 23, 2022
6914192
change badge namedlogo
guyp-descope May 23, 2022
2ae8bf5
change coverage badge
guyp-descope May 24, 2022
8089b04
change coverage badge
guyp-descope May 24, 2022
11f02a4
revert to the latest coverage badge
guyp-descope May 24, 2022
6da0ad6
1. Add support for refresh token 2. Fix decorator (to support pre-pos…
guyp-descope May 24, 2022
d6d3409
fix UT
guyp-descope May 24, 2022
cf65450
add license check for pre-commit and part of the ci workflow
guyp-descope May 24, 2022
08e56e7
fix license checks
guyp-descope May 24, 2022
344022f
fix license checks 2
guyp-descope May 24, 2022
1e8a68d
seperate the coverage steps to run on a different workflow that run o…
guyp-descope May 25, 2022
63a161e
1. fixed few bugs 2. added flask decorator functions example 3. imp…
guyp-descope May 26, 2022
b2b1474
fix pr comments
guyp-descope May 26, 2022
4ae2458
fix some more pr comments
guyp-descope May 26, 2022
e32b425
1. change the api so the claims (jwt payload) will be available for t…
guyp-descope May 30, 2022
cfb24c8
set alg as const
guyp-descope May 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
per-file-ignores = __init__.py:F401
ignore = E501
10 changes: 10 additions & 0 deletions .github/actions/gitleaks/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Leaks checks
description: 'gitleaks checks'
runs:
using: "composite"
steps:
- name: gitleaks checks
run: |
chmod +x ./scripts/gitleaks/gitleaks.sh
./scripts/gitleaks/gitleaks.sh
shell: bash
53 changes: 53 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Python package

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt

- name: Lint with flake8
run: |
flake8 . --count --show-source --statistics

- name: Lint with black
uses: psf/black@stable
with:
options: "--check --verbose"

- name: Run isort
uses: isort/isort-action@master
with:
configuration: --profile black

- name: Check leaks
uses: ./.github/actions/gitleaks

- name: License Checker
uses: andersy005/gh-action-py-liccheck@main
with:
strategy-ini-file: ./liccheck.ini
level: paranoid
requirements-txt-file: ./requirements-dev.txt
no-deps: false
liccheck-version: 0.6.4

- name: Tests
run: |
python -m pytest tests/*
58 changes: 58 additions & 0 deletions .github/workflows/ci_pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Python package on pull request

on: [pull_request]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Build coverage file
run: |
python -m pytest --junitxml=/tmp/pytest.xml --cov-report=term-missing:skip-covered --cov=descope tests/ | tee /tmp/pytest-coverage.txt
- name: Pytest coverage comment
id: coverageComment
uses: MishaKav/pytest-coverage-comment@main
with:
pytest-coverage-path: /tmp/pytest-coverage.txt
junitxml-path: /tmp/pytest.xml
create-new-comment: false

- name: Coverage
run: |
echo "Coverage Percantage - ${{ steps.coverageComment.outputs.coverage }}"
echo "Coverage Color - ${{ steps.coverageComment.outputs.color }}"
echo "Coverage Html - ${{ steps.coverageComment.outputs.coverageHtml }}"
echo "Coverage Warnings - ${{ steps.coverageComment.outputs.warnings }}"
echo "Coverage Errors - ${{ steps.coverageComment.outputs.errors }}"
echo "Coverage Failures - ${{ steps.coverageComment.outputs.failures }}"
echo "Coverage Skipped - ${{ steps.coverageComment.outputs.skipped }}"
echo "Coverage Tests - ${{ steps.coverageComment.outputs.tests }}"
echo "Coverage Time - ${{ steps.coverageComment.outputs.time }}"
echo "Not Success Test Info - ${{ steps.coverageComment.outputs.notSuccessTestInfo }}"
- name: Create the Badge
uses: schneegans/dynamic-badges-action@v1.0.0
with:
auth: ${{ secrets.CI_READ_COMMON }}
gistID: 277ec23e4e70728824362a0d24fbd0f9
filename: pytest-coverage-comment.json
label: Coverage Report
message: ${{ steps.coverageComment.outputs.coverage }}
color: ${{ steps.coverageComment.outputs.color }}
namedLogo: pytest
35 changes: 35 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
exclude: 'docs/'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
- repo: https://github.com/dhatim/python-license-check
rev: master
hooks:
- id: liccheck
language: system
args: ["-r./requirements-dev.txt", "-lparanoid"]
149 changes: 148 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,149 @@
# python-sdk
# Python SDK
Python library used to integrate with Descope

### Prerequisites

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.


### Installation
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


## Usage
Use (copy-paste) the pre defined samples decorators based on your framework (Flask supported) or the api as describe below

### API
.. 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", "dummy@dummy.com")
auth_client = AuthClient(PROJECT_ID)
auth_client.sign_up_otp(DeliveryMethod.EMAIL, "dummy@dummy.com", 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, "dummy@dummy.com")

@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')

#### Exception
.. code-block:: python

AuthException
Example:
from descope import DeliveryMethod, AuthClient, AuthException
try:
auth_client = AuthClient(PROJECT_ID)
auth_client.sign_in_otp(DeliveryMethod.EMAIL, "dummy@dummy.com")
except AuthException:
#Handle exception

#
### Run The Example

1. Clone repo locally `git clone github.com/descope/python-sdk`
2. Install the requirements `pip3 install -r requirements-dev.txt`

3. export your project id

```
export DESCOPE_PROJECT_ID=<insert here>
```

5. Run the example application `python samples/web_sample_app.py`
6. Application runs on `http://localhost:9000`
7. 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": "dummy@dummy.com",
"user": {
"username": "dummy",
"name": "dummy",
"phone": "11111111111",
"email": "dummy@dummy.com"
}
}

/api/verify
Body:
{
"code": "111111",
"email": "dummy@dummy.com"
}

** Response will have the new generate session cookies

/api/private
Use the session cookies (otherwise you will get HTTP 401 - Unauthorized)

### Unit Testing
.. code-block:: python

python -m pytest tests/*
8 changes: 8 additions & 0 deletions descope/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from descope.auth import AuthClient
from descope.common import (
REFRESH_SESSION_COOKIE_NAME,
SESSION_COOKIE_NAME,
DeliveryMethod,
User,
)
from descope.exceptions import AuthException
Loading