mockauth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package mockauth is a fake auth service for the CLI's tests and the kapten-mock server. It follows the token endpoint of auth (auth/internal/oauth): every request carries a DPoP proof it verifies, device codes and refresh tokens are bound to the proof key, and refresh tokens rotate, a reused one revoking its family. Access tokens it issued can be checked with Verify, which the mock Engine does.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidToken = errors.New("the access token is invalid, expired or revoked")

ErrInvalidToken is an access token the server did not issue, or that expired or was revoked.

Functions

This section is empty.

Types

type Server

type Server struct {
	// URL is the issuer: the base URL the server is reachable at.
	URL string
	// Errorf reports a request the real auth would also refuse but that a
	// correct CLI never sends, such as an invalid DPoP proof.
	Errorf func(format string, args ...any)

	// Subject, Email and Organization are put in the next issued tokens.
	Subject, Email, Organization string
	// OrganizationName answers GET /organizations/{Organization}.
	OrganizationName string
	// PendingPolls is how many polls answer authorization_pending before
	// the device code is approved.
	PendingPolls int
	// SlowDowns is how many of those answer slow_down instead.
	SlowDowns int
	// Deny makes the user deny the device request.
	Deny bool
	// Unavailable is how many token requests answer 503, as a gateway does
	// while auth restarts.
	Unavailable int
	// Expire makes the device code expired.
	Expire bool
	// ManualApproval keeps device requests pending until their code is
	// approved, through Approve or the /device page.
	ManualApproval bool
	// Interval is the poll interval, in seconds, given with device codes.
	Interval int
	// TTL is the lifetime of access tokens.
	TTL time.Duration
	// Applications maps OAuth application client ids to their secrets.
	Applications map[string]string
	// ApplicationKeys maps OAuth application client ids to their public keys,
	// for private_key_jwt.
	ApplicationKeys map[string]jwk.Key
	// OnToken runs on each token request, before it is answered.
	OnToken func()

	// Calls counts token requests by grant type.
	Calls map[string]int
	// Proofs are the jkt of every verified token request proof.
	Proofs []string
	// contains filtered or unexported fields
}

Server is a fake auth service. It is an http.Handler served at URL, its issuer.

func New

func New() *Server

New returns a fake auth service. Set URL to where it is served before the first request.

func (*Server) Approve

func (s *Server) Approve(userCode string) bool

Approve approves the pending device request showing userCode.

func (*Server) Close

func (s *Server) Close()

Close is a no-op: the caller owns the listener.

func (*Server) Count

func (s *Server) Count(grant string) int

Count is the number of token requests of a grant type.

func (*Server) ExpireAccessTokens

func (s *Server) ExpireAccessTokens()

ExpireAccessTokens makes every issued access token expired, as if their lifetime had passed; refresh tokens stay valid.

func (*Server) RevokeSessions

func (s *Server) RevokeSessions()

RevokeSessions revokes every grant, as when the browser session that approved them signs out: access and refresh tokens stop working.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP answers auth's discovery, device, token and organization endpoints.

func (*Server) Set

func (s *Server) Set(f func(*Server))

Set changes fields under the server lock.

func (*Server) Verify

func (s *Server) Verify(token string) error

Verify checks an access token the way a resource server would.

Jump to

Keyboard shortcuts

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