cfaccess

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0 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 cfaccess checks Cloudflare Access tokens: the JWT that Cloudflare Access puts in the Cf-Access-Jwt-Assertion header (and the CF_Authorization cookie) of every request it lets through to an application behind a Cloudflare Tunnel.

A token is accepted only if it is signed with RS256 by one of the team's current signing keys, published at https://TEAM/cdn-cgi/access/certs; if its audience is the application's AUD tag and its issuer is https://TEAM; if it carries exp, iat, aud and iss; if it has not expired and is already valid (exp and nbf, with a little clock skew); and, when an email allowlist is configured, if its email is on the list.

The keys are cached for about an hour. A token whose key ID the cache does not know makes the verifier fetch the keys again (Cloudflare rotates them), at most once every refetchEvery, so forged key IDs cannot turn the verifier into a request amplifier.

Index

Constants

View Source
const (
	Header = "Cf-Access-Jwt-Assertion"
	Cookie = "CF_Authorization"
)

Header and Cookie carry the token.

Variables

View Source
var ErrNoToken = errors.New("no Cloudflare Access token")

ErrNoToken: the request carries no token.

Functions

func FromCloudflare

func FromCloudflare(r *http.Request) bool

FromCloudflare reports whether a request came through Cloudflare: it carries a header that Cloudflare sets.

func TokenFrom

func TokenFrom(r *http.Request) string

TokenFrom returns the token of a request: the header, else the cookie.

Types

type Claims

type Claims struct {
	Email string
	Sub   string
}

Claims are the parts of a verified token callers use.

type Config

type Config struct {
	// Team is the team domain (myteam.cloudflareaccess.com). A leading
	// https:// and a trailing slash are ignored.
	Team string
	// AUD is the application's Audience (AUD) tag.
	AUD string
	// Emails, if not empty, are the only emails allowed in (compared
	// lower-cased).
	Emails []string

	// CertsURL replaces https://TEAM/cdn-cgi/access/certs (tests).
	CertsURL string
	// Client fetches the keys; default a client with a 10 s timeout.
	Client *http.Client
	// Now replaces the clock (tests).
	Now func() time.Time
}

Config configures a Verifier.

type Verifier

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

Verifier checks tokens for one Access application.

func New

func New(c Config) (*Verifier, error)

New makes a verifier. Team and AUD are required.

func (*Verifier) Issuer

func (v *Verifier) Issuer() string

Issuer is the iss a token must carry.

func (*Verifier) Verify

func (v *Verifier) Verify(ctx context.Context, token string) (*Claims, error)

Verify checks a token and returns its claims.

Directories

Path Synopsis
Package cfaccesstest makes Cloudflare Access tokens for tests: an RSA signing key set, its JWKS document and signed JWTs.
Package cfaccesstest makes Cloudflare Access tokens for tests: an RSA signing key set, its JWKS document and signed JWTs.

Jump to

Keyboard shortcuts

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