auth

package
v0.12.11 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: AGPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterAgentClient

func RegisterAgentClient(ctx context.Context, hydraAdminURL string, jwkData []byte, tlsCfg *tls.Config) error

RegisterAgentClient registers the clawker-agent OAuth2 client with Hydra via the admin API. Both clawker-cli and clawker-agent use the same public JWK (the CLI's signing key) — distinct client IDs keep the scope surface clean even though the signing key is shared. Idempotent: returns nil on 409 Conflict.

func RegisterCLIClient

func RegisterCLIClient(ctx context.Context, hydraAdminURL string, jwkData []byte, tlsCfg *tls.Config) error

RegisterCLIClient registers the clawker-cli OAuth2 client with Hydra via the admin API. The jwkData is the raw JSON of the CLI's public JWKS (bind-mounted from the host). Idempotent: returns nil if the client already exists (409 Conflict).

func WriteOryConfigs

func WriteOryConfigs(cp config.ControlPlaneSettings, hydraSecret string) error

WriteOryConfigs writes config files for Hydra, Kratos, and Oathkeeper to the config directory. Ports are read from ControlPlaneSettings. Called by the CP binary at startup before launching subprocesses. Idempotent — overwrites on every start so configs stay in sync with the binary version.

Types

type AuthInterceptor

type AuthInterceptor[S ~string] struct {
	// contains filtered or unexported fields
}

AuthInterceptor validates bearer tokens via an Introspector and enforces per-method scope requirements.

Flow: extract bearer token from gRPC metadata → introspect → check active + scope → optional client_id pin → allow or deny. Fail-closed on any error.

It is generic over the service's scope type S (a distinct named string type per service, e.g. adminv1.AdminScope, agentv1.AgentScope). Type inference binds S from the methodScopes map at construction, so an admin interceptor cannot be built from an agent scope map and vice versa — cross-service scope wiring fails to compile.

func NewAuthInterceptor

func NewAuthInterceptor[S ~string](introspector Introspector, methodScopes map[string]S, log *logger.Logger) *AuthInterceptor[S]

NewAuthInterceptor creates an interceptor that validates tokens via the given Introspector. methodScopes maps gRPC method names (e.g. "/clawker.admin.v1.AdminService/Install") to required OAuth2 scopes (e.g. adminv1.ScopeAdmin). S is inferred from the map.

func (*AuthInterceptor[S]) GRPCServerOptions

func (a *AuthInterceptor[S]) GRPCServerOptions() []grpc.ServerOption

GRPCServerOptions returns gRPC server options wired with both interceptors.

func (*AuthInterceptor[S]) RequireClientID

func (a *AuthInterceptor[S]) RequireClientID(clientID string) *AuthInterceptor[S]

RequireClientID pins the interceptor to a specific OAuth2 client_id. Defense-in-depth on top of scope: when set, any token whose introspection result reports a different client_id is rejected with codes.PermissionDenied even if the scope check passed. Used on the agent listener (clientID = consts.ClientIDAgent) so a future Hydra misconfiguration that grants agent:self:register to a non-agent client doesn't silently let the wrong client through. The admin listener leaves this empty — the admin scope is required on every admin RPC except the public GetSystemTime, and the CLI client_id is the only one Hydra is currently configured to grant that scope to. Returns the receiver for fluent chaining at construction.

func (*AuthInterceptor[S]) StreamInterceptor

func (a *AuthInterceptor[S]) StreamInterceptor() grpc.StreamServerInterceptor

StreamInterceptor returns a gRPC stream server interceptor.

func (*AuthInterceptor[S]) UnaryInterceptor

func (a *AuthInterceptor[S]) UnaryInterceptor() grpc.UnaryServerInterceptor

UnaryInterceptor returns a gRPC unary server interceptor.

type HydraIntrospector

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

HydraIntrospector implements Introspector by calling Hydra's admin introspection endpoint (POST /admin/oauth2/introspect, RFC 7662).

func NewHydraIntrospector

func NewHydraIntrospector(introspectURL string, tlsCfg *tls.Config) *HydraIntrospector

NewHydraIntrospector creates an introspector that validates tokens against the given Hydra admin introspection URL. The optional TLS config is used when Hydra serves HTTPS (self-signed cert).

func (*HydraIntrospector) Introspect

func (h *HydraIntrospector) Introspect(ctx context.Context, token, requiredScope string) (*IntrospectionResult, error)

type IntrospectionResult

type IntrospectionResult struct {
	Active   bool   `json:"active"`
	Scope    string `json:"scope"`
	ClientID string `json:"client_id"`
	Sub      string `json:"sub"`
	Exp      int64  `json:"exp"`
}

IntrospectionResult represents the relevant fields from an OAuth2 token introspection response (RFC 7662).

type Introspector

type Introspector interface {
	Introspect(ctx context.Context, token, requiredScope string) (*IntrospectionResult, error)
}

Introspector validates an OAuth2 bearer token and returns its introspection result. Implementations must be safe for concurrent use.

type OryStack

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

OryStack owns the control plane's Ory authentication choreography: Kratos (identity), Hydra (OAuth2 token issuance + introspection), and Oathkeeper (HTTP reverse-proxy auth for the future webui). It writes the Ory config files, starts each daemon as a managed subprocess, waits for them to become healthy, and registers the CLI and agent OAuth2 clients with Hydra.

The CA pool / TLS config built here (CATLS / CACertPool) is the SINGLE CA surface for the control plane — health checks, Hydra introspection, client registration, and gRPC mTLS client-cert verification all reuse it. The gRPC stack reuses these accessors rather than building a second pool.

subMgr is orchestrator-owned: OryStack uses it to start/health-check the Ory daemons but never owns its lifecycle (shutdown/drain stay with the orchestrator).

Every gate returns an error (fail-closed). OryStack never panics, os.Exit's, or log.Fatal's — the orchestrator decides what a startup-gate failure means.

func NewOryStack

func NewOryStack(cfg config.Config, subMgr *subprocess.SubprocessManager, caCertPath, jwkPath string, log *logger.Logger) (*OryStack, error)

NewOryStack constructs the Ory auth stack and builds the single CP CA pool / TLS config from caCertPath up front, so CATLS / CACertPool are available to the orchestrator (and the gRPC stack) before Start runs. It does NOT start any subprocess — call Start for that.

Returns an error (never panics) if the CA cert cannot be read or parsed.

func (*OryStack) CACertPool

func (s *OryStack) CACertPool() *x509.CertPool

CACertPool returns the single CP CA cert pool. Reused for mTLS client-cert verification by the gRPC stack.

func (*OryStack) CATLS

func (s *OryStack) CATLS() *tls.Config

CATLS returns the single CP CA TLS config (RootCAs pinned to the CLI CA, ServerName the CP container name, TLS 1.3 floor). Reused by the orchestrator (aggregate health probes) and the gRPC stack — never build a second pool.

func (*OryStack) Start

func (s *OryStack) Start(ctx context.Context) error

Start runs the Ory bringup choreography, fail-closed: ensure the Hydra secret, write the Ory configs, start Kratos + Hydra, wait for all three listeners (Kratos public, Hydra public, Hydra admin) to report healthy, register the CLI + agent OAuth2 clients, then start Oathkeeper. Any failure returns an error; the orchestrator treats this as a pre-SetReady startup gate.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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