devicecode

package
v0.0.0-...-89b23cf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package devicecode implements the OAuth 2.0 device-authorization grant (RFC 8628) against Microsoft Entra ID. The helpers here are shared between the standalone `forge channel msteams-login` CLI subcommand and the MS Teams branch of the `forge init` TUI wizard, so both flows produce identical refresh tokens and reuse the same polling / error semantics.

The flow has two halves:

  1. RequestDeviceCode — POST /devicecode → user_code + verification_uri
  2. PollDeviceToken — POST /token repeatedly until the user completes the consent step in their browser

Both halves are network calls; both honour the caller's context for cancellation and timeout. OpenURL is a best-effort cross-platform browser launcher with the same shape as forge-core/llm/oauth.openBrowser.

Index

Constants

View Source
const DefaultLoginBase = "https://login.microsoftonline.com"

DefaultLoginBase is the OAuth 2.0 authority for Microsoft Entra ID's commercial cloud. Sovereign clouds override via the LoginBase argument.

View Source
const DefaultScope = "https://graph.microsoft.com/.default offline_access"

DefaultScope is the .default scope marker plus offline_access — the same scope set the runtime authManager (forge-plugins/channels/msteams/auth.go) requests, so refresh tokens captured by this package are interchangeable with ones captured externally.

Variables

This section is empty.

Functions

func OpenURL

func OpenURL(u string) error

OpenURL launches the host's default browser pointed at u. Best-effort — failures (no display, no opener, sandboxed env) are returned so the caller can fall back to printing the URL for manual paste.

Types

type DeviceCodeResponse

type DeviceCodeResponse struct {
	UserCode        string `json:"user_code"`
	DeviceCode      string `json:"device_code"`
	VerificationURI string `json:"verification_uri"`
	ExpiresIn       int    `json:"expires_in"`
	Interval        int    `json:"interval"`
	Message         string `json:"message,omitempty"`
}

DeviceCodeResponse is the trimmed Microsoft response to POST /devicecode.

func RequestDeviceCode

func RequestDeviceCode(ctx context.Context, client *http.Client, loginBase, tenant, clientID string) (*DeviceCodeResponse, error)

RequestDeviceCode initiates the device-authorization grant. Returns the user_code + verification_uri pair the operator must visit in a browser, plus the opaque device_code the caller passes to PollDeviceToken.

type TokenResponse

type TokenResponse struct {
	AccessToken  string `json:"access_token,omitempty"`
	TokenType    string `json:"token_type,omitempty"`
	ExpiresIn    int    `json:"expires_in,omitempty"`
	RefreshToken string `json:"refresh_token,omitempty"`
	Scope        string `json:"scope,omitempty"`
	Error        string `json:"error,omitempty"`
	ErrorDesc    string `json:"error_description,omitempty"`
}

TokenResponse is the trimmed token endpoint payload.

func PollDeviceToken

func PollDeviceToken(ctx context.Context, client *http.Client, loginBase, tenant, clientID, clientSecret string, dc *DeviceCodeResponse) (*TokenResponse, error)

PollDeviceToken polls the token endpoint until the user completes consent, the device code expires, or the context is cancelled. Honours the server-advertised interval and the slow_down rate-limit response per RFC 8628 §3.5.

clientSecret is optional: pass "" for public-client apps (native/mobile registration) and the secret value for confidential-client apps (web registration). Entra returns AADSTS7000218 if a confidential client omits its secret here, so when in doubt, supply it — public clients silently ignore the extra parameter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL