-
Notifications
You must be signed in to change notification settings - Fork 8
Initial Python sdk code #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
7699205
initial python sdk code
b83a0da
fix github isort action
902e9fe
fix github isort action wip2
guyp-descope 043ffbb
fix github isort action wip3
guyp-descope 78e5a94
fix github unittest action wip4
guyp-descope 03941d0
fix github unittest action wip5
guyp-descope 9afaf77
fix github unittest action wip6
guyp-descope 757c24d
fix github unittest action wip7
guyp-descope 2b9f5b3
add github python code coverage action wip8
guyp-descope 252ca25
add github python code coverage action wip9
guyp-descope 30b1c74
add github python code coverage action wip10
guyp-descope 3d07f9a
add github python code coverage action wip11
guyp-descope 1a50cee
fixes some bugs and added functionality of fetching public key
guyp-descope 18efbc8
Add unittest for better coverage
guyp-descope c9811bc
remove the pyproject.toml file (not in used)
guyp-descope bdf01c4
add gitleaks Action
guyp-descope 3f45417
add gitleaks to workflow, some PR fixes
guyp-descope 5946440
add github checkout action
guyp-descope 2d22181
fix github action
guyp-descope 0ce81e1
fix github action
guyp-descope d65b343
fix PR issues
guyp-descope 31ebac5
add support for multiple public keys, add mutex for thread safe
guyp-descope be52ea7
replace email regex with python email validator package
guyp-descope d377caf
add new package to requirements file
guyp-descope 6914192
change badge namedlogo
guyp-descope 2ae8bf5
change coverage badge
guyp-descope 8089b04
change coverage badge
guyp-descope 11f02a4
revert to the latest coverage badge
guyp-descope 6da0ad6
1. Add support for refresh token 2. Fix decorator (to support pre-pos…
guyp-descope d6d3409
fix UT
guyp-descope cf65450
add license check for pre-commit and part of the ci workflow
guyp-descope 08e56e7
fix license checks
guyp-descope 344022f
fix license checks 2
guyp-descope 1e8a68d
seperate the coverage steps to run on a different workflow that run o…
guyp-descope 63a161e
1. fixed few bugs 2. added flask decorator functions example 3. imp…
guyp-descope b2b1474
fix pr comments
guyp-descope 4ae2458
fix some more pr comments
guyp-descope e32b425
1. change the api so the claims (jwt payload) will be available for t…
guyp-descope cfb24c8
set alg as const
guyp-descope File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[flake8] | ||
per-file-ignores = __init__.py:F401 | ||
ignore = E501 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
guyp-descope marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Prerequisites | ||
|
||
In order to initiate the AuthClient object you must specify the project ID given by Descope either by: | ||
guyp-descope marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- 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 | ||
guyp-descope marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.. 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/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.