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
- type Grant
- type Server
- func (s *Server) AddLegacySession(refresh string)
- func (s *Server) Counts() (refreshes, exchanges, upgrades int)
- func (s *Server) CurrentRefresh(g *Grant) string
- func (s *Server) ExpireAccess(g *Grant)
- func (s *Server) Grant(refresh string) *Grant
- func (s *Server) KnowsScope(scope string) bool
- func (s *Server) Lock()
- func (s *Server) MaxConcurrentExchanges() int
- func (s *Server) NewGrant(scopes ...string) *Grant
- func (s *Server) ProductToken(tok string) (audience, org string, scopes []string, ok bool)
- func (s *Server) RevokeGrant(g *Grant)
- func (s *Server) Revoked(g *Grant) bool
- func (s *Server) Rotate(g *Grant) string
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Stats() Stats
- func (s *Server) Unlock()
- type Stats
Constants ¶
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.
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 (*Server) AddLegacySession ¶
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) CurrentRefresh ¶
CurrentRefresh is the grant's current refresh token.
func (*Server) ExpireAccess ¶
ExpireAccess makes the server refuse g's CLI access tokens issued so far, as if they had expired early.
func (*Server) KnowsScope ¶
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 ¶
MaxConcurrentExchanges is the most token exchanges seen in flight at once.
func (*Server) NewGrant ¶
NewGrant creates a sign-in directly (as if a login had happened) and returns it. No scopes means full.
func (*Server) ProductToken ¶
ProductToken describes a token the fake minted, for assertions.
func (*Server) RevokeGrant ¶
RevokeGrant ends a sign-in (session revoked in the dashboard).
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.