msal-interactive-token-acquirer 1.4.0
A library to get MSAL token interactively for native client.
License: MIT
Requires Python: !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4
Keywords: MSAL OAuth2

[![Wheel](https://github.com/masamitsu-murase/msal_interactive/actions/workflows/wheel.yml/badge.svg)](https://github.com/masamitsu-murase/msal_interactive/actions/workflows/wheel.yml)
[![PyPI version](https://badge.fury.io/py/msal-interactive-token-acquirer.svg)](https://badge.fury.io/py/msal-interactive-token-acquirer)

# MSAL Interactive Token Acquirer

This is a library to acquire Microsoft OAuth2 access token **interactively**.

## Overview

This library supports **interactive** authentication.  
If you need a library for non-interactive authentication, you can use [MSAL for python](https://github.com/AzureAD/microsoft-authentication-library-for-python), which supports many authentication flows.

## How to use

### Basic usage

You can acquire Microsoft OAuth2 access token interactively as follows:

```python
from msal_interactive_token_acquirer import MsalInteractiveTokenAcquirer

tenant = "common"
client_id = "5d2a0ea0-a46a-4626-835e-04e13f75fed0"
scopes = ["User.Read"]
msal = MsalInteractiveTokenAcquirer(tenant, client_id, scopes)
msal.acquire_token_interactively()  # (*1)
msal.access_token()
# => This returns Bearer token.
```

Note that the redirect URI of the application must be "https://login.microsoftonline.com/common/oauth2/nativeclient".  
This shows the following window at `(*1)`.  
![window](https://raw.githubusercontent.com/masamitsu-murase/msal_interactive/master/resources/window.png)

You can save and load `MsalInteractiveTokenAcquirer` as follows:

```python
from msal_interactive_token_acquirer import MsalInteractiveTokenAcquirer
import pickle

# ...snip...

# msal is an instance of MsalInteractiveTokenAcquirer.
with open("msal.pickle", "wb") as f:
    pickle.dump(msal, f)

# ...snip...

with open("msal.pickle", "rb") as f:
    msal = pickle.load(f)
msal.access_token()
# => This returns Bearer token.
```

### Authentication with Requests HTTP library

You can use this library for authentication with Requests as follows:

```python
from msal_interactive_token_acquirer import MsalInteractiveTokenAcquirer
from msal_interactive_token_acquirer.requests_auth import MsalAuth
import requests

msal = MsalInteractiveTokenAcquirer("...", "...", ["..."])
msal.acquire_token_interactively()

auth = MsalAuth(msal)
response = requests.get("https://......", auth=auth)
```

Or, you can use `MsalSession` like `requests.Session` as follows:

```python
from msal_interactive_token_acquirer import MsalInteractiveTokenAcquirer
from msal_interactive_token_acquirer.requests import MsalSession

msal = MsalInteractiveTokenAcquirer("...", "...", ["..."])
msal.acquire_token_interactively()

session = MsalSession(msal)
response = session.get("https://......")
```

## License

You can use this library under the MIT License.

Copyright 2020 Masamitsu MURASE

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.