token

package
v0.0.0-...-02e3363 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package token implements VersionGate's API Token domain concept: issuing, verifying, and revoking Project-scoped bearer credentials (specs/decisions/authentication.md). Like internal/project, this package has no dependency on any specific storage technology.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("token: invalid or revoked")

ErrInvalid is returned by Verify when raw does not correspond to any active token. It covers both "no such token" and "revoked token" — callers cannot distinguish which, by design: telling them apart would leak whether a given token value ever existed (specs/decisions/authentication.md).

View Source
var ErrNotFound = errors.New("token: not found")

ErrNotFound is returned by a Repository when no Token matches a given hash or ID.

Functions

This section is empty.

Types

type ID

type ID string

ID identifies an API Token record. Like project.ID, it is assigned by a Repository at creation time.

type Repository

type Repository interface {
	Create(ctx context.Context, projectID project.ID, tokenHash string) (Token, error)
	GetByHash(ctx context.Context, tokenHash string) (Token, error)
	Revoke(ctx context.Context, id ID) (Token, error)
}

Repository persists and retrieves Tokens. Infrastructure provides the implementation; only a hash is ever passed to or read from it — the raw token value never reaches storage.

type Token

type Token struct {
	ID        ID
	ProjectID project.ID
	CreatedAt time.Time
	RevokedAt *time.Time // nil means the token is active.
}

Token is metadata about an issued API Token. The raw secret value itself is never part of this type — see Issue's return value for the only place it is ever available.

func Issue

func Issue(ctx context.Context, repo Repository, projectID project.ID) (Token, string, error)

Issue generates a new random token for projectID, persists only its hash via repo, and returns both the Token record and the raw secret value. The raw value is never stored and is only ever available here, at creation — callers must show it to the operator immediately, since it cannot be recovered afterward.

func Revoke

func Revoke(ctx context.Context, repo Repository, id ID) (Token, error)

Revoke immediately invalidates the token identified by id. Revoking an already-revoked token is not an error — it is idempotent, and does not change the original revocation time.

func Verify

func Verify(ctx context.Context, repo Repository, raw string) (Token, error)

Verify resolves raw to its Token record, if raw corresponds to a currently active (non-revoked) token. Any other case — no matching token, or a matching but revoked one — is reported identically as ErrInvalid.

func (Token) Revoked

func (t Token) Revoked() bool

Revoked reports whether the token has been revoked.

Jump to

Keyboard shortcuts

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