Metadata-Version: 2.1
Name: flask-ms-oauth2
Version: 1.0.0
Summary: Flask implementation for Microsoft Oauth2 authentication
Home-page: https://github.com/shrivastava-v-ankit/flask-ms-oauth2
Author: Ankit Shrivastava
License: MIT
Project-URL: Source, https://github.com/shrivastava-v-ankit/flask-ms-oauth2/
Project-URL: Tracker, https://github.com/shrivastava-v-ankit/flask-ms-oauth2/issues
Keywords: flask,microsoft,json web token,oauth2,authentication
Platform: any
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Flask
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Version Control :: Git
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: Flask-WTF (==1.1.1)
Requires-Dist: requests (==2.28.2)
Requires-Dist: msal (==1.20.0)
Requires-Dist: pyjwt (==2.6.0)
Requires-Dist: Flask (==2.2.3)
Requires-Dist: Jinja2 (==3.0.0)
Requires-Dist: MarkupSafe (==2.1.2)
Requires-Dist: WTForms (==3.0.1)
Requires-Dist: Werkzeug (==2.2.3)
Requires-Dist: click (==8.1.3)
Requires-Dist: importlib-metadata (==6.4.1)
Requires-Dist: itsdangerous (==2.1.2)
Requires-Dist: zipp (==3.15.0)
Requires-Dist: pyasn1 (==0.4.8)
Requires-Dist: ecdsa (==0.18.0)
Requires-Dist: rsa (==4.9)
Requires-Dist: six (==1.16.0)
Requires-Dist: certifi (==2022.12.7)
Requires-Dist: charset-normalizer (==2.0.12)
Requires-Dist: idna (==3.4)
Requires-Dist: urllib3 (==1.26.15)
Requires-Dist: cffi (==1.15.1)
Requires-Dist: cryptography (==36.0.2)
Requires-Dist: pycparser (==2.21)

# 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


