Skip to main content

Helper Library to use the Microsoft Authentication Library (MSAL) with aiohttp

Project description

aiohttp_msal Python library

AsyncIO based OAuth using the Microsoft Authentication Library (MSAL) for Python.

Blocking MSAL functions are executed in the executor thread. Should be useful until such time as MSAL Python gets a true async version...

Tested with MSAL Python 1.21.0 onward - MSAL Python docs

AsycMSAL class

The AsyncMSAL class wraps the behavior in the following example app https://github.com/Azure-Samples/ms-identity-python-webapp/blob/master/app.py#L76

It is responsible to manage tokens & token refreshes and as a client to retrieve data using these tokens.

Acquire the token

Firstly you should get the tokens via OAuth

  1. initiate_auth_code_flow referernce

    The caller is expected to:

    1. somehow store this content, typically inside the current session of the server,
    2. guide the end user (i.e. resource owner) to visit that auth_uri, typically with a redirect
    3. and then relay this dict and subsequent auth response to acquire_token_by_auth_code_flow().

    Step 1 and part of Step 3 is stored by this class in the aiohttp_session

  2. acquire_token_by_auth_code_flow referernce

Use the token

Now you are free to make requests (typically from an aiohttp server)

session = await get_session(request)
aiomsal = AsyncMSAL(session)
async with aiomsal.get("https://graph.microsoft.com/v1.0/me") as res:
    res = await res.json()

Example web server

Complete routes can be found in routes.py

Start the login process

@ROUTES.get("/user/login")
async def user_login(request: web.Request) -> web.Response:
    """Redirect to MS login page."""
    session = await new_session(request)

    redir = AsyncMSAL(session).build_auth_code_flow(
        redirect_uri=get_route(request, URI_USER_AUTHORIZED)
    )

    return web.HTTPFound(redir)

Acquire the token after being redirected back to the server

@ROUTES.post(URI_USER_AUTHORIZED)
async def user_authorized(request: web.Request) -> web.Response:
    """Complete the auth code flow."""
    session = await get_session(request)
    auth_response = dict(await request.post())

    aiomsal = AsyncMSAL(session)
    await aiomsal.async_acquire_token_by_auth_code_flow(auth_response)

Helper methods

  • @ROUTES.get("/user/photo")

    Serve the user's photo from their Microsoft profile

  • get_user_info

    Get the user's email and display name from MS Graph

  • get_manager_info

    Get the user's manager info from MS Graph

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_msal-0.6.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

aiohttp_msal-0.6-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_msal-0.6.tar.gz.

File metadata

  • Download URL: aiohttp_msal-0.6.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for aiohttp_msal-0.6.tar.gz
Algorithm Hash digest
SHA256 f91474b4da74ce5be6ca902595ceecf76cb04988b5a7b59c2099317a977412f5
MD5 21466e9b2c6838b507ee6da032fe3ef6
BLAKE2b-256 888cbec10e7af58531d721d9f32395ca56f7cad085476adecbd7fe1005b0cd00

See more details on using hashes here.

Provenance

File details

Details for the file aiohttp_msal-0.6-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_msal-0.6-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for aiohttp_msal-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e5d13741afb19d9efbedf0257edd1e6d51de95599a42affe0a6d0a34159c8035
MD5 bd073b3dba8bb3b4c4adc2cc15aeb866
BLAKE2b-256 1586a65c0d2a061a4554ed70f83a3a76f28bb71d12ee657c9f1a90fb29073120

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page