oauth2

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 13 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 oauth2 implements the plain OAuth 2.0 authorization code flow with PKCE (S256) as an authentication.FederatedProvider, for providers that do not issue OpenID Connect ID tokens.

State handling matches package oidc: the raw state is never stored, the pending state is consumed atomically before the code exchange, and it expires after Options.StateTTL. Because plain OAuth 2.0 has no verifiable identity token, the required UserInfoMapper is the only identity source: it receives the access token and must establish the principal, for example by calling the provider's user-info API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Name identifies the provider (lower-cased and trimmed). Required.
	Name string
	// Config holds the client credentials and endpoints. ClientID, AuthURL
	// and TokenURL are required; endpoints must use https, or http on a
	// loopback host.
	Config xoauth2.Config
	// RedirectURIs lists the exact redirect URIs Begin accepts (https, or
	// http on a loopback host). Required, without duplicates.
	RedirectURIs []string
	// State persists pending callback state. Required.
	State authoidc.StateStore
	// Mapper establishes the principal from the access token. Required.
	Mapper UserInfoMapper
	// Clock returns the current time used for state expiry and principal
	// validation; its result is converted to UTC. Defaults to time.Now.
	Clock func() time.Time
	// Random supplies state and PKCE entropy. Defaults to
	// crypto/rand.Reader; any replacement must be cryptographically secure.
	Random io.Reader
	// StateTTL bounds the time between Begin and Complete. Defaults to ten
	// minutes.
	StateTTL time.Duration
}

Options configures a Provider.

type Provider

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

Provider is an OAuth 2.0 authorization code flow provider. It is safe for concurrent use when its StateStore and Mapper are.

func New

func New(options Options) (*Provider, error)

New validates options and returns a Provider. It returns authentication.ErrInvalidCredential or authentication.ErrInvalidCallback for invalid options.

func (*Provider) Begin

Begin starts a login for a registered redirect URI and returns the authorization URL. Scopes are requested as given; "openid" is not added.

func (*Provider) Complete

Complete consumes the callback state, exchanges the code and maps the access token to a principal. It returns authentication.ErrReplay for unknown or reused state, authentication.ErrExpired after Options.StateTTL, and authentication.ErrUnauthenticated for every other failure.

func (*Provider) Name

func (p *Provider) Name() string

Name returns the normalized provider name.

type UserInfoMapper

type UserInfoMapper func(context.Context, *xoauth2.Token) (authentication.Principal, error)

UserInfoMapper derives the principal from an access token. Its result is validated with authentication.ValidatePrincipal.

Jump to

Keyboard shortcuts

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