oidc

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package oidc verifies Forgejo Actions ID tokens so an xyOps deploy job can trust "this is john/ops, push to main, run N, commit X" without CI holding any xyOps credential.

Forgejo signs ID tokens with RS256 and publishes its keys at <issuer>/.well-known/keys (issuer = <forgejo>/api/actions). Facts from the Forgejo 17 source that shape this package:

  • ref_protected is always "false" (services/actions/context.go), so it proves nothing. Branch protection must be checked separately.
  • Tokens live ID_TOKEN_EXPIRATION_TIME seconds (default 3600) and carry no jti. Replay is stopped by requiring a run newer than the last deployed one (see Newer), not by expiry.
  • Claims are strings, including run_id and repository_id.

Only the Go standard library is used.

Index

Constants

View Source
const Leeway = 60 * time.Second

Leeway allowed for clock differences between Forgejo and the deploy host.

Variables

This section is empty.

Functions

func UnverifiedAudience

func UnverifiedAudience(token string) (string, error)

UnverifiedAudience returns the token's aud claim WITHOUT checking anything. Use it only to choose which policy to verify against (e.g. which app slot); never act on it before Verify succeeds with that policy.

Types

type Claims

type Claims struct {
	Issuer            string `json:"iss"`
	Subject           string `json:"sub"`
	Audience          string `json:"aud"`
	Expiry            int64  `json:"exp"`
	IssuedAt          int64  `json:"iat"`
	NotBefore         int64  `json:"nbf"`
	Actor             string `json:"actor"`
	EventName         string `json:"event_name"`
	Ref               string `json:"ref"`
	RefType           string `json:"ref_type"`
	Repository        string `json:"repository"`
	RepositoryID      string `json:"repository_id"`
	RepositoryOwner   string `json:"repository_owner"`
	RepositoryOwnerID string `json:"repository_owner_id"`
	RunID             string `json:"run_id"`
	RunNumber         string `json:"run_number"`
	RunAttempt        string `json:"run_attempt"`
	SHA               string `json:"sha"`
	Workflow          string `json:"workflow"`
	WorkflowRef       string `json:"workflow_ref"`
}

Claims are the verified token claims a deploy job may rely on.

func Verify

func Verify(token string, keys KeySet, p Policy, now time.Time) (*Claims, error)

Verify checks the token's signature against keys, then every claim against the policy, at time now. It returns the claims only if all checks pass.

func VerifyWorkflow

func VerifyWorkflow(token string, keys KeySet, p Policy, now time.Time, workflow string) (*Claims, error)

VerifyWorkflow is Verify for deploys from any allowed ref: instead of a fixed list of workflow_ref values, the token's workflow_ref must be exactly "<workflow>@<the token's own ref>", where workflow is "<owner>/<repo>/<path>". p.WorkflowRefs must be ["*"] to make that explicit.

type Deployed

type Deployed struct {
	RunID      int64  `json:"run_id"`
	RunAttempt int64  `json:"run_attempt"`
	SHA        string `json:"sha"`
}

Deployed records the last run that deployed, kept by the deploy job.

func Newer

func Newer(last Deployed, c *Claims) (Deployed, error)

Newer refuses a token whose run is not strictly newer than the last deployed run (a re-run of the same run counts only with a higher attempt). This stops replaying an old, still-valid token to roll a stack back.

type KeySet

type KeySet map[string]*rsa.PublicKey

KeySet holds RSA verification keys by key ID.

func FetchKeySet

func FetchKeySet(issuer string, client *http.Client) (KeySet, error)

FetchKeySet downloads the issuer's discovery document and its JWKS. The jwks_uri must be HTTPS on the issuer's own host; redirects are refused.

func ParseKeySet

func ParseKeySet(data []byte) (KeySet, error)

ParseKeySet parses a JWKS document, keeping only RSA signing keys that are RS256 (or unlabelled) and at least 2048 bits.

type Policy

type Policy struct {
	// Issuer is "<forgejo>/api/actions", e.g. "https://code.yawn.dev/api/actions".
	Issuer string
	// Audience must be requested explicitly by the workflow (never the default,
	// which is just the owner's URL and is shared by every repo of that owner).
	Audience string
	// Audiences, if set, replaces Audience with a set of accepted values. A
	// deploy job can then bind its mode to the token (e.g. "x:plan" vs
	// "x:apply") and read the verified Claims.Audience instead of a caller
	// parameter.
	Audiences []string
	// RepositoryID and RepositoryOwnerID pin the repository by its immutable
	// IDs, so a deleted-and-recreated or renamed repository does not match.
	RepositoryID      string
	RepositoryOwnerID string
	// Refs are the exact branch refs allowed to deploy, e.g. ["refs/heads/main"].
	Refs []string
	// TagPrefixes, if set, also allow tag refs starting with one of these
	// prefixes, e.g. ["refs/tags/v"] for release deploys. Each prefix must
	// start with "refs/tags/". Branches still need Refs.
	TagPrefixes []string
	// Events are the allowed triggers, e.g. ["push"]. pull_request is refused
	// regardless of this list.
	Events []string
	// WorkflowRefs are the exact allowed workflow_ref values, e.g.
	// ["john/ops/.forgejo/workflows/deploy.yml@refs/heads/main"].
	WorkflowRefs []string
	// MaxAge bounds how old a token may be (iat), tighter than its expiry.
	MaxAge time.Duration
}

Policy is what a token must prove. Every field is required: an empty value is a configuration error, never "match anything".

Jump to

Keyboard shortcuts

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