# Flask Microsoft OAuth2
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![PyPI](https://img.shields.io/pypi/v/flask-ms-oauth2.svg)](https://pypi.org/project/flask-ms-oauth2)
[![CircleCI](https://circleci.com/gh/shrivastava-v-ankit/flask-ms-oauth2.svg?style=svg)](https://circleci.com/gh/shrivastava-v-ankit/flask-ms-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.
</br>
## Installation
```bash
pip install flask-ms-oauth2
```
### Usage
```python
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
```bash
pipenv install --dev
```
Using virtualenv
```bash
python3 -m venv venv
source venv/bin/activate
pip install .
```
### Contributing
1. Fork repo- https://github.com/shrivastava-v-ankit/flask-ms-oauth2.git
2. Create your feature branch - `git checkout -b feature/name`
3. Add Python test (pytest) and coverage report for new/changed feature.
4. Commit your changes - `git commit -am "Added name"`
5. Push to the branch - `git push origin feature/name`
6. Create a new pull request
Raw data
{
"_id": null,
"home_page": "https://github.com/shrivastava-v-ankit/flask-ms-oauth2",
"name": "flask-ms-oauth2",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "flask,microsoft,json web token,oauth2,authentication",
"author": "Ankit Shrivastava",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/87/9f/3ba916ece4d1338b3db659b66a1846c34c5020b7f1df573c775cb55b81b1/flask-ms-oauth2-1.0.0.tar.gz",
"platform": "any",
"description": "# Flask Microsoft OAuth2\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![PyPI](https://img.shields.io/pypi/v/flask-ms-oauth2.svg)](https://pypi.org/project/flask-ms-oauth2)\n[![CircleCI](https://circleci.com/gh/shrivastava-v-ankit/flask-ms-oauth2.svg?style=svg)](https://circleci.com/gh/shrivastava-v-ankit/flask-ms-oauth2)\n\n\nflask-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.\n\n</br>\n\n## Installation\n\n```bash\npip install flask-ms-oauth2\n```\n\n### Usage\n\n```python\nfrom flask import Flask\nfrom flask import redirect\nfrom flask import url_for\nfrom flask import session\nfrom flask import jsonify\nfrom flask_ms_oauth2 import MSOAuth2Manager\nfrom flask_ms_oauth2 import login_handler\nfrom flask_ms_oauth2 import logout_handler\nfrom flask_ms_oauth2 import callback_handler\n\napp = Flask(__name__)\napp.secret_key = \"my super secret key\"\n\n# Setup the flask-ms-oauth2 extention\napp.config['CLIENT_ID'] = \"xxxxxxxxxxxxxxxxxxxxxxxxxx\"\napp.config['CLIENT_SECRET'] = \"xxxxxxxxxxxxxxxxxxxxxxxxxx\"\napp.config['TENANT_ID'] = \"xxxxxxxxxxxxxxxxxxxxxxxxxx\"\napp.config[\"ERROR_REDIRECT_URI\"] = \"page500\" # Optional\napp.config[\"STATE\"] = \"mysupersecrethash\" # Optional\n\napp.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\n\napp.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\n\n\nmsoauth2 = MSOAuth2Manager(app)\n\n\n@app.route('/login', methods=['GET'])\ndef login():\n print(\"Do the stuff before login to Microsoft Oauth2 Service\")\n response = redirect(url_for(\"msoauth2login\"))\n return response\n\n\n@app.route('/logout', methods=['GET'])\ndef logout():\n print(\"Do the stuff before logout from Microsoft Oauth2 Service\")\n response = redirect(url_for(\"msoauth2logout\"))\n return response\n\n\n# Use @login_handler decorator on Microsoft OAuth2 login route\n@app.route('/msoauth2/login', methods=['GET'])\n@login_handler\ndef msoauth2login():\n pass\n\n\n@app.route('/home', methods=['GET'])\ndef home():\n current_user = session[\"username\"]\n return jsonify(logged_in_as=current_user), 200\n\n\n# Use @callback_handler decorator on Microsoft OAuth2 callback route\n@app.route('/auth/callback', methods=['GET'])\n@callback_handler\ndef callback():\n for key in list(session.keys()):\n print(f\"Value for {key} is {session[key]}\")\n response = redirect(url_for(\"home\"))\n return response\n\n\n\n# Use @logout_handler decorator on Microsoft OAuth2 logout route\n@app.route('/msoauth2/logout', methods=['GET'])\n@logout_handler\ndef msoauth2logout():\n pass\n\n\n@app.route('/page500', methods=['GET'])\ndef page500():\n return jsonify(Error=\"Something went wrong\"), 500\n\n\nif __name__ == '__main__':\n app.run(debug=True)\n```\n\n\n\n### Development Setup\n\nUsing pipenv\n```bash\npipenv install --dev \n```\nUsing virtualenv\n```bash\npython3 -m venv venv\nsource venv/bin/activate\npip install .\n```\n\n### Contributing\n\n1. Fork repo- https://github.com/shrivastava-v-ankit/flask-ms-oauth2.git\n2. Create your feature branch - `git checkout -b feature/name`\n3. Add Python test (pytest) and coverage report for new/changed feature.\n4. Commit your changes - `git commit -am \"Added name\"`\n5. Push to the branch - `git push origin feature/name`\n6. Create a new pull request\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Flask implementation for Microsoft Oauth2 authentication",
"version": "1.0.0",
"split_keywords": [
"flask",
"microsoft",
"json web token",
"oauth2",
"authentication"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "92930a879fa9b0138caa1c5dcdb8548981572a3f3893e445c32e7562fd057fe6",
"md5": "f9f57d0486b60bb1aaf1753f683d19ed",
"sha256": "e0a054a86488f2f2cf528396fb0a9f766b0a5a0f6e1fbe6915dd31221da60372"
},
"downloads": -1,
"filename": "flask_ms_oauth2-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f9f57d0486b60bb1aaf1753f683d19ed",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 9534,
"upload_time": "2023-04-18T11:03:14",
"upload_time_iso_8601": "2023-04-18T11:03:14.254324Z",
"url": "https://files.pythonhosted.org/packages/92/93/0a879fa9b0138caa1c5dcdb8548981572a3f3893e445c32e7562fd057fe6/flask_ms_oauth2-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "879f3ba916ece4d1338b3db659b66a1846c34c5020b7f1df573c775cb55b81b1",
"md5": "987a914b9f02ef75be257a86becc0d00",
"sha256": "78e776c66330fe2d01f89621a40302d25fb3195cdb0e2ef93f13b336a9ec3358"
},
"downloads": -1,
"filename": "flask-ms-oauth2-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "987a914b9f02ef75be257a86becc0d00",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 15585,
"upload_time": "2023-04-18T11:03:18",
"upload_time_iso_8601": "2023-04-18T11:03:18.022477Z",
"url": "https://files.pythonhosted.org/packages/87/9f/3ba916ece4d1338b3db659b66a1846c34c5020b7f1df573c775cb55b81b1/flask-ms-oauth2-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-18 11:03:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "shrivastava-v-ankit",
"github_project": "flask-ms-oauth2",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"lcname": "flask-ms-oauth2"
}