Python requests session for microsoft with support for oauth2, adal and msal
Project description
Python Requests for OAuth2 with ADAL / MSAL with auto-renew access token
About
This project provides a simple Requests compatible session that you can use to authenticate with Microsoft using he following:
- Azure Active Directory Authentication Library (ADAL)
- Microsoft Authentication Library (MSAL). (not finished, use ADAL instead)
Session renews access token automatically (by default token expires each hour).
The package is available on PyPi.
How to use
Install the package
python3 -m pip install --upgrade requests_ms_auth
Use an example
NOTE: These examples are made for easy access to Equinor specific systems.
NOTE: Before using the example you should first fill the credentials inside the example file.
Description | Example code |
---|---|
Python code to access Metadata API using session directly | metadata_api_example.py |
The session should automatically fetch a token on startup and renew it when the token expires.
Session will also verify itself in the constructor using the optional verification_url
if specified, allowing you to terminate early on failure.
Configuration
Session configuration details might be found in the docstring here
Implementation details
-
The library uses
pip-compile
withrequirements.in
to manage and pin requirements. Requirements for test are maintained in a separatetest_rquirements.in
. -
The library uses a Makefile to manage building, packaging and uploading of versions, as well as many short-cuts for running tests, compiling requirements and more.
-
The library is built and tested by github actions.
-
The package is prepared and uploaded to PyPi by github actions.
-
The library defaults to ADAL and can be told to use MSAL as an option.
-
To supply OAuth2 compatability the library depends on
Tests
Config env variables
Export following ENV variables (with previously replaced values):
- from shell:
export INTEGRATION_TENANT=tenant
export INTEGRATION_CLIENT_ID=id
export INTEGRATION_CLIENT_SECRET=secret
export INTEGRATION_RESOURCE=resourse
export INTEGRATION_AUTHORITY_HOST_URL=authority
export INTEGRATION_LIVE_VERIFICATION_URL=verification
export INTEGRATION_LIVE_VERIFICATION_ELEMENT=element
- OR add env variables to Pycharm or other IDE (with previously added values):
INTEGRATION_TENANT=tenant;INTEGRATION_CLIENT_ID=id;INTEGRATION_CLIENT_SECRET=secret;INTEGRATION_RESOURCE=resourse;INTEGRATION_AUTHORITY_HOST_URL=authority;INTEGRATION_LIVE_VERIFICATION_URL=verification;INTEGRATION_LIVE_VERIFICATION_ELEMENT=element;
- OR use
.env
file:
INTEGRATION_TENANT=tenant
INTEGRATION_CLIENT_ID=id
INTEGRATION_CLIENT_SECRET=secret
INTEGRATION_RESOURCE=resourse
INTEGRATION_AUTHORITY_HOST_URL=authority
INTEGRATION_LIVE_VERIFICATION_URL=verification
INTEGRATION_LIVE_VERIFICATION_ELEMENT=element
Run tests
make test
License
Please see LICENSE file for details. requests_ms_auth is licensed under GNU AFFERO GENERAL PUBLIC LICENSE and has G-Faps.
History
This project grew from the needs of the latigo project.
TODOs
- check if MSAL method works;
- cover crucial parts with tests;
- clear Makefile;
- handle TODOs in the code;
- enable mypy for examples dir.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file requests_ms_auth-0.3.2.tar.gz
.
File metadata
- Download URL: requests_ms_auth-0.3.2.tar.gz
- Upload date:
- Size: 27.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbd5493a51268b756431a1854f9b3cb941636125ea0516d120d0fe1adeaa642c |
|
MD5 | 51fe6b19e7d7a4af34a33321b628c16d |
|
BLAKE2b-256 | ed5eddcd6b39d5cf46f3b4b64712cd0ad0ace1a29cda222709e8c4b52af02cfc |