Documentation
¶
Overview ¶
Package session is turnd's own login: an OpenID Connect login through go-authn/rp in a popup window, and a short-lived bearer token turnd signs itself, which the playground then presents to the credential and rendezvous endpoints.
The token is "t1.<payload>.<mac>" (both base64url): payload is the expiry (8 bytes, big endian Unix seconds), a 16-byte random nonce, then the subject; mac is HMAC-SHA256 over a domain-separation label and the payload, under a server key that never leaves turnd. It is compared in constant time and is refused when expired or altered.
Index ¶
- Constants
- Variables
- func Bearer(r *http.Request) (string, bool)
- func Subject(issuer, sub string) string
- func ValidSubject(s string) bool
- type Auth
- type Claims
- type Family
- type Login
- type LoginConfig
- type Refresher
- type RenewConfig
- type Renewer
- type Signer
- func (s *Signer) Issue(subject string, now time.Time) (token string, expiry time.Time, err error)
- func (s *Signer) IssueRenewable(subject string, fam Family, now time.Time) (token string, expiry time.Time, err error)
- func (s *Signer) TTL() time.Duration
- func (s *Signer) Verify(token string, now time.Time) (Claims, error)
- func (s *Signer) VerifyGrace(token string, now time.Time, grace time.Duration) (Claims, error)
Constants ¶
const ( // MinKeyLen is the shortest session key accepted (the HMAC-SHA256 output size). MinKeyLen = 32 // MaxSubjectLen bounds the subject of a token. MaxSubjectLen = 128 // MaxTokenLen bounds a token Verify reads; anything longer is refused unread. MaxTokenLen = 512 )
Variables ¶
var ( // ErrInvalid is a token that is malformed, signed by no key of ours, or altered. ErrInvalid = errors.New("session: invalid token") // ErrExpired is a genuine token whose time is over. ErrExpired = errors.New("session: token expired") )
var ErrEnded = errors.New("session: cannot be renewed, sign in again")
ErrEnded is the one refusal Renewer.Renew gives for a session that cannot be renewed, whatever the cause: the person must sign in again.
ErrUnauthorized is the one refusal Subject gives, whatever the cause: a client that sent a bad token is not owed which check caught it.
ErrUnavailable is a renewal that could not be tried or finished because the identity provider did not answer. The session is intact: retry.
Functions ¶
func Subject ¶
Subject is the opaque, stable identifier turnd uses for a person: a hash of issuer and "sub", never a name. It ends up in TURN usernames, which an eavesdropper sees (draft-uberti-behave-turn-rest-00 §6).
func ValidSubject ¶
ValidSubject says whether s can be a token's subject: non-empty, UTF-8, at most MaxSubjectLen bytes, no control or space characters.
Types ¶
type Auth ¶
type Auth struct {
Signer *Signer
// AccessTokens, when non-nil, accepts issuer access tokens. Build it with
// oidc.Config{Type: "at+jwt"} so an ID token is never taken for one.
AccessTokens *oidc.Verifier
Now func() time.Time
// Logf receives the real reason for a refusal (never sent to the client).
Logf func(ctx context.Context, format string, args ...any)
// Observe receives the reason of every refusal as one fixed word:
// "session_token" or "access_token" (a token that failed verification),
// "no_access_verifier" (an issuer token with no verifier configured),
// "no_subject". Nil observes nothing.
Observe func(ctx context.Context, reason string)
}
Auth authenticates an API request from its Authorization: Bearer header: a turnd session token, or (when AccessTokens is set) an access token of the configured issuer, verified by go-authn/oidc.
func (*Auth) Authenticate ¶
Authenticate returns the opaque subject of a token: a turnd session token or an issuer access token. It is what a gRPC service calls with the token of its "authorization: Bearer" metadata.
type Claims ¶
type Claims struct {
Subject string
Expiry time.Time
Nonce [nonceN]byte
// Family is the renewal family; the zero value for a token that predates
// renewal or was issued without one.
Family Family
}
Claims is what a verified token says.
type Family ¶ added in v0.5.0
type Family [familyN]byte
Family names the renewal family of a session: a random identifier under which turnd keeps the identity provider's refresh token server side. It is not a secret of its own (it only means something inside a token turnd signed).
type Login ¶
type Login struct {
// contains filtered or unexported fields
}
Login serves /login and /callback.
type LoginConfig ¶
type LoginConfig struct {
Client *rp.Client
Signer *Signer
// Origins is the allowlist of origins ("https://go-tex.github.io") that may
// open /login and receive the token by postMessage. Compared exactly; the
// popup's return target comes from here and nowhere else.
Origins []string
// Secure marks the flow cookie Secure (set it when the public URL is https).
Secure bool
// FlowTTL is how long a login may take (default 10 minutes); MaxFlows bounds
// the logins in progress (default 4096).
FlowTTL time.Duration
MaxFlows int
Now func() time.Time
// Renewer, when set, keeps the provider's refresh token of each login
// server side and issues renewable session tokens. A login that comes back
// without a refresh token still gets a session, one that cannot be renewed.
Renewer *Renewer
// Logf receives one line per callback outcome: the pseudonymous subject
// on success, the reason on a refusal, never a code, token or cookie.
// Without it the operator cannot tell a login that worked from one that
// failed: the popup shows the same "You can close this window" for both
// when it has no opener. Nil logs nothing.
Logf func(ctx context.Context, format string, args ...any)
// Observe receives the outcome of every callback and of every /login
// start, as one fixed word: "issued", "unknown_flow", "no_cookie",
// "cookie_mismatch", "provider_error", "issuer_check", "exchange_failed",
// "issue_failed" (callback); "started", "origin_refused", "too_many_flows",
// "internal_error" (login). ctx is the request's, so the span in it can be
// annotated. Nil observes nothing.
Observe func(ctx context.Context, outcome string)
}
LoginConfig configures NewLogin.
type Refresher ¶ added in v0.5.0
type Refresher interface {
Refresh(ctx context.Context, refreshToken string, f rp.For) (*rp.Token, error)
}
Refresher is the part of go-authn/rp's *rp.Client a renewal needs.
type RenewConfig ¶ added in v0.5.0
type RenewConfig struct {
Refresher Refresher
Signer *Signer
// Lifetime is how long a family lives from the login: the identity
// provider's refresh lifetime (bridge: the client's refresh_lifetime, fixed
// at the login, not extended by rotation). Default 168 h.
Lifetime time.Duration
// Grace is how long after its expiry a session token can still be renewed
// (default 5 minutes).
Grace time.Duration
// MaxFamilies bounds the store (default 10000). When it is full the family
// closest to its end is dropped to make room: its person signs in again.
MaxFamilies int
Now func() time.Time
// Logf receives one line per outcome: the pseudonymous subject, never a
// token or a family identifier.
Logf func(format string, args ...any)
}
RenewConfig configures NewRenewer.
type Renewer ¶ added in v0.5.0
type Renewer struct {
// contains filtered or unexported fields
}
Renewer keeps refresh tokens server side, in memory, keyed by family, and renews session tokens with them. A restart empties it: everybody signs in again.
func NewRenewer ¶ added in v0.5.0
func NewRenewer(cfg RenewConfig) (*Renewer, error)
NewRenewer validates cfg.
func (*Renewer) Open ¶ added in v0.5.0
Open starts a family for a login that came with a refresh token and returns its identifier, to embed in the session token (Signer.IssueRenewable).
func (*Renewer) Renew ¶ added in v0.5.0
Renew trades a session token, valid or expired for less than the grace, for a new one. Any refusal from the provider ends the family (ErrEnded); a failure to reach it leaves it (ErrUnavailable).
type Signer ¶
type Signer struct {
// contains filtered or unexported fields
}
Signer issues and verifies tokens. Issue signs with the first key; Verify accepts any of them, so a key can be rotated without logging everybody out.
func NewSigner ¶
NewSigner is a signer with the given lifetime for tokens it issues. A key shorter than MinKeyLen, a non-positive ttl or a duplicate key is refused.
func (*Signer) IssueRenewable ¶ added in v0.5.0
func (s *Signer) IssueRenewable(subject string, fam Family, now time.Time) (token string, expiry time.Time, err error)
IssueRenewable is Signer.Issue for a session that can be renewed: the token carries fam, which turnd's renewer maps to the stored refresh token.