{"modulePath":"github.com/asteroid-computing/go-lambda-edge","version":"v0.1.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package authn\n\nPackage authn selects one configured Bearer or EdgeIAM verifier from an HTTP Authorization header and transports the verified caller to protected handlers. [NewCognitoVerifier](#NewCognitoVerifier) provides local RS256 Cognito access-token verification with direct JSON v2 and bounded JWKS caching. It does not check token revocation, authorize actions, or infer trust from a gateway, a TLS field, an action header or forwarded headers.\n\nUse New to configure explicit verifier functions. Authenticate supports custom HTTP pipelines without writing responses or installing context. Handler adds required-authentication middleware. Public routes omit that middleware; CORS and preflight policy belong to the gateway or outer middleware.\n\nVerifiers must authenticate credentials before returning a caller and classify rejection with [ErrInvalidCredentials](#ErrInvalidCredentials). Adapt iamproof.ErrInvalidProof explicitly when wiring an IAM proof verifier; this package does not import AWS packages. Never retry another verifier after rejection. Authenticate and authorize the selected action before committing a response or starting a stream.\n\n## Variables\n\n```go\nvar (\n\t// ErrInvalidConfiguration means a constructor rejected its configuration, or a method was called on a nil or zero value or with a nil context.\n\t// It is returned directly, not wrapped in an [*Error].\n\tErrInvalidConfiguration = errors.New(\"authn: invalid configuration\")\n\n\t// ErrMissingCredentials means the request has no Authorization field (401 with all configured challenges).\n\tErrMissingCredentials = errors.New(\"authn: missing credentials\")\n\n\t// ErrMalformedCredentials means the Authorization field is repeated, comma-combined, empty or not valid scheme framing (400).\n\tErrMalformedCredentials = errors.New(\"authn: malformed credentials\")\n\n\t// ErrUnsupportedScheme means the credential uses a well-formed scheme that is not configured (401 with all configured challenges).\n\tErrUnsupportedScheme = errors.New(\"authn: unsupported scheme\")\n\n\t// ErrHeaderTooLarge means the Authorization field exceeds [Config.MaxAuthorizationBytes] (431, without a challenge).\n\tErrHeaderTooLarge = errors.New(\"authn: authorization header too large\")\n\n\t// ErrInvalidCredentials means the selected verifier definitely rejected the credential (401 with that scheme's challenge).\n\t// A [VerifyFunc] returns it to classify a rejection.\n\tErrInvalidCredentials = errors.New(\"authn: invalid credentials\")\n\n\t// ErrUnavailable means verification could not complete, for example because a dependency failed or a verifier returned an unclassified error (503, without a challenge).\n\tErrUnavailable = errors.New(\"authn: verification unavailable\")\n\n\t// ErrVerifierContract means a verifier reported success with an unacceptable caller, such as the wrong kind or source (500).\n\t// It indicates a composition fault, not a client error.\n\tErrVerifierContract = errors.New(\"authn: invalid verifier result\")\n)\n```\nInspectable failure categories never contain credentials or provider errors. Match them with [errors.Is](/errors#Is). Request-time failures are wrapped in an [\\*Error](#Error) whose [Error.StatusCode](#Error.StatusCode) is noted below.\n\n## Types\n\n```go\ntype Authenticator struct {\n\t// contains filtered or unexported fields\n}\n```\nAuthenticator is an immutable selector configured by New. Its zero value is unconfigured. Shared use requires concurrency-safe verifier functions.\n\n```go\nfunc New(cfg Config) (*Authenticator, error)\n```\nNew validates and copies cfg without I/O or invoking a verifier. It returns a nil Authenticator and [ErrInvalidConfiguration](#ErrInvalidConfiguration) for invalid configuration.\n\n```go\nfunc (a *Authenticator) Authenticate(ctx context.Context, headers http.Header) (identity.Caller, error)\n```\nAuthenticate selects at most one verifier and returns a verified caller. It neither modifies headers nor installs context. Callers must not mutate headers concurrently. Every failure returns an anonymous zero caller.\n\nAn established caller fails with [identity.ErrConflict](/github.com/asteroid-computing/go-lambda-edge/identity#ErrConflict) before credential work. Runtime failures are \\*Error with sanitized categories; caller cancellation returns ctx.Err directly. Nil contexts and nil/zero Authenticators return [ErrInvalidConfiguration](#ErrInvalidConfiguration). No provider diagnostic or context cause is exposed.\n\n```go\nfunc (a *Authenticator) Handler(next http.Handler) (http.Handler, error)\n```\nHandler constructs required-authentication middleware. It validates next, including typed nils. Success installs one caller in a derived request context and invokes next exactly once. It leaves the original request and headers intact.\n\nFailure writes a generic text response, no-store and the applicable challenges; next is not invoked. Caller cancellation attempts 503 without a challenge, but delivery is not guaranteed. Provider/handler panics propagate. Public routes and CORS/preflight must be configured separately; there is no OPTIONS bypass.\n\n```go\ntype CognitoClient struct {\n\tClientID     string\n\tAudience     string\n\tAllowUnbound bool\n}\n```\nCognitoClient restricts one app client independently of its resource audience. Audience requires an exact string aud. AllowUnbound additionally permits an absent aud; with an empty Audience it permits only absent aud. At least one of Audience or AllowUnbound is required. No policy accepts an arbitrary aud.\n\n```go\ntype CognitoConfig struct {\n\t// Issuer is an exact HTTPS pool URL, without trailing slash, escaped path, dot segments, credentials, query or fragment.\n\t// JWKS is fetched from this trusted endpoint plus /.well-known/jwks.json, never from token headers.\n\tIssuer string\n\t// Clients must contain at least one policy with a distinct nonempty ClientID.\n\tClients []CognitoClient\n\t// ClockSkew permits 0..5 minutes of leeway.\n\t// Zero does not extend token life.\n\tClockSkew time.Duration\n\t// CacheTTL defaults to 15 minutes;\n\t// explicit values allow 1 minute..24 hours.\n\t// Expired keys are not used during outages.\n\t// No token results are cached.\n\tCacheTTL time.Duration\n\t// Timeout defaults to 5 seconds;\n\t// explicit values must be positive and \u003c=1 minute.\n\tTimeout time.Duration\n\t// Transport defaults to http.DefaultTransport.\n\t// A supplied transport remains caller-owned, must support concurrency and must honor context cancellation.\n\tTransport http.RoundTripper\n\t// MaxTokenBytes defaults to 16 KiB;\n\t// explicit values allow 1 byte..1 MiB.\n\t// The separate Authorization field bound also includes the scheme/framing.\n\tMaxTokenBytes int\n\t// ClaimsBudget defaults to identity's 256 KiB weighted allowance.\n\t// Explicit values allow 1 byte..6 MiB, independently of the compact token byte limit.\n\tClaimsBudget int\n}\n```\nCognitoConfig supplies explicit trust and resource policy for access tokens.\n\n```go\ntype CognitoVerifier struct {\n\t// contains filtered or unexported fields\n}\n```\nCognitoVerifier verifies a narrow RS256 Cognito access-token profile using JSON v2 and standard-library cryptography. It does not check revocation or grant permissions. It is safe for concurrent use, but must not be copied. Its zero value is unconfigured. Reuse one instance to share its bounded cache.\n\n```go\nfunc NewCognitoVerifier(cfg CognitoConfig) (*CognitoVerifier, error)\n```\nNewCognitoVerifier validates and copies cfg without I/O. Invalid configuration returns a nil verifier and [ErrInvalidConfiguration](#ErrInvalidConfiguration). It trusts the configured issuer endpoint; it does not establish that the operator supplied an AWS URL.\n\n```go\nfunc (v *CognitoVerifier) Verify(ctx context.Context, token string) (identity.Caller, error)\n```\nVerify returns one locally verified caller without installing it in context. It accepts access tokens only, with integer exp/iat and optional nbf, and a string aud when present. Rejections match [ErrInvalidCredentials](#ErrInvalidCredentials); unavailable JWKS matches [ErrUnavailable](#ErrUnavailable). Caller cancellation returns ctx.Err, never its cause. Every failure returns a zero caller and sanitized diagnostics.\n\n```go\nfunc (v *CognitoVerifier) Warm(ctx context.Context) error\n```\nWarm obtains a fresh usable JWKS snapshot, sharing any required fetch. It obeys refresh/backoff gates; it does not force refresh, validate a token or start a background refresh loop. Failures are [ErrUnavailable](#ErrUnavailable), caller ctx.Err or [ErrInvalidConfiguration](#ErrInvalidConfiguration) for a nil context or unconfigured verifier.\n\n```go\ntype Config struct {\n\t// Bearer and IAMProof are independently optional;\n\t// at least one is required.\n\t// Each function must be safe for concurrent use when sharing an Authenticator.\n\tBearer   VerifyFunc\n\tIAMProof VerifyFunc\n\t// Realm is a challenge label, not a proof audience or issuer restriction.\n\t// Empty defaults to \"edge\";\n\t// explicit values allow 1-256 printable ASCII bytes.\n\tRealm string\n\t// MaxAuthorizationBytes bounds represented Authorization value bytes before trimming.\n\t// Zero defaults to 16 KiB;\n\t// explicit values must be in 1..1 MiB.\n\t// This neither changes iamproof's 8 KiB proof limit nor bounds all headers.\n\tMaxAuthorizationBytes int\n}\n```\nConfig supplies explicit verifier dependencies and HTTP framing policy.\n\n```go\ntype Error struct {\n\t// contains filtered or unexported fields\n}\n```\nError describes a sanitized HTTP authentication failure. Inspect its category with errors.Is and its HTTP treatment with errors.AsType\\[\\*Error]. Its private state never retains a supplied credential or an underlying provider error. The zero value describes an internal failure, without a challenge.\n\n```go\nfunc (e *Error) Challenges() []string\n```\nChallenges returns an owned copy of complete WWW-Authenticate values.\n\n```go\nfunc (e *Error) Error() string\n```\nError returns a diagnostic containing only the sanitized category.\n\n```go\nfunc (e *Error) GoString() string\n```\nGoString returns the same sanitized diagnostic used by [Error.Error](#Error.Error).\n\n```go\nfunc (e *Error) StatusCode() int\n```\nStatusCode returns the recommended HTTP status, or 500 for a zero [Error](#Error).\n\n```go\nfunc (e *Error) Unwrap() error\n```\nUnwrap exposes only a package category or [identity.ErrConflict](/github.com/asteroid-computing/go-lambda-edge/identity#ErrConflict).\n\n```go\ntype VerifyFunc func(ctx context.Context, credential string) (identity.Caller, error)\n```\nVerifyFunc authenticates a credential without its HTTP scheme. It must return [ErrInvalidCredentials](#ErrInvalidCredentials) for a definite rejection; unknown errors are treated as unavailable. Success requires JWT/SourceLocallyVerifiedToken for Bearer or IAM/SourceVerifiedIAMProof for EdgeIAM. Constructors alone do not verify tokens.\n\n","path":"github.com/asteroid-computing/go-lambda-edge/authn","name":"authn","synopsis":"Package authn selects one configured Bearer or EdgeIAM verifier from an HTTP Authorization header and transports the verified caller to protected handlers.","isRedistributable":true}
