auth

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 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 auth implements the OAuth2 device-authorization-grant client (RFC 8628).

Index

Constants

View Source
const (
	CodeAuthorizationPending = "authorization_pending"
	CodeSlowDown             = "slow_down"
	CodeExpiredToken         = "expired_token"
	CodeAccessDenied         = "access_denied"
)

OAuth device-flow error codes (RFC 8628 §3.5).

View Source
const (
	TokenTypeAccess  = "access_token"
	TokenTypeRefresh = "refresh_token"
)

Token type hints for Revoke (RFC 7009 §2.1); "" omits the hint.

Variables

View Source
var ErrKeychainUnavailable = errors.New("could not store the token in the OS keychain")

ErrKeychainUnavailable means the OS keychain refused a write for a profile that did not choose file storage. Tokens never fall back to the plaintext file on their own.

View Source
var ErrNotFound = errors.New("no stored credential")

ErrNotFound means no credential is stored for the requested profile.

Functions

func SameHost

func SameHost(a, b string) bool

SameHost reports whether two stored host spellings name one host.

Types

type Client

type Client struct {

	// SlowDownIncrement is added on each slow_down (RFC 8628 §3.5).
	SlowDownIncrement time.Duration
	// contains filtered or unexported fields
}

Client calls the OAuth host: device grant, refresh, and revoke.

func NewClient

func NewClient(baseURL, clientID string, httpClient *http.Client) *Client

NewClient returns an OAuth Client for baseURL as clientID; a nil httpClient gets a default.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns the resolved OAuth host; login stores it so refresh reaches the issuing host.

func (*Client) ListTokens

func (c *Client) ListTokens(ctx context.Context, accessToken string) ([]TokenSession, error)

ListTokens returns the caller's OAuth sessions.

func (*Client) PollToken

func (c *Client) PollToken(ctx context.Context, deviceCode, verifier string, interval, expiresIn time.Duration) (*Token, error)

PollToken polls until approval, denial, ctx cancel, or expiresIn (a client-side deadline; <= 0 disables it), honouring interval and slow_down (RFC 8628 §3.5).

func (*Client) Refresh

func (c *Client) Refresh(ctx context.Context, refreshToken string, scopes []string) (*Token, error)

Refresh exchanges a refresh token for a new access token, optionally changing scopes.

func (*Client) RequestDeviceCode

func (c *Client) RequestDeviceCode(ctx context.Context, scopes []string) (*DeviceAuth, *PKCE, error)

RequestDeviceCode starts the device flow and returns the response and the PKCE pair.

func (*Client) Revoke

func (c *Client) Revoke(ctx context.Context, token, tokenTypeHint string) error

Revoke invalidates a token (RFC 7009); tokenTypeHint may be "".

func (*Client) RevokeAllTokens

func (c *Client) RevokeAllTokens(ctx context.Context, accessToken string) (int, error)

RevokeAllTokens revokes every session of the caller and returns the count.

func (*Client) RevokeTokenID

func (c *Client) RevokeTokenID(ctx context.Context, accessToken, id string) (int, error)

RevokeTokenID revokes one of the caller's sessions and returns the number revoked.

type Credential

type Credential struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	ExpiresAt    time.Time `json:"expires_at,omitempty"`
	Scope        string    `json:"scope,omitempty"`
}

Credential is a stored token set for one profile.

func CredentialFromToken

func CredentialFromToken(t *Token) *Credential

CredentialFromToken converts a Token into a Credential with an absolute ExpiresAt.

func (*Credential) Expired

func (c *Credential) Expired() bool

Expired reports whether the access token is past expiry minus a skew; a zero ExpiresAt never expires.

func (*Credential) Revoked

func (c *Credential) Revoked() bool

Revoked reports whether the credential holds no token.

func (*Credential) String

func (c *Credential) String() string

String masks the tokens.

type DeviceAuth

type DeviceAuth struct {
	DeviceCode              string `json:"device_code"`
	UserCode                string `json:"user_code"`
	VerificationURI         string `json:"verification_uri"`
	VerificationURIComplete string `json:"verification_uri_complete"`
	ExpiresIn               int    `json:"expires_in"`
	Interval                int    `json:"interval"`
}

