device

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package device implements an OAuth 2.0 Device Authorization Grant (RFC 8628) client for command-line tools on machines without a browser.

Start requests a device and user code and returns what the user must see; Poll waits for the user to approve the request on another device and returns the issued tokens. Endpoints come from OpenID Connect discovery or explicit configuration. The returned ID token is not verified by this package; relying servers must verify it (for example with authentication/jwt or go-oidc) before trusting it.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAccessDenied reports that the user denied the authorization request.
	ErrAccessDenied = errors.New("device: access denied")
	// ErrServer reports a transport failure or an invalid authorization server
	// response.
	ErrServer = errors.New("device: authorization server request failed")
)

Functions

This section is empty.

Types

type Authorization

type Authorization struct {
	DeviceCode              authoidc.Secret
	UserCode                string
	VerificationURI         string
	VerificationURIComplete string
	ExpiresAt               time.Time
	Interval                time.Duration
}

Authorization is a pending device authorization. Show UserCode and VerificationURI (or VerificationURIComplete) to the user, then call Poll. DeviceCode is redacted when formatted.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client runs device authorization flows for one OAuth client. It is safe for concurrent use.

func New

func New(ctx context.Context, options Options) (*Client, error)

New validates options and, when Issuer is set, performs OpenID Connect discovery.

func (*Client) Poll

func (c *Client) Poll(ctx context.Context, authorization Authorization) (*oauth2.Token, string, error)

Poll waits until the user completes the authorization and returns the token and the raw, unverified ID token (empty when none was issued). It polls at the authorization interval, waits 5 seconds longer after each slow_down response, and keeps waiting on authorization_pending. It returns authentication.ErrExpired when the authorization expires, ErrAccessDenied when the user denies it, an *Error for other OAuth errors, and ctx.Err() when ctx ends first.

func (*Client) Start

func (c *Client) Start(ctx context.Context) (Authorization, error)

Start requests a new device authorization.

type Error

type Error struct {
	Code       string
	StatusCode int
}

Error is an OAuth error response from the authorization server. Code is the RFC 6749 error code when it is well formed and empty otherwise. Error matches authentication.ErrUnauthenticated with errors.Is.

func (*Error) Error

func (e *Error) Error() string

Error describes the OAuth error without any response body.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns authentication.ErrUnauthenticated.

type Options

type Options struct {
	Issuer        string
	DeviceAuthURL string
	TokenURL      string
	ClientID      string
	// ClientSecret is optional; device clients are usually public. When set
	// it is sent with HTTP Basic authentication.
	ClientSecret string
	// Scopes are requested verbatim. With Issuer, "openid" is added when
	// missing so that an ID token is issued.
	Scopes []string
	// HTTPClient defaults to a client with a 30 second timeout.
	HTTPClient *http.Client
}

Options configures a Client. Set either Issuer, for OpenID Connect discovery, or both DeviceAuthURL and TokenURL. Endpoints must use https, except http on loopback hosts.

Jump to

Keyboard shortcuts

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