authz

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 3 Imported by: 7

Documentation

Overview

Package authz defines the Authorizer contract that every generated command handler invokes before its pipeline runs.

The protosource plugin stamps a canonical function name ({proto_package}. {CommandMessageName}, e.g. "example.app.sample.v1.Create") into each generated handler at code-generation time. At request time the handler calls Authorizer.Authorize with that name, letting the implementation decide whether the caller is allowed to proceed.

The framework ships [allowall.Authorizer] as the default binding so that generated code compiles and runs out of the box with no real authorization wired up. Production deployments override that binding with a concrete implementation — for example, the shadow-token authorizer published by the protosource-auth project.

Index

Constants

This section is empty.

Variables

View Source
var ErrForbidden = errors.New("authz: forbidden")

ErrForbidden indicates that the caller was identified but does not hold the required function. Generated handlers map this to HTTP 403.

View Source
var ErrUnauthenticated = errors.New("authz: unauthenticated")

ErrUnauthenticated indicates that the caller could not be identified — missing, expired, or malformed credentials. Generated handlers map this to HTTP 401.

Functions

func JWTFromContext

func JWTFromContext(ctx context.Context) string

JWTFromContext returns the JWT stashed by an Authorizer via WithJWT, or "" if none is present.

func UserIDFromContext

func UserIDFromContext(ctx context.Context) string

UserIDFromContext returns the user id stashed by an Authorizer via WithUserID, or "" if none is present.

func WithJWT

func WithJWT(ctx context.Context, jwt string) context.Context

WithJWT returns a child context carrying a forwarded JWT. Shadow-token authorizers that dereference opaque tokens to real JWTs use this so downstream handlers can reuse the JWT for outbound service calls.

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID returns a child context carrying the authenticated user id.

Types

type Authorizer

type Authorizer interface {
	Authorize(ctx context.Context, request protosource.Request, requiredFunction string) (context.Context, error)
}

Authorizer gates every generated command handler.

Implementations inspect the incoming request (cookies, authorization header, etc.) to determine the caller's identity, verify that the caller holds requiredFunction, and optionally enrich the returned context with identity facts (WithUserID, WithJWT) that downstream handler code can read.

The requiredFunction argument is the canonical function name for the command being invoked. By convention it is "{proto_package}.{MessageName}" (e.g. "example.app.sample.v1.Create"). The protosource plugin generates this string at compile time — callers never construct it.

Error semantics mapped by generated handlers:

  • Returning ErrUnauthenticated yields HTTP 401.
  • Returning ErrForbidden yields HTTP 403.
  • Any other non-nil error is treated as ErrForbidden for conservative safety — implementations should wrap their internal errors in one of the typed sentinels above when they want a specific status code.

Implementations should be safe for concurrent use.

Directories

Path Synopsis
Package allowall provides a no-op authz.Authorizer that permits every request.
Package allowall provides a no-op authz.Authorizer that permits every request.

Jump to

Keyboard shortcuts

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