DeviceAuth is the response to a device-authorization request (RFC 8628 §3.2).

type Error

type Error struct {
	Code        string
	Description string
	StatusCode  int
}

Error is an OAuth host error response carrying the RFC error code.

func (*Error) Error

func (e *Error) Error() string

type Manager

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

Manager stores credentials in the OS keychain, or inline in the 0600 hosts file when the profile keeps them there: chosen with --insecure-storage, or a login that found no usable keychain.

func NewManager

func NewManager(hosts *config.Hosts) *Manager

NewManager returns a Manager backed by the given hosts document.

func (*Manager) Current added in v0.1.1

func (m *Manager) Current(profile string) (*Credential, error)

Current re-reads the stored credential of profile, including writes by other processes. It leaves the document the Manager holds alone: callers keep reading the profile they resolved, even after a logout elsewhere.

func (*Manager) Delete

func (m *Manager) Delete(profile string) error

Delete removes a profile and its credential from keychain and file.

func (*Manager) Get

func (m *Manager) Get(profile string) (*Credential, error)

Get returns the credential stored for a profile.

func (*Manager) Hosts

func (m *Manager) Hosts() *config.Hosts

Hosts returns the underlying hosts document.

func (*Manager) LockRefresh added in v0.1.1

func (m *Manager) LockRefresh(ctx context.Context) (release func(), ok bool)

LockRefresh serializes spending a refresh token across processes. The server rotates the token on use, so two processes spending the same one leave the loser with invalid_grant and the profile revoked. It waits until ctx is done; ok is false when no lock was taken (another holder for the whole wait, Ctrl-C, or no config dir).

func (*Manager) MarkRevoked

func (m *Manager) MarkRevoked(profile string, from *Credential) error

MarkRevoked clears a profile's tokens but keeps the profile, writing only while it still holds from.

func (*Manager) Set

func (m *Manager) Set(profile, host string, cred *Credential, scopes []string, insecure bool, email string) (usedFile bool, err error)

Set stores cred under the cross-process lock; nil scopes and "" email keep the stored values. usedFile reports that the token went to the 0600 file, which happens only when insecure is set; a keychain failure otherwise returns ErrKeychainUnavailable and writes nothing.

func (*Manager) SetActive

func (m *Manager) SetActive(profile string) error

SetActive switches the active profile; ErrNotFound if it does not exist on disk.

func (*Manager) SetIfCurrent

func (m *Manager) SetIfCurrent(profile, host string, from, cred *Credential, scopes []string, insecure bool) (usedFile bool, err error)

SetIfCurrent stores cred only while the profile still holds from, else returns *ProfileChangedError: a mismatch means the label changed hands and cred belongs to its previous holder.

type PKCE

type PKCE struct {
	Verifier  string
	Challenge string
}

PKCE is a code_verifier and its S256 code_challenge.

func NewPKCE

func NewPKCE() (*PKCE, error)

NewPKCE generates a random S256 PKCE pair (RFC 7636).

type ProfileChangedError

type ProfileChangedError struct {
	Profile string
	Held    *Credential
}

ProfileChangedError is a refused conditional write: the profile no longer holds the credential read before the exchange. Held is what it holds now, or nil.

func (*ProfileChangedError) Error

func (e *ProfileChangedError) Error() string

type Token

type Token struct {
	AccessToken  string `json:"access_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	Scope        string `json:"scope"`
	Email        string `json:"email"`
	Name         string `json:"name"`
}

Token is an issued token pair; Email/Name are set when the server includes them.

type TokenSession

type TokenSession struct {
	ID         string   `json:"id"`
	Scopes     []string `json:"scopes"`
	Revoked    bool     `json:"revoked"`
	Current    bool     `json:"current"`
	CreatedAt  string   `json:"created_at"`
	ExpiresAt  string   `json:"expires_at"`
	LastUsedAt string   `json:"last_used_at"`
}

TokenSession is one OAuth session from GET /oauth/tokens; token values are never returned.

Jump to

Keyboard shortcuts

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