Documentation
¶
Overview ¶
Package auth implements the OAuth2 device-authorization-grant client (RFC 8628).
Index ¶
- Constants
- Variables
- func SameHost(a, b string) bool
- type Client
- func (c *Client) BaseURL() string
- func (c *Client) ListTokens(ctx context.Context, accessToken string) ([]TokenSession, error)
- func (c *Client) PollToken(ctx context.Context, deviceCode, verifier string, ...) (*Token, error)
- func (c *Client) Refresh(ctx context.Context, refreshToken string, scopes []string) (*Token, error)
- func (c *Client) RequestDeviceCode(ctx context.Context, scopes []string) (*DeviceAuth, *PKCE, error)
- func (c *Client) Revoke(ctx context.Context, token, tokenTypeHint string) error
- func (c *Client) RevokeAllTokens(ctx context.Context, accessToken string) (int, error)
- func (c *Client) RevokeTokenID(ctx context.Context, accessToken, id string) (int, error)
- type Credential
- type DeviceAuth
- type Error
- type Manager
- func (m *Manager) Current(profile string) (*Credential, error)
- func (m *Manager) Delete(profile string) error
- func (m *Manager) Get(profile string) (*Credential, error)
- func (m *Manager) Hosts() *config.Hosts
- func (m *Manager) LockRefresh(ctx context.Context) (release func(), ok bool)
- func (m *Manager) MarkRevoked(profile string, from *Credential) error
- func (m *Manager) Set(profile, host string, cred *Credential, scopes []string, insecure bool, ...) (usedFile bool, err error)
- func (m *Manager) SetActive(profile string) error
- func (m *Manager) SetIfCurrent(profile, host string, from, cred *Credential, scopes []string, insecure bool) (usedFile bool, err error)
- type PKCE
- type ProfileChangedError
- type Token
- type TokenSession
Constants ¶
const ( CodeAuthorizationPending = "authorization_pending" CodeSlowDown = "slow_down" CodeExpiredToken = "expired_token" CodeAccessDenied = "access_denied" )
OAuth device-flow error codes (RFC 8628 §3.5).
const ( TokenTypeAccess = "access_token" TokenTypeRefresh = "refresh_token" )
Token type hints for Revoke (RFC 7009 §2.1); "" omits the hint.
Variables ¶
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.
var ErrNotFound = errors.New("no stored credential")
ErrNotFound means no credential is stored for the requested profile.
Functions ¶
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 ¶
NewClient returns an OAuth Client for baseURL as clientID; a nil httpClient gets a default.
func (*Client) BaseURL ¶
BaseURL returns the resolved OAuth host; login stores it so refresh reaches the issuing host.
func (*Client) ListTokens ¶
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 ¶
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) RevokeAllTokens ¶
RevokeAllTokens revokes every session of the caller and returns the count.
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.
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 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 ¶
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) Get ¶
func (m *Manager) Get(profile string) (*Credential, error)
Get returns the credential stored for a profile.
func (*Manager) LockRefresh ¶ added in v0.1.1
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 ¶
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 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.