pat

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package pat implements personal access tokens: long-lived, named, scoped bearer credentials a user creates for themselves (a CLI, a script, an integration) outside of an interactive login. It has no notion of HTTP — a host application resolves an incoming bearer header via Resolve and decides what to do with the result.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken   = errors.New("authit/pat: invalid, revoked, or expired token")
	ErrExpiryTooFar   = errors.New("authit/pat: requested expiry exceeds the configured maximum")
	ErrExpiryRequired = errors.New("authit/pat: an expiry is required")
	ErrNotOwner       = errors.New("authit/pat: token does not belong to this user")
)

Functions

func HasScope

func HasScope(t store.PersonalAccessToken, scope string) bool

HasScope reports whether t was granted scope.

Types

type Config

type Config struct {
	// Prefix is prepended to every generated token, e.g. "ghp_" or "mb_",
	// so a token is recognizable at a glance (in a shell history, a leaked
	// log line, a secret scanner's rule). Defaults to "authit_".
	Prefix string
	// MaxExpiry, if set, caps how far in the future CreateToken will allow
	// ExpiresAt to be. nil means no cap.
	MaxExpiry *time.Duration
	// RequireExpiry, if true, makes CreateToken reject a nil expiresAt.
	// Off by default; GitHub and GitLab have both moved toward mandatory
	// expiry over time, so a host application that wants that stance can
	// opt in here rather than authit assuming it.
	RequireExpiry bool
	// AuditLogger receives security-relevant events (token creation,
	// revocation). Nil means events are not recorded — see package audit.
	AuditLogger audit.Logger
}

Config tunes the pat package's behavior.

type Service

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

Service implements personal-access-token issuance, resolution, and revocation.

func NewService

func NewService(stores Stores, cfg Config) (*Service, error)

NewService constructs a Service. Config.AuditLogger may be nil, in which case audit.NoopLogger is used.

func (*Service) CreateToken

func (s *Service) CreateToken(ctx context.Context, userID, name string, scopes []string, expiresAt *time.Time) (string, store.PersonalAccessToken, error)

CreateToken issues a new personal access token for userID. Returns the raw token exactly once — only its hash is persisted, so the caller must hand it to the user immediately.

func (*Service) ListTokens

func (s *Service) ListTokens(ctx context.Context, userID string) ([]store.PersonalAccessToken, error)

ListTokens lists every token belonging to userID (including expired or revoked ones, so a UI can show their status — filter by ExpiresAt/ RevokedAt as needed).

func (*Service) Resolve

func (s *Service) Resolve(ctx context.Context, rawToken string) (store.PersonalAccessToken, error)

Resolve validates a raw bearer token and returns the record it maps to, bumping LastUsedAt on success (best-effort: a failure to record that does not fail the resolution). Callers typically check HasScope against the result afterward.

func (*Service) RevokeToken

func (s *Service) RevokeToken(ctx context.Context, userID, tokenID string) error

RevokeToken revokes a token, scoped to userID so a caller can't revoke another user's token.

type Stores

type Stores struct {
	Tokens store.PersonalAccessTokenStore
}

Stores groups the persistence ports the pat package needs.

Jump to

Keyboard shortcuts

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