Skip to main content

Pulse MSAL

Microsoft Entra ID (Azure AD) authentication plugin for Pulse applications using MSAL.

Architecture

Server-side OAuth2 authentication using Microsoft Authentication Library (MSAL). Handles token acquisition, caching, and session management.

┌──────────────────────────────────────────────────────────────────┐
│  Pulse App                                                       │
│  ┌────────────────┐  ┌─────────────────┐  ┌──────────────────┐  │
│  │   MSALPlugin   │──│ TokenCacheStore │──│ UserSession      │  │
│  └────────────────┘  └─────────────────┘  └──────────────────┘  │
│          │                                                       │
│          │ OAuth2 Auth Code Flow                                 │
│          ▼                                                       │
│  ┌────────────────────────────────────────────────────────────┐  │
│  │              Microsoft Entra ID                             │  │
│  └────────────────────────────────────────────────────────────┘  │
└──────────────────────────────────────────────────────────────────┘

Folder Structure

src/pulse_msal/
├── __init__.py    # Public exports
└── plugin.py      # MSALPlugin, TokenCacheStore, auth helpers

Usage

Setup

import pulse as ps
from pulse_msal import MSALPlugin, FileTokenCacheStore

app = ps.App(
    plugins=[
        MSALPlugin(
            client_id="your-client-id",
            client_secret="your-client-secret",
            tenant_id="your-tenant-id",
            redirect_uri="http://localhost:8000/auth/callback",
            scopes=["User.Read"],
            token_cache_store=FileTokenCacheStore(".token_cache"),
        ),
    ],
    routes=[...],
)

Authentication Functions

from pulse_msal import auth, login, logout

@ps.component
def protected_page():
    user = auth()
    if not user:
        return ps.button("Login", onClick=lambda _: login())

    return ps.div([
        ps.p(f"Hello, {user['name']}"),
        ps.button("Logout", onClick=lambda _: logout()),
    ])

Claims Mapping

Custom claims extraction:

from pulse_msal import MSALPlugin, ClaimsMapper

def custom_mapper(claims: dict) -> dict:
    return {
        "id": claims.get("oid"),
        "email": claims.get("preferred_username"),
        "name": claims.get("name"),
        "roles": claims.get("roles", []),
    }

MSALPlugin(
    ...,
    claims_mapper=custom_mapper,
)

Token Cache Stores

FileTokenCacheStore

File-based token caching for development:

from pulse_msal import FileTokenCacheStore

FileTokenCacheStore(cache_dir=".token_cache")

RedisTokenCacheStore

Redis-backed caching for production:

from pulse_msal import RedisTokenCacheStore

RedisTokenCacheStore(
    redis_url="redis://localhost:6379",
    prefix="msal:",
)

Custom Store

Implement the TokenCacheStore protocol:

from pulse_msal import TokenCacheStore

class CustomStore(TokenCacheStore):
    async def get(self, key: str) -> bytes | None: ...
    async def set(self, key: str, value: bytes) -> None: ...
    async def delete(self, key: str) -> None: ...

Main Exports

  • MSALPlugin - authentication plugin
  • TokenCacheStore - cache protocol
  • FileTokenCacheStore - file-based cache
  • RedisTokenCacheStore - Redis cache
  • ClaimsMapper - claims transformation type
  • auth() - get current user
  • login() - initiate login flow
  • logout() - clear session

Release files for pulse-msal 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pulse-msal 0.1.5
File Size Uploaded
pulse_msal-0.1.5.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pulse-msal 0.1.5
File Interpreter ABI Platform
pulse_msal-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / pulse_msal-0.1.5.tar.gz

Download URL pulse_msal-0.1.5.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
863374f65bdab7ff5220818556a4c961e52fc771d113eaf27dfc4e197264075c
BLAKE2b-256 checksum
How to use checksums
bef2ff58489edf0d484ec03d8adcb9bf111d3eb204e590ee4ba422d6577cb81b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pulse_msal-0.1.5-py3-none-any.whl

Download URL pulse_msal-0.1.5-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67a3f63dba24ad33962a9b7de917a2a42bb39fac06c0e4337235860aa9b3a66f
BLAKE2b-256 checksum
How to use checksums
5052068ab694c1d7d2a1ea4bd8575c9860cc5424c06298c4440e37cc21e077c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page