apikey

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 23 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 apikey authenticates the access keys a service hands people and CI: bearer tokens osk_<id>.<secret> (RFC 6750), checked against a file of hashed records.

Index

Constants

View Source
const Prefix = "osk_"

Prefix heads every key, so an authenticator tells one from any other bearer token.

View Source
const RefreshInterval = time.Second

RefreshInterval bounds how often a request has the store look at the file.

View Source
const Source = "api-key"

Source is the Principal.Source of a key.

Variables

This section is empty.

Functions

func Hash

func Hash(secret string) string

Hash is what the file holds of a secret: its SHA-256 in hex.

func Revoke

func Revoke(path, id string) error

Revoke marks the record of id disabled in the file at path, which is rewritten whole; a key that is not there, or is disabled already, is an error.

func Split

func Split(key string) (id, secret string, ok bool)

Split reads a key as osk_<id>.<secret>; ok is false for any other string.

func ValidScope

func ValidScope(s string) bool

ValidScope reports whether s is a scope-token of RFC 6749 § 3.3.

Types

type Record

type Record struct {
	ID       string    `json:"id"`
	SHA256   string    `json:"sha256"`
	Subject  string    `json:"subject"`
	Scopes   []string  `json:"scopes"`
	Expires  time.Time `json:"expires,omitzero"`
	Disabled bool      `json:"disabled,omitempty"`
}

Record is one key as the file holds it: the SHA-256 of the secret in hex, never the secret, so reading the file does not give the key away.

func Mint

func Mint(path, subject string, scopes []string, expires time.Time) (string, Record, error)

Mint issues a key for subject with scopes, expiring at expires (zero for never), and appends its record to the file at path, created if absent; the key is returned once, the file never holds it.

func ReadFile

func ReadFile(path string) ([]Record, error)

ReadFile reads the records of the file at path, one JSON object per line with blank lines allowed; a line that is not a valid record, or an id held twice, fails the file with its line number.

func (Record) Expired

func (r Record) Expired(now time.Time) bool

Expired reports whether the record's lifetime had ended at now; a record with no expiry never expires.

type Store

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

Store is the keys of one file, as a guard.Authenticator; it re-reads the file when its content changes and on Reload, so a revocation needs no restart.

func Open

func Open(path string, logger *slog.Logger) (*Store, error)

Open reads the file at path once, failing as ReadFile does; logger (nil for the default) hears each later reload and the one line a failed one logs.

func (*Store) Len

func (s *Store) Len() int

Len is the number of records held, disabled and expired ones included.

func (*Store) Metadata

func (*Store) Metadata() guard.ResourceMetadata

Metadata is empty: no authorization server issues a key, and no scope is requested for one.

func (*Store) Path

func (s *Store) Path() string

Path is the file the store reads.

func (*Store) Reload

func (s *Store) Reload() error

Reload re-reads the file now, as on SIGHUP; a file that does not read keeps the keys held and is returned as the error.

func (*Store) Verify

func (s *Store) Verify(_ context.Context, authorization string, _ *http.Request) (guard.Principal, guard.Refusal)

Verify accepts a key of the file as its subject (RFC 6750 § 2.1): a bearer token that is no key is NothingPresented; an unknown id, wrong secret or disabled key Mismatch; a dead one Expired.

Jump to

Keyboard shortcuts

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