Skip to content

Add device code fallback for Graph token auth#2535

Open
aadnehovda wants to merge 3 commits intomicrosoft:mainfrom
aadnehovda:codex-server-core-graph-auth-device-code
Open

Add device code fallback for Graph token auth#2535
aadnehovda wants to merge 3 commits intomicrosoft:mainfrom
aadnehovda:codex-server-core-graph-auth-device-code

Conversation

@aadnehovda
Copy link
Copy Markdown

Issue:
Get-GraphAccessToken currently assumes a local browser can be launched for the authorization code callback. This fails on Server Core and other non-interactive/remote PowerShell sessions where browser-based authentication is not available.

Reason:
Server Core and remote administration scenarios need an authentication path that does not require a browser on the target machine.

Fix:
Adds an -AuthFlow parameter with Auto, AuthorizationCode, and DeviceCode options. Auto keeps the existing authorization code flow when available, but uses device code authentication when running in Server Core, non-interactive, or remote PowerShell contexts, and also falls back to device code if browser launch/local callback fails. The device code implementation uses Invoke-RestMethod and handles expected OAuth polling responses such as authorization_pending, slow_down, expired_token, and authorization_declined.

Validation:

  • Ran .build\CodeFormatter.ps1 -Save -Branch main
  • Ran .build\Build.ps1
  • Ran PowerShell parser validation for Shared\AzureFunctions\Get-GraphAccessToken.ps1
  • Ran mocked auth-flow validation for browser URL encoding, browser-failure fallback, and Server Core immediate device-code selection
  • Ran live PowerShell Direct validation on a local Server Core Hyper-V VM; confirmed Auto selected device code auth and reached the Microsoft device-code endpoint
  • Attempted .build\SpellCheck.ps1; local host does not have npm, so cspell could not run locally

Copilot AI review requested due to automatic review settings May 5, 2026 10:46
@aadnehovda aadnehovda requested a review from a team as a code owner May 5, 2026 10:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a non-browser authentication path to Get-GraphAccessToken to support Server Core and non-interactive/remote PowerShell scenarios where the existing localhost callback + browser launch flow cannot work.

Changes:

  • Added -AuthFlow parameter (Auto, AuthorizationCode, DeviceCode) to control auth flow selection.
  • Implemented OAuth device code flow (init + polling) and automatic fallback from auth-code flow when browser/callback is unavailable.
  • Refactored token handling to return a consistent { AccessToken, TenantId } result object.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

enables a client application to obtain authorized access to protected resources like web APIs.
The auth code flow requires a user-agent that supports redirection from the authorization server
(the Microsoft identity platform) back to your application.
This function is used to get an access token for the Azure Graph API. By default, it uses the OAuth 2.0 authorization
ErrorAction = "Stop"
}

try {
}

if ($deviceCodeResponse.StatusCode -ne 200 -or $null -eq $deviceCodeResponse.Content) {
Write-Host "Unable to initiate device code authentication." -ForegroundColor Red
Comment on lines 436 to 437
Write-Host "Authorization code is missing in callback" -ForegroundColor Red

@aadnehovda
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants