auth

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const GoogleJWKSURL = "https://www.googleapis.com/oauth2/v3/certs"

GoogleJWKSURL serves Google's RS256 public keys for ID tokens.

View Source
const LegacyAdminID = "legacy:admin"

LegacyAdminID is the sentinel user_id assigned when a request authenticates with the deprecated CONTEXO_API_KEY shared secret. Handlers may use this to bypass per-user membership checks while phase-1 ships ahead of the CLI and dashboard rewrites.

View Source
const SessionIssuer = "contexo"

SessionIssuer identifies sessions minted by this server (claim "iss").

View Source
const SessionTTL = 30 * 24 * time.Hour

SessionTTL is how long a session JWT stays valid after mint.

Variables

View Source
var GoogleIssuers = map[string]bool{
	"https://accounts.google.com": true,
	"accounts.google.com":         true,
}

GoogleIssuers are the values Google may set as `iss` on ID tokens.

Functions

func GenerateAPIKey

func GenerateAPIKey() (string, error)

GenerateAPIKey produces a 32-byte random key encoded as base64url.

func GinMiddleware

func GinMiddleware(validate KeyValidator) gin.HandlerFunc

GinMiddleware returns Gin middleware that validates API keys.

func HashKey

func HashKey(key string) string

HashKey computes the SHA-256 hash of an API key for storage.

func IsLegacy

func IsLegacy(userID string) bool

IsLegacy reports whether userID refers to the legacy shared-key auth path.

func ValidateKey

func ValidateKey(rawKey, storedHash string) bool

ValidateKey checks if a raw key matches its stored hash using constant-time comparison.

Types

type GoogleClaims

type GoogleClaims struct {
	Email         string `json:"email"`
	EmailVerified bool   `json:"email_verified"`
	Name          string `json:"name"`
	Subject       string `json:"sub"`
}

GoogleClaims is the subset of an ID token we care about.

type GoogleVerifier

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

GoogleVerifier verifies Google ID tokens for a configured OAuth client.

func NewGoogleVerifier

func NewGoogleVerifier(clientID string) *GoogleVerifier

NewGoogleVerifier constructs a verifier for the given OAuth Client ID.

func (*GoogleVerifier) Verify

func (v *GoogleVerifier) Verify(idToken string) (*GoogleClaims, error)

Verify parses idToken, checks the signature against Google's JWKS, and validates issuer/audience/expiry/email_verified. Returns the trusted claims.

type KeyValidator

type KeyValidator func(key string) (userID string, valid bool)

KeyValidator is a function that checks if a key is valid and returns the user ID.

type Resolver

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

Resolver turns a Bearer token into a user_id. It accepts three flavors:

  • Session JWT (HS256, issued by SessionSigner) — for dashboard sessions
  • Personal Access Token (prefix "ctxp_") — for CLI/MCP
  • Legacy API key (matches CONTEXO_API_KEY env var) — for back-compat

func NewResolver

func NewResolver(session *SessionSigner, users *userstore.Store, legacyKey string) *Resolver

NewResolver constructs a Resolver. Any of session/users may be nil to disable a flavor. legacyKey may be "" to disable legacy auth.

func (*Resolver) Resolve

func (r *Resolver) Resolve(token string) (string, error)

Resolve attempts to extract a user_id from the bearer token.

func (*Resolver) Validator

func (r *Resolver) Validator() KeyValidator

Validator returns a KeyValidator suitable for GinMiddleware.

type SessionClaims

type SessionClaims struct {
	UserID string `json:"sub"`
	Email  string `json:"email"`
	jwt.RegisteredClaims
}

SessionClaims are the contents of a session JWT minted by MintSession.

type SessionSigner

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

SessionSigner mints and verifies HS256 session JWTs using a shared secret.

func NewSessionSigner

func NewSessionSigner(secret string) (*SessionSigner, error)

NewSessionSigner constructs a signer from a server secret.

func (*SessionSigner) Mint

func (s *SessionSigner) Mint(userID, email string) (string, time.Time, error)

Mint creates a session JWT for the user.

func (*SessionSigner) Verify

func (s *SessionSigner) Verify(raw string) (userID, email string, err error)

Verify parses and validates a session JWT, returning the user id and email.

type Verifier

type Verifier interface {
	Verify(idToken string) (*GoogleClaims, error)
}

Verifier produces trusted GoogleClaims from a raw ID token. The production implementation is *GoogleVerifier; tests can substitute a fake.

Jump to

Keyboard shortcuts

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