The package implements the OAuth 2.0 Authorization Code flow with PKCE against the Microsoft identity platform v2.0 endpoint. Users visit a "Connect Microsoft" link, get redirected to Microsoft's consent screen, come back to a package-owned callback, and land in your app with a persisted MicrosoftConnection row.
This page walks through each leg in the default direct mode. See the sub-pages for deeper coverage of specific stages.
In broker mode (MICROSOFT_OAUTH_MODE=broker) the same routes run through an OAuth broker instead of talking to Microsoft directly. The flow, session handling, and persisted MicrosoftConnection are the same. The differences are covered on that page.
Since 1.1.0, the Microsoft HTTP calls are made by the stateless MicrosoftClient. OAuthManager wraps it with the session and persistence steps described below.
Routes
The service provider mounts three routes under the fixed prefix /auth/microsoft:
| Route | Method | Name | Middleware | Purpose |
|---|---|---|---|---|
/auth/microsoft/connect |
GET | microsoft.auth.connect |
web, auth |
Redirect to Microsoft's consent screen. |
/auth/microsoft/reauthorize |
GET | microsoft.auth.reauthorize |
web, auth |
Incremental consent for scopes added since the initial connection. |
/auth/microsoft/callback |
GET | microsoft.auth.callback |
web |
Handle Microsoft's redirect back. |
The prefix, middleware, and names are fixed — they're baked into routes/web.php inside the package and not driven by config. If you need a different URL structure, skip the built-in routes entirely and wire your own controller that calls OAuthManager::authorizationUrl() / handleCallback() directly.
The callback route deliberately does NOT require auth — the user is mid-redirect from Microsoft and may or may not still be authenticated in the local session, but the OAuth state value verifies it's the same browser that started the flow. connect and reauthorize do require auth because they need the authenticated user's identifier to associate the resulting connection with.
The flow, end to end
┌──────────────┐ ┌──────────────────┐ ┌───────────────┐
│ Your app │ │ Package routes │ │ Microsoft │
└──────┬───────┘ └────────┬─────────┘ └──────┬────────┘
│ │ │
│ User clicks "Connect" │ │
│─────────────────────────>│ GET /connect │
│ │ │
│ │ Build authorize URL │
│ │ Store state + verifier │
│ │ + user_id in session │
│ │─────────────────────────>│
│ │ │ User picks account
│ │ │ User approves scopes
│ │ │
│ │<─────────────────────────│ GET /callback?code=…&state=…
│ │ │
│ │ Verify state (CSRF) │
│ │ POST /token │
│ │─────────────────────────>│
│ │<─────────────────────────│ { access_token, refresh_token,
│ │ │ id_token, expires_in, ... }
│ │ │
│ │ Decode id_token claims │
│ │ Enforce tid vs. tenant │
│ │ Persist MicrosoftConn. │
│ │ │
│<─────────────────────────│ redirect_after_connect │
│ │ microsoft.status=connected
Connect
MicrosoftAuthController::connect() is the entry point. It resolves the authenticated user, calls OAuthManager::authorizationUrl( $userId ), and returns a redirect()->away() to the built URL.
authorizationUrl() builds the URL with:
client_idfrom the configured credential driver.redirect_urifrom the credential driver when it stores one, elseconfig('microsoft-oauth.redirect_uri'). See Drivers → The redirect URI.response_type=code— the authorization-code grant.response_mode=query— Microsoft returns the code and state on the callback URL's query string.scope= the space-separated de-duplicated union of everything the scope registry returns.state= a random 40-char string, stored in the session.code_challenge/code_challenge_method=S256— PKCE. The verifier is stored in the session; the challenge is the URL-safe base64 ofSHA-256(verifier).prompt=config('microsoft-oauth.prompt', 'select_account')on the initial connect; alwaysconsenton/reauthorize.
If the credential driver reports missing client_id, this throws OAuthException("Microsoft OAuth is not configured: client_id is missing."). The controller catches this specifically and flashes the message to microsoft.error, redirecting to redirect_after_error.
Details: OAuth → Connect.
Callback
Microsoft redirects back to microsoft.auth.callback with either ?code=…&state=… on success or ?error=…&error_description=… on failure.
MicrosoftAuthController::callback():
- If
?error=…is present, flashes theerror_description(orerrorif description is empty) tomicrosoft.errorand redirects toredirect_after_error. In broker mode, arenew_urlon the broker's own host is also flashed asmicrosoft.renew_url. - Otherwise, requires both
codeandstatein the query — missing either flashes"Microsoft callback is missing required code or state parameter."and redirects. - Delegates to
OAuthManager::handleCallback( $code, $state ).
handleCallback():
- Pulls the stored
state, PKCEcode_verifier,user_id, andincrementalflag from the session. Any missing state or verifier or user_id throwsOAuthException. - Compares the returned
stateagainst the stored one in constant time (MicrosoftClient::verifyState(), which useshash_equals()) to defeat timing attacks. Mismatch ="OAuth state mismatch; possible CSRF attempt.". - POSTs to the token endpoint with
grant_type=authorization_code,code,code_verifier,client_id,redirect_uri,scope. Confidential clients also sendclient_secret; public clients omit it. - Decodes the returned
id_token's payload (base64url) to extract:oid(preferred) orsub→microsoft_user_id(stable per app+user).email(preferred) orpreferred_username→email.tid→ tenant id of the account. The JWT signature is not verified — the token arrived over TLS from Microsoft's token endpoint on a connection we initiated, so the identity claims are trustworthy for persistence purposes only (not authorization).
- Enforces the tenant authority rules against the
tidclaim. Aconsumers-only registration that gets a work-accounttidthrowsOAuthException. - Upserts a
MicrosoftConnectionfor the user, setsaccess_token,refresh_token,token_type,expires_at,scopes,tid,microsoft_user_id,email,status = 'connected',disconnect_reason = null, and saves.
Concurrent-callback safety: two callbacks for the same user racing at the same millisecond can both call firstOrNew() and both try to INSERT because the row didn't exist yet. persistConnection() catches the driver-specific duplicate-key error (23505 on Postgres; 23000 + 1062 / UNIQUE constraint failed / Duplicate entry on MySQL / SQLite) and retries against the row the other request just inserted.
Refresh token behavior: Microsoft returns a refresh_token on every successful exchange when offline_access is granted (unlike Google, which only issues one on first consent). applyTokens() still guards against a missing refresh_token in the response — if it isn't present, the existing stored token is preserved rather than wiped.
Details: OAuth → Callback.
Reauthorize
When a new service package is installed after a user is already connected, the scope registry starts returning scopes the connection doesn't hold. MicrosoftAuthController::reauthorize():
- Requires an authenticated user.
- Calls
OAuthManager::incrementalAuthorizationUrl( $userId ), which returns either an authorization URL or anIncrementalConsentResultenum case. - On
IncrementalConsentResult::NoConnection(user has never connected) → redirects to/connectso they get a real authorization prompt. - On
IncrementalConsentResult::AlreadyAuthorized(every registered scope already granted) → redirects toredirect_after_connectwithmicrosoft.status=already-authorized. - Otherwise →
redirect()->away()to the incremental URL.
The incremental URL requests the full scope union (so the returned token covers everything) but always uses prompt=consent so the consent screen actually shows. Microsoft's UI only asks the user to approve the delta — scopes they've already granted are silently re-included.
Details: OAuth → Reauthorize.
No /disconnect route
Unlike some sibling packages, this one does not ship a /disconnect route or a scope-limited disconnect UI. Marking a connection disconnected is a two-line call:
use ArtisanPackUI\MicrosoftOAuth\Models\MicrosoftConnection;
MicrosoftConnection::where( 'user_id', $user->id )->first()
?->markDisconnected( 'Disconnected by user.' );
Wire that into whatever surface makes sense in your app (a settings page, an admin action, a delete-account flow). If you need remote revocation on Microsoft's side, POST to Microsoft's logout endpoint from your own code before calling markDisconnected() — see FAQ.
Exceptions
The OAuth manager throws these exception types:
ArtisanPackUI\MicrosoftOAuth\Exceptions\OAuthException— thrown byauthorizationUrl()when the tenant is invalid orclient_idis missing, and byhandleCallback()on state mismatch, missing PKCE verifier, missing user_id,tid-authority mismatch, or a failed code exchange.ArtisanPackUI\MicrosoftOAuth\Exceptions\TokenRefreshException— thrown by the token manager when a refresh fails. See its page for the terminal-error rules. ItsLicenseExpiredExceptionsubclass covers a lapsed broker license.ArtisanPackUI\MicrosoftOAuth\Exceptions\MissingConnectionException— a subclass ofOAuthExceptionthrown byDefaultTokenProviderwhen a user has noMicrosoftConnectionon file at all.
Both carry the OAuth error code via getError() (since 1.1.0). See Exceptions.
The default controller catches OAuthException in connect(), reauthorize(), and callback() and flashes the message; other callers should handle it themselves.
Deeper topics
- Connect — building the authorize URL, PKCE, session state, error modes.
- Callback — code exchange, id_token decoding,
tidenforcement, concurrent-callback race handling. - Reauthorize — incremental consent details,
IncrementalConsentResultcases, when to trigger it. - Broker Mode — the same flow through an OAuth broker.
- Stateless Client — the session-free primitives underneath.
Continue to Tenants →

