Identity

0.11.0 · active · verified Fri Apr 17

Identity is an authentication and authorization library optimized for web applications, building upon Microsoft's MSAL Python. It provides high-level APIs for popular frameworks like Flask, Quart, and Django, simplifying integration with Microsoft Identity Platform. The library is actively maintained with frequent updates addressing bug fixes and introducing new features.

Common errors

Warnings

Install

Imports

Quickstart

This Flask example demonstrates how to set up `identity` for authentication. It initializes `Auth` with credentials typically pulled from environment variables, protects the root route with `@login_required`, handles the redirect after login, and provides a logout mechanism. Ensure you replace the placeholder environment variables with your actual Azure AD (or Entra ID) application registration details for client ID, client secret, and authority.

import os
from flask import Flask, render_template_string, session, redirect, url_for, request
from identity.flask import Auth, login_required

app = Flask(__name__)
app.secret_key = os.urandom(32) # Use a strong, rotated key in production

# Configure Identity using environment variables
auth = Auth(
    app,
    authority=os.environ.get('IDENTITY_AUTHORITY', 'https://login.microsoftonline.com/common'),
    client_id=os.environ.get('IDENTITY_CLIENT_ID', 'YOUR_CLIENT_ID'),
    client_secret=os.environ.get('IDENTITY_CLIENT_SECRET', 'YOUR_CLIENT_SECRET'),
    redirect_uri=os.environ.get('IDENTITY_REDIRECT_URI', 'http://localhost:5000/redirect'),
    endpoint=os.environ.get('IDENTITY_ENDPOINT', 'https://graph.microsoft.com/v1.0/users'),
    scope=os.environ.get('IDENTITY_SCOPE', 'User.ReadBasic.All').split()
)

@app.route("/")
@login_required
def index():
    user_data = session.get('user', {})
    return render_template_string(
        """
        <h1>Welcome, {{ user.get('name', 'Guest') }}!</h1>
        <p>Logged in user details: {{ user }}</p>
        <p><a href="{{ url_for('logout') }}">Logout</a></p>
        """,
        user=user_data
    )

@app.route(auth.redirect_uri_path)
def auth_redirect():
    auth.complete_login(request.args)
    return redirect(url_for("index"))

@app.route("/logout")
def logout():
    return auth.logout(url_for("index", _external=True))

if __name__ == "__main__":
    # Set dummy values for quick local test if env vars are not set
    os.environ.setdefault('IDENTITY_CLIENT_ID', 'YOUR_CLIENT_ID_FROM_AZURE')
    os.environ.setdefault('IDENTITY_CLIENT_SECRET', 'YOUR_CLIENT_SECRET_FROM_AZURE')
    # Make sure to replace YOUR_TENANT_ID with your actual tenant ID or 'common' for multi-tenant
    os.environ.setdefault('IDENTITY_AUTHORITY', 'https://login.microsoftonline.com/YOUR_TENANT_ID')
    os.environ.setdefault('IDENTITY_REDIRECT_URI', 'http://localhost:5000/redirect')
    os.environ.setdefault('IDENTITY_ENDPOINT', 'https://graph.microsoft.com/v1.0/me')
    os.environ.setdefault('IDENTITY_SCOPE', 'User.ReadBasic.All')

    print("\n--- To run this app, make sure to replace placeholders YOUR_CLIENT_ID_FROM_AZURE and YOUR_CLIENT_SECRET_FROM_AZURE with actual values from your Azure App Registration. ---\n")
    app.run(debug=True, port=5000)

view raw JSON →