Flask Microsoft OAuth2
flask-ms-oauth2 is a Flask implementation of authentication using Microsoft OAuth2 Service. This extension helps to implement authentication solutions based on Microsoft OAuth2 Service. It contains helpful functions and properties to handle oauth2 and token based authentication flows.
Installation
pip install flask-ms-oauth2
Usage
from flask import Flask
from flask import redirect
from flask import url_for
from flask import session
from flask import jsonify
from flask_ms_oauth2 import MSOAuth2Manager
from flask_ms_oauth2 import login_handler
from flask_ms_oauth2 import logout_handler
from flask_ms_oauth2 import callback_handler
app = Flask(__name__)
app.secret_key = "my super secret key"
# Setup the flask-ms-oauth2 extention
app.config['CLIENT_ID'] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
app.config['CLIENT_SECRET'] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
app.config['TENANT_ID'] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
app.config["ERROR_REDIRECT_URI"] = "page500" # Optional
app.config["STATE"] = "mysupersecrethash" # Optional
app.config['REDIRECT_URI'] = "https://yourdomainhere/msoauth2/callback" # Specify this url in Callback URLs section of Appllication client settings within Microsoft OAuth2 Sevice. Post login application will redirect to this URL
app.config['SIGNOUT_URI'] = "https://yourdomainhere/login" # Specify this url in Sign out URLs section of Appllication client settings. Post logout application will redirect to this URL
msoauth2 = MSOAuth2Manager(app)
@app.route('/login', methods=['GET'])
def login():
print("Do the stuff before login to Microsoft Oauth2 Service")
response = redirect(url_for("msoauth2login"))
return response
@app.route('/logout', methods=['GET'])
def logout():
print("Do the stuff before logout from Microsoft Oauth2 Service")
response = redirect(url_for("msoauth2logout"))
return response
# Use @login_handler decorator on Microsoft OAuth2 login route
@app.route('/msoauth2/login', methods=['GET'])
@login_handler
def msoauth2login():
pass
@app.route('/home', methods=['GET'])
def home():
current_user = session["username"]
return jsonify(logged_in_as=current_user), 200
# Use @callback_handler decorator on Microsoft OAuth2 callback route
@app.route('/auth/callback', methods=['GET'])
@callback_handler
def callback():
for key in list(session.keys()):
print(f"Value for {key} is {session[key]}")
response = redirect(url_for("home"))
return response
# Use @logout_handler decorator on Microsoft OAuth2 logout route
@app.route('/msoauth2/logout', methods=['GET'])
@logout_handler
def msoauth2logout():
pass
@app.route('/page500', methods=['GET'])
def page500():
return jsonify(Error="Something went wrong"), 500
if __name__ == '__main__':
app.run(debug=True)
Development Setup
Using pipenv
pipenv install --dev
Using virtualenv
python3 -m venv venv
source venv/bin/activate
pip install .
Contributing
- Fork repo- https://github.com/shrivastava-v-ankit/flask-ms-oauth2.git
- Create your feature branch -
git checkout -b feature/name - Add Python test (pytest) and coverage report for new/changed feature.
- Commit your changes -
git commit -am "Added name" - Push to the branch -
git push origin feature/name - Create a new pull request
Metadata
Release files for flask-ms-oauth2 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask-ms-oauth2-1.0.0.tar.gz | 15.6 kB | Without provenance | Details for flask-ms-oauth2-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_ms_oauth2-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for flask_ms_oauth2-1.0.0-py3-none-any.whl |
Total release size: 25.1 kB
Release files / flask-ms-oauth2-1.0.0.tar.gz
| Download URL | flask-ms-oauth2-1.0.0.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
78e776c66330fe2d01f89621a40302d25fb3195cdb0e2ef93f13b336a9ec3358
|
|
BLAKE2b-256 checksum How to use checksums |
879f3ba916ece4d1338b3db659b66a1846c34c5020b7f1df573c775cb55b81b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.28.2 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.5
|
Release files / flask_ms_oauth2-1.0.0-py3-none-any.whl
| Download URL | flask_ms_oauth2-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e0a054a86488f2f2cf528396fb0a9f766b0a5a0f6e1fbe6915dd31221da60372
|
|
BLAKE2b-256 checksum How to use checksums |
92930a879fa9b0138caa1c5dcdb8548981572a3f3893e445c32e7562fd057fe6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.28.2 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.5
|
Security for flask-ms-oauth2 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.