oauth

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package oauth implements the client side of the Webda OAuth 2.1 authorization server.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotSupported = errors.New("this server does not support OAuth sign-in")

ErrNotSupported is returned when the server exposes no OAuth discovery document.

Functions

func AuthorizeURL

func AuthorizeURL(m *Metadata, p AuthorizeParams) string

AuthorizeURL builds the browser authorization URL.

func Origin

func Origin(raw string) string

Origin returns scheme://host:port of an absolute URL, with the default port made explicit ("" when raw is not an absolute URL with a host).

func RandomState

func RandomState() (string, error)

RandomState returns a 16 random bytes base64url state.

func Revoke

func Revoke(ctx context.Context, hc *http.Client, m *Metadata, clientID, token string) error

Revoke revokes a refresh token; any 2xx counts as success.

func Sanitize

func Sanitize(s string) string

Sanitize drops control characters (including ESC) from server-provided text before it is printed.

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

Sleep waits d or until ctx ends, returning ctx.Err() in that case.

Types

type AuthorizeParams

type AuthorizeParams struct{ ClientID, RedirectURI, State, Challenge, Scope, Hostname, Prompt string }

AuthorizeParams are the inputs of the authorization request.

type DeviceCode

type DeviceCode 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"`
}

DeviceCode is a device authorization answer (RFC 8628).

func StartDevice

func StartDevice(ctx context.Context, hc *http.Client, m *Metadata, clientID, scope, hostname string) (*DeviceCode, error)

StartDevice requests a device and user code.

type Error

type Error struct {
	Status      int
	Code        string
	Description string
}

Error is an RFC 6749 error answer.

func (*Error) Error

func (e *Error) Error() string

type Loopback

type Loopback struct {
	RedirectURI string // http://127.0.0.1:<port>/callback
	// contains filtered or unexported fields
}

Loopback is a one-shot authorization callback listener on 127.0.0.1 (RFC 8252).

func ListenLoopback

func ListenLoopback() (*Loopback, error)

ListenLoopback binds 127.0.0.1:0 and serves GET /callback.

func (*Loopback) Arm

func (l *Loopback) Arm(state, resultURL string)

Arm sets the expected state and the result page before the browser is opened, so a callback that arrives immediately (remembered consent) is accepted. Callbacks before Arm are refused.

func (*Loopback) Close

func (l *Loopback) Close() error

Close shuts the listener down.

func (*Loopback) Wait

func (l *Loopback) Wait(ctx context.Context, timeout time.Duration) (string, error)

Wait blocks until the armed callback arrives (returns the code), an authorization error (returns *Error), ctx is done, or timeout. Stray requests never end the wait; once Wait returns, later callbacks get 410.

type Metadata

type Metadata struct {
	Issuer                      string `json:"issuer"`
	AuthorizationEndpoint       string `json:"authorization_endpoint"`
	TokenEndpoint               string `json:"token_endpoint"`
	RevocationEndpoint          string `json:"revocation_endpoint"`
	DeviceAuthorizationEndpoint string `json:"device_authorization_endpoint"`
	ResultURI                   string `json:"webda_result_uri"`
	OperationsURI               string `json:"webda_operations_uri"`
}

Metadata is the authorization server discovery document.

func Discover

func Discover(ctx context.Context, hc *http.Client, baseURL string) (*Metadata, error)

Discover fetches <baseURL>/.well-known/oauth-authorization-server; 404 gives ErrNotSupported.

func LoadOrDiscover

func LoadOrDiscover(ctx context.Context, hc *http.Client, baseURL, cachePath string) (*Metadata, error)

LoadOrDiscover reads cachePath when present and for the same base URL, else discovers and writes it (0600).

type PKCE

type PKCE struct{ Verifier, Challenge string }

PKCE holds a code verifier and its S256 challenge.

func NewPKCE

func NewPKCE() (PKCE, error)

NewPKCE generates a verifier (32 random bytes) and its S256 challenge.

type Tokens

type Tokens struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token,omitempty"`
	ExpiresIn    int    `json:"expires_in"`
	Scope        string `json:"scope"`
}

Tokens is a token endpoint answer.

func ExchangeCode

func ExchangeCode(ctx context.Context, hc *http.Client, m *Metadata, clientID, code, verifier, redirectURI string) (*Tokens, error)

ExchangeCode trades an authorization code for tokens.

func PollDevice

func PollDevice(ctx context.Context, hc *http.Client, m *Metadata, clientID string, dc *DeviceCode, sleep func(context.Context, time.Duration) error) (*Tokens, error)

PollDevice polls the token endpoint until tokens, a terminal error, ctx done or expiry.

func Refresh

func Refresh(ctx context.Context, hc *http.Client, m *Metadata, clientID, refreshToken string) (*Tokens, error)

Refresh trades a refresh token for new tokens; refresh tokens rotate, so the answer's one must be stored.

func (Tokens) ExpiresAt

func (t Tokens) ExpiresAt(now time.Time) time.Time

ExpiresAt returns now + ExpiresIn (300 s when ExpiresIn <= 0).

Directories

Path Synopsis
Package oauthtest provides an in-memory OAuth 2.1 authorization server for tests.
Package oauthtest provides an in-memory OAuth 2.1 authorization server for tests.

Jump to

Keyboard shortcuts

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