lex-identity-entra
LegionIO identity provider extension for Microsoft Entra ID (Azure AD). Implements the unified identity provider contract for delegated (user), application (client credentials), managed identity, and workload identity authentication patterns.
Features
- Delegated auth — OAuth2 PKCE browser flow with local callback server; device-code fallback
-
Token persistence — in memory during normal operation; graceful shutdown temporarily hands delegated tokens to Local cache for the next daemon process. Vault (
kv/data/users/<identity>/entra/<qualifier>/auth) reading and writing remain separate opt-ins viavault_read_enabled/vault_write_enabled - Scope fingerprinting — MD5 fingerprint of active scopes stored with token; any scope change forces re-authentication on next boot
-
Identity integration —
AuthValidatorupgradesLegion::Identity::ProcessviaResolver.upgrade!and registers withLegion::Identity::Brokerfor cross-extension token access - Application credentials — client credentials flow for service-to-service auth
- Managed Identity — Azure IMDS token acquisition for hosted workloads
- Workload Identity — federated credential support for Kubernetes and CI/CD
Installation
Add to your Gemfile:
gem 'lex-identity-entra'Configuration
# ~/.legionio/settings/identity.yml
identity:
entra:
auth:
tenant_id: "your-tenant-id"
client_id: "your-client-id"
delegated:
browser_auth:
auto_authenticate: false # set true to open browser automatically (boot + token refresher)
callback_timeout: 120
# Guard for automatic (actor-initiated) sign-in:
backoff_base_seconds: 1800 # wait after an unanswered tab; doubles each time
backoff_max_seconds: 21600
max_auto_attempts: 3 # then stop opening tabs until you sign in manually
skip_when_screen_locked: true # macOS: never open tabs while the screen is locked
skip_when_idle_seconds: 900 # macOS: skip if no keyboard/mouse input for this long (0 = off)
token:
vault_read_enabled: false # set true to read the token from Vault
vault_write_enabled: false # set true to write the token to Vault
refresh_buffer: 60
refresh_interval: 900
scopes:
enabled_categories:
- microsoft_graph
- teams
- one_note
- sharepoint
- azure_communication_services
- yammerUsage
Delegated (user) authentication
# Via CLI
legion lex exec entra auth login
legion lex exec entra auth statusThe AuthValidator actor fires 9 seconds after boot. If auto_authenticate: true is set,
it opens a browser window automatically. Otherwise trigger login via the CLI.
Interactive sign-in is protected by Helpers::InteractiveAuthGuard:
-
Single-flight: only one browser/device-code attempt runs at a time, per process (mutex) and across
processes (
flockon~/.legionio/run/entra_browser_auth.lock). -
Backoff: when an automatic attempt times out, the next one waits
backoff_base_seconds(default 30 min), doubling up tobackoff_max_seconds. Aftermax_auto_attemptsunanswered tabs, automatic sign-in pauses until you sign in manually (legionio auth teamsorlegion lex exec entra auth login). Backoff state is kept in~/.legionio/run/entra_browser_auth_state.json(no secrets), so restarting the daemon does not reopen a tab right away. - Presence: on macOS, automatic attempts are skipped while the screen is locked or the user is idle.
-
No refresh storms: a refresh token that Microsoft rejects (
invalid_grantand similar) is not retried until a new token is saved, and transient refresh errors back off from 30 s up to 15 min. One warning tells you how to sign in again. -
TokenRefresheronly opens a browser whenauto_authenticate: true.
Accessing tokens from another extension
token = Legion::Identity::Broker.token_for(:entra_delegated, qualifier: :delegated)Application (client credentials)
identity:
entra:
application:
tenant_id: "your-tenant-id"
client_id: "your-client-id"
client_secret: "your-client-secret" # or use VaultScope categories
| Category | Description |
|---|---|
microsoft_graph |
Core Graph API: User.Read, Files, Devices, OpenID scopes |
mail |
Mailbox access for lex-outlook: Mail.Read + Mail.Send only (opt-in) |
teams |
Teams, Chat, Channel, OnlineMeetings, Presence, Activity |
one_note |
OneNote notebook read/write |
sharepoint |
SharePoint/OneDrive files and sites |
azure_communication_services |
Teams calls and chat management |
yammer |
Viva Engage / Yammer communities and conversations |
Note on
microsoft_graphset, so existing delegated installs see no scope change. Addingidentity.entra.delegated.scopes.enabled_categorieschanges the delegatedscope_fingerprintand forces a one-time re-consent for that qualifier on next boot. This is expected least-privilege behavior, not an error.
Token storage
Restart handoff settings use the framework's extension settings namespace:
extensions:
identity:
entra:
token:
restart_cache_enabled: true # false disables shutdown handoff and restoration
restart_cache_ttl: null # remaining token lifetime; positive seconds cap it| Backend | Path | When used |
|---|---|---|
| HashiCorp Vault | kv/data/users/<identity>/entra/delegated/auth |
Only when opted in: vault_read_enabled / vault_write_enabled (both default false) |
| Memory | In-process store | Normal operation, including login and refresh |
| Local cache | Framework-namespaced restart entry, scoped by OS user, tenant, and application | Delegated token handoff on graceful shutdown only; deleted when restored |
The delegated token refresher's cancellation hook writes the current access token,
refresh token, and metadata synchronously through Legion::Cache::Helper's
local_cache_set. Its TTL is the access token's remaining lifetime, rounded down
to whole seconds. Expired tokens, missing expiration times, and subsecond remaining
lifetimes are not cached; the handoff never uses a zero/no-expiration TTL. A
configured restart_cache_ttl can shorten retention but cannot extend it beyond
the access token's expiry. Invalid TTL settings skip the write with a warning.
On the next token lookup, the daemon reads and deletes the handoff through
local_cache_get / local_cache_delete, restores it to memory, and applies the
existing scope and refresh checks. Restarting does not extend the original expiry.
Vault-only tokens continue to use the opt-in Vault path.
This requires an already connected Local Memcached or Redis backend that outlives the daemon. Lite mode's in-process shared cache is not a substitute. Crashes, forced termination, eviction, unavailable Local cache, and downtime beyond the TTL can require sign-in again. No core library changes or encryption are involved: the temporary cache value contains credentials. The extension does not write token files; any Redis disk persistence is controlled by the backend's configuration.
Identity provider contract
{
canonical_name: "jdoe", # normalized from onPremisesSamAccountName or mailNickname
kind: :human,
source: :entra_delegated,
provider_identity: "tenant-id-guid:object-id-guid",
tenant_id: "tenant-id-guid",
object_id: "object-id-guid",
profile: { ... } # normalized Graph /me profile and verified subject context
}The tenant/object key is derived only after Graph accepts the exact bearer token and
the token's oid matches /me.id. Opaque tokens still allow profile retrieval but
cannot establish a shared authenticated identity without verifiable tenant context.
Canonical names and display names are not account-linking evidence. LegionIO 1.9.53+
reuses a discovered Teams principal through this immutable key without granting
access to the discovering reader's stored Teams content.
Existing object-ID-only identity rows are not automatically adopted: their tenant must be established explicitly before linking them to the new scoped key. No tokens or source profiles are copied into the shared identity registry by Teams discovery.
Development
bundle install
bundle exec rspec --format json --out tmp/rspec_results.json --format progress --out tmp/rspec_progress.txt
bundle exec rubocop -ARestart tests use the real cache helpers and actor cancellation. Locally they
start an isolated redis-server process with disk persistence disabled. CI uses
its Redis service with a unique test namespace; ENTRA_TEST_REDIS_URL can select
an explicit test server. Install redis-server for local development.