authz

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package authz calls auth-api's POST /authz/check to verify a caller's bearer token and resolve their roles, and provides Gin middleware that gates a route on holding one of a set of allowed roles. See platform's volume-edit-authorization spec (openspec/changes/volume-edit-with-approval-workflow in sweetrpg/platform).

Index

Constants

View Source
const (
	RoleUser      = "user"
	RoleSubmitter = "submitter"
	RoleEditor    = "editor"
	RoleModerator = "moderator"
	RoleApprover  = "approver"
	RoleAdmin     = "admin"
)

Platform role names, matching auth-api's fixed role model exactly (see openspec/specs/user-authorization/spec.md's "Role model" requirement).

Variables

This section is empty.

Functions

func HasRole

func HasRole(roles []string, want string) bool

HasRole reports whether roles contains want.

func RequireAnyRole

func RequireAnyRole(client *Client, service string, allowedRoles ...string) gin.HandlerFunc

RequireAnyRole returns Gin middleware that verifies the caller's bearer token against auth-api's /authz/check for the given service name, then requires the caller hold at least one of allowedRoles. On success, the verified roles and subject are stashed in the Gin context (read via Roles(c) / Subject(c)) for the handler to use.

func Roles

func Roles(c *gin.Context) []string

Roles returns the verified roles stashed in the Gin context by RequireAnyRole.

func Subject

func Subject(c *gin.Context) string

Subject returns the verified Auth0 subject stashed in the Gin context by RequireAnyRole.

Types

type CheckResponse

type CheckResponse struct {
	Allowed bool     `json:"allowed"`
	Roles   []string `json:"roles"`
	Sub     string   `json:"sub"`
	Reason  string   `json:"reason"`
}

CheckResponse is the union of auth-api's allowed/denied /authz/check response shapes.

type Client

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

Client calls auth-api's /authz/check endpoint.

func NewClient

func NewClient(baseURL string) *Client

NewClient builds a Client against auth-api's base URL (e.g. http://api-v1.sweetrpg-auth.svc.cluster.local:8000). An empty baseURL is accepted so the service can still start when AUTH_API_URL isn't configured; every Check call will then fail with a transport error, which RequireAnyRole surfaces as a 503.

func (*Client) Check

func (c *Client) Check(ctx context.Context, token, service string) (*CheckResponse, error)

Check verifies token against auth-api and returns the caller's allowed/roles/subject for the given service name. Returns InvalidTokenError if auth-api rejects the token itself.

type InvalidTokenError

type InvalidTokenError struct{}

InvalidTokenError means auth-api rejected the bearer token itself (missing, expired, unverifiable) - distinct from a service-level deny (Allowed: false with a Reason) or a transport/backend failure.

func (InvalidTokenError) Error

func (InvalidTokenError) Error() string

Jump to

Keyboard shortcuts

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