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
- Variables
- func DecodeKey(text string) ([]byte, error)
- func KeyFingerprint(key []byte) string
- func NewKey() ([]byte, error)
- func OpenWith(c *SecretCipher, sealed string) (string, error)
- func OrgScope(orgID string) string
- func PortableReset(collection string) map[string]any
- func PortableToPlain(collection string, doc map[string]any, ek *SecretCipher) error
- func PortableWithoutSecrets(collection string, doc map[string]any) (keep bool)
- func SealWith(c *SecretCipher, token string) (string, error)
- type Scope
- type ScopeOp
- type Scopes
- type SecretCipher
Constants ¶
const KeyLength = 32
KeyLength is the size of a field key in bytes.
const MaxScopes = 32
MaxScopes bounds the grants of one key.
const PlainKey = "$plain"
PlainKey wraps a sealed field's plaintext in a document an import receives.
Variables ¶
var ErrKeyValue = errors.New("a key is 32 bytes, written in base64url")
ErrKeyValue means an imported key isn't 32 bytes of base64url.
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.
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 KeyFingerprint ¶
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 OpenWith ¶
func OpenWith(c *SecretCipher, sealed string) (string, error)
OpenWith decrypts what SealWith made.
func PortableReset ¶
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 ¶
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.
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.
type Scopes ¶
type Scopes []Scope
Scopes is the list of grants of a key.
func ParseScopes ¶
ParseScopes parses grants; duplicates collapse, and the order is kept.
func (Scopes) Allows ¶
Allows reports whether op on database/name is granted. A key without scopes allows everything.
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.