authcore

package
v0.9.12 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 12 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 authcore holds the parts of backd's authentication that need no store: API key scopes, the secrets cipher, field key encoding, and the portable form of authentication data in export packages. It never imports the MongoDB driver, so the CLI uses it without linking the server. internal/auth builds the stores on it.

Index

Constants

View Source
const KeyLength = 32

KeyLength is the size of a field key in bytes.

View Source
const MaxScopes = 32

MaxScopes bounds the grants of one key.

View Source
const PlainKey = "$plain"

PlainKey wraps a sealed field's plaintext in a document an import receives.

Variables

View Source
var ErrKeyValue = errors.New("a key is 32 bytes, written in base64url")

ErrKeyValue means an imported key isn't 32 bytes of base64url.

View Source
var PortableCollections = []string{"users", "identities", "orgs", "org_members", "org_teams", "org_connections", "mfa", "devices"}

The realm's authentication data that moves with an app (design/export-import.md §7): the system collections an export package can hold, in the order an import writes them.

View Source
var PortableSealed = map[string][]string{
	"identities": {"apple_refresh_token"},
	"mfa":        {"secret"},
	"devices":    {"token"},
}

PortableSealed names, per collection, the fields sealed with the realm's secrets cipher. An export opens them and seals them again under the one-time export key; an import (after the CLI opened them with that key) seals the plaintext under the target realm's cipher.

Functions

func DecodeKey

func DecodeKey(text string) ([]byte, error)

DecodeKey reads a key written in base64url, with or without padding.

func KeyFingerprint

func KeyFingerprint(key []byte) string

KeyFingerprint is the first 8 hex characters of SHA-256 over a fixed label and the key: it lets someone check that a saved copy is the right one without the key being shown again.

func NewKey

func NewKey() ([]byte, error)

NewKey is a CreatedKey's source of a generated key: 32 random bytes.

func OpenWith

func OpenWith(c *SecretCipher, sealed string) (string, error)

OpenWith decrypts what SealWith made.

func OrgScope

func OrgScope(orgID string) string

OrgScope is the scope of an organization's secrets: "org:<id>".

func PortableReset

func PortableReset(collection string) map[string]any

PortableReset names fields an export carries empty because their value can't travel: the recovery codes are keyed hashes under the realm's master key and can't be re-keyed (users get new ones), and a device is tied to a session that is not exported.

func PortableToPlain

func PortableToPlain(collection string, doc map[string]any, ek *SecretCipher) error

PortableToPlain opens, with the export key, the fields of a package document that an export sealed under it, and hands them over as {"$plain": value} for an import to seal under the target realm's cipher. The key never leaves the machine that runs the import.

func PortableWithoutSecrets

func PortableWithoutSecrets(collection string, doc map[string]any) (keep bool)

PortableWithoutSecrets prepares a package document for an import made without the export key: it reports whether the document can be imported at all (a second factor or a device is useless without its secret) and removes the sealed fields of the ones that can.

func SealWith

func SealWith(c *SecretCipher, token string) (string, error)

SealWith encrypts a token under a cipher, in the form "<key id>.<nonce>.<ciphertext>".

Types

type Scope

type Scope struct {
	Op       ScopeOp
	Database string // "" is every database
	Name     string // a collection (read, write) or a function (call); "" is all of the database
}

Scopes limit what a data API key reaches, so a leaked key exposes only what it was made for. A key without scopes keeps full access (every key made before scopes existed); a key with scopes may do exactly what they grant and nothing else. A grant is `ops` or `ops:target`:

read                  read every collection
read:blog             read the collections of the database blog
write:blog/posts      create, update and delete in blog/posts
call                  call every function
call:main             call the functions of the database main
call:main/export      call that function (and read its jobs)

write means create, update and delete; it doesn't include read. The targets are relative to the key's realm.

func ParseScope

func ParseScope(s string) (Scope, error)

ParseScope parses one grant.

func (Scope) String

func (sc Scope) String() string

type ScopeOp

type ScopeOp string

ScopeOp is the kind of access a grant gives.

const (
	ScopeRead  ScopeOp = "read"
	ScopeWrite ScopeOp = "write"
	ScopeCall  ScopeOp = "call"
)

type Scopes

type Scopes []Scope

Scopes is the list of grants of a key.

func ParseScopes

func ParseScopes(list []string) (Scopes, error)

ParseScopes parses grants; duplicates collapse, and the order is kept.

func (Scopes) Allows

func (s Scopes) Allows(op ScopeOp, database, name string) bool

Allows reports whether op on database/name is granted. A key without scopes allows everything.

func (Scopes) Limited

func (s Scopes) Limited() bool

Limited reports whether the key has scopes at all: without, it has full access.

func (Scopes) Strings

func (s Scopes) Strings() []string

Strings writes the grants as they are parsed.

type SecretCipher

type SecretCipher struct {
	KeyID string
	// contains filtered or unexported fields
}

SecretCipher encrypts and decrypts secret values with AES-256-GCM, under a key derived from the operator's master key (never the master key's raw bytes directly, so an accidental log of the derived key alone can't be turned back into the configured secret). KeyID is a fingerprint safe to store and log: it identifies which master key a value was encrypted with, for rotation, without revealing it.

func ExportKey

func ExportKey(text string) (*SecretCipher, error)

ExportKey makes the cipher of an export key from its text (the key itself is 32 random bytes in base64url); its KeyID is the fingerprint the manifest records.

func NewSecretCipher

func NewSecretCipher(raw []byte) (*SecretCipher, error)

NewSecretCipher derives a cipher from raw master key material (any length, at least 32 characters is enforced by settings). The same raw material always derives the same cipher and KeyID.

func (*SecretCipher) MAC

func (c *SecretCipher) MAC(parts ...string) string

MAC is a keyed hash (hex HMAC-SHA-256) of the parts, under a key derived from the master key and kept apart from the encryption key: for values that must be compared but never read back, such as recovery codes. The parts are length-prefixed, so ("a","bc") and ("ab","c") differ.

func (*SecretCipher) Open

func (c *SecretCipher) Open(ciphertext, nonce, keyID string) (string, error)

Open decrypts a value sealed with a cipher whose KeyID matches keyID.

func (*SecretCipher) Seal

func (c *SecretCipher) Seal(plaintext string) (ciphertext, nonce string, err error)

Seal encrypts plaintext, returning base64 ciphertext and nonce.

Jump to

Keyboard shortcuts

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