authtest

package
v0.26.0 Latest Latest
Warning

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

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

Documentation

Overview

Package authtest is a fake Norcube Auth for tests: the OAuth server the CLI signs in with (metadata, authorization redirect, code exchange, refresh with rotation and reuse detection, token exchange, device flow, session upgrade, revocation) plus the few account routes commands call right after a login. It keeps everything in memory and is safe for concurrent use. Only tests import it.

Index

Constants

View Source
const (
	ClientID            = "norcube-cli"
	GrantTokenExchange  = "urn:ietf:params:oauth:grant-type:token-exchange"
	GrantDeviceCode     = "urn:ietf:params:oauth:grant-type:device_code"
	TokenTypeAccess     = "urn:ietf:params:oauth:token-type:access_token"
	TokenTypeCLISession = "urn:norcube:params:oauth:token-type:cli-session"
	// ReuseGrace: a refresh token rotated out less than this long ago is
	// refused without ending the sign-in.
	ReuseGrace = 10 * time.Second
)

Wire constants of the OAuth server.

#nosec G101 -- OAuth grant and token type identifiers, not credentials.

View Source
const (
	HomeOrgID = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"
	AcmeOrgID = "0b9d2b8e-1c1f-4c3e-9a51-6d1c2f9a7e01"
	// OtherOrgID exists, but the account isn't a member.
	OtherOrgID = "6f1e8a52-3d4b-4f7a-8c2e-9b0a1d2c3e4f"
)

Organizations the fake account belongs to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Grant

type Grant struct {
	ID      string
	Scopes  []string
	Revoked bool
	// Refresh is the current refresh token; Access the current CLI
	// access token.
	Refresh string
	Access  string
	// contains filtered or unexported fields
}

Grant is one sign-in (a CLI device).

type Server

type Server struct {
	*httptest.Server

	// OAuthOff makes the metadata 404 (an older Norcube Auth); NoExchange
	// leaves the token exchange out of grant_types_supported.
	OAuthOff   bool
	NoExchange bool
	// NoDevice leaves the device endpoint out of the metadata.
	NoDevice bool
	// IssOff stops sending iss on the redirect (and says so).
	IssOff bool
	// WrongIss sends another issuer on the redirect.
	WrongIss bool
	// TokenEndpoint replaces the token endpoint the metadata names.
	TokenEndpoint string
	// Deny answers the authorization with access_denied.
	Deny bool
	// AccessTTL is the CLI access token's lifetime in seconds (299),
	// ProductTTL the exchanged tokens' (300).
	AccessTTL  int
	ProductTTL int
	// Delay slows down refreshes and exchanges to widen race windows.
	Delay time.Duration
	// DeviceAnswers are the device polls' answers in order; after them
	// the request is approved.
	DeviceAnswers []string
	// RevokeStatus, when set, is what /oauth/revoke answers.
	RevokeStatus int
	// NoDefaultOrg answers the sign-in with default_organization null.
	NoDefaultOrg bool
	// Clock replaces time.Now (grace checks).
	Clock func() time.Time

	// LegacyRefresh is the one refresh token the older cookie refresh
	// accepts ("" for none).
	LegacyRefresh string

	// Counters.
	Authorizations  int
	CodeExchanges   int
	Refreshes       int
	Exchanges       int
	Upgrades        int
	UpgradeRefusals int
	Reuses          int
	// GraceHits counts refresh tokens presented again within ReuseGrace:
	// not a reuse, but in a race test a sign that two callers refreshed
	// with the same token.
	GraceHits       int
	Revocations     []url.Values
	SignOuts        []url.Values
	LegacyRefreshes int
	DevicePolls     int
	// AuthorizeQuery and DeviceCodeForm are the last requests' values.
	AuthorizeQuery url.Values
	DeviceCodeForm url.Values
	// RefreshTimes records when each refresh succeeded.
	RefreshTimes []time.Time
	// contains filtered or unexported fields
}

Server is the fake. Set the knobs before the first request (or under Lock for a running test).

func New

func New() *Server

New starts a fake Norcube Auth. Close it when done.

func (*Server) AddLegacySession

func (s *Server) AddLegacySession(refresh string)

AddLegacySession registers a refresh JWT of the older sign-in that has a session, so it can be upgraded (or refreshed the old way).

func (*Server) Counts

func (s *Server) Counts() (refreshes, exchanges, upgrades int)

Counts returns refreshes, exchanges and upgrades so far.

func (*Server) CurrentRefresh

func (s *Server) CurrentRefresh(g *Grant) string

CurrentRefresh is the grant's current refresh token.

func (*Server) ExpireAccess

func (s *Server) ExpireAccess(g *Grant)

ExpireAccess makes the server refuse g's CLI access tokens issued so far, as if they had expired early.

func (*Server) Grant

func (s *Server) Grant(refresh string) *Grant

Grant returns the grant a refresh token (current or rotated) belongs to.

func (*Server) KnowsScope

func (s *Server) KnowsScope(scope string) bool

KnowsScope reports whether the fake's catalog has the scope.

func (*Server) Lock

func (s *Server) Lock()

Lock and Unlock guard the knobs and counters while requests run.

func (*Server) MaxConcurrentExchanges

func (s *Server) MaxConcurrentExchanges() int

MaxConcurrentExchanges is the most token exchanges seen in flight at once.

func (*Server) NewGrant

func (s *Server) NewGrant(scopes ...string) *Grant

NewGrant creates a sign-in directly (as if a login had happened) and returns it. No scopes means full.

func (*Server) ProductToken

func (s *Server) ProductToken(tok string) (audience, org string, scopes []string, ok bool)

ProductToken describes a token the fake minted, for assertions.

func (*Server) RevokeGrant

func (s *Server) RevokeGrant(g *Grant)

RevokeGrant ends a sign-in (session revoked in the dashboard).

func (*Server) Revoked

func (s *Server) Revoked(g *Grant) bool

Revoked reports whether g ended.

func (*Server) Rotate

func (s *Server) Rotate(g *Grant) string

Rotate rotates g's refresh token as another process would.

func (*Server) ServeHTTP

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

func (*Server) Stats

func (s *Server) Stats() Stats

Stats returns the counters so far.

func (*Server) Unlock

func (s *Server) Unlock()

type Stats

type Stats struct {
	Authorizations, CodeExchanges, Refreshes, Exchanges, Upgrades, UpgradeRefusals int
	Reuses, GraceHits, LegacyRefreshes, DevicePolls                                int
	Revocations, SignOuts                                                          []url.Values
	AuthorizeQuery, DeviceCodeForm                                                 url.Values
	RefreshTimes                                                                   []time.Time
}

Stats is a copy of the counters and the last requests' values.

Jump to

Keyboard shortcuts

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