store

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 24 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 store persists the bridge state in SQLite, with secrets encrypted by AES-GCM.

Index

Constants

View Source
const (
	AccessToken  = "access"
	RefreshToken = "refresh"
)

Token kinds.

View Source
const LocalUser = "local"

LocalUser is the single user of the stdio mode.

Variables

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

ErrNotFound is returned when a record does not exist or has expired.

View Source
var ErrTokenReuse = errors.New("refresh token reuse")

ErrTokenReuse means a rotated refresh token was presented again; its whole family is revoked.

Functions

func MasterKey

func MasterKey(dir string, useKeyring bool) (key []byte, source string, err error)

MasterKey loads the master key of dir or generates it on first start. With useKeyring, a key is kept in the OS keyring when one is available; otherwise, or when a key file already exists, it lives in dir/master.key (mode 0600).

func RandomToken

func RandomToken(prefix string) string

RandomToken returns a URL-safe random string with prefix.

Types

type Account

type Account struct {
	ID         string
	UserID     string
	Email      string
	Issuer     string
	SessionURL string
	Token      *oauth2.Token
	Default    bool
}

Account is a connected mail account with its mail-server token.

type Client

type Client struct {
	ClientID     string   `json:"client_id"`
	ClientName   string   `json:"client_name,omitempty"`
	RedirectURIs []string `json:"redirect_uris"`
	AuthMethod   string   `json:"token_endpoint_auth_method"`
	SecretHash   string   `json:"-"`
}

Client is an MCP client registered at the bridge (RFC 7591).

type Grant

type Grant struct {
	UserID        string `json:"user_id"`
	ClientID      string `json:"client_id"`
	RedirectURI   string `json:"redirect_uri,omitempty"`
	CodeChallenge string `json:"code_challenge,omitempty"`
	Resource      string `json:"resource"`
	Scope         string `json:"scope"`
	ExpiresAt     int64  `json:"-"`
	Family        string `json:"-"`
}

Grant is what an authorization code or token stands for.

type Host

type Host struct {
	Meta         *mailauth.Meta
	Registration *mailauth.Registration
}

Host is a discovered authorization server and this bridge's registration there.

type Store

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

Store is the bridge database.

func Open

func Open(dir string, master []byte) (*Store, error)

Open opens (and creates or migrates) the database in dir with the given master key.

func (*Store) Account

func (s *Store) Account(ctx context.Context, userID, accountID string) (*Account, error)

Account returns one account of a user by id.

func (*Store) Accounts

func (s *Store) Accounts(ctx context.Context, userID string) ([]*Account, error)

Accounts lists the accounts of a user. Accounts whose token cannot be decrypted are skipped.

func (*Store) AuthRequest

func (s *Store) AuthRequest(ctx context.Context, id string, v any, consume bool) error

AuthRequest loads a pending authorization request; with consume it is deleted.

func (*Store) Client

func (s *Store) Client(ctx context.Context, id string) (*Client, error)

Client returns a registered client.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) ConsumeCode

func (s *Store) ConsumeCode(ctx context.Context, code string) (*Grant, error)

ConsumeCode redeems an authorization code once. The code is kept until it expires: presenting it again revokes the tokens issued from it (RFC 9700 §4.5) and returns ErrTokenReuse.

func (*Store) Hash

func (s *Store) Hash(secret string) string

Hash returns a keyed hash of a secret, used to store issued tokens and codes.

func (*Store) Host

func (s *Store) Host(ctx context.Context, issuer, kind string) (*Host, error)

Host returns the stored registration for issuer; kind separates loopback and web redirect URIs.

func (*Store) Key

func (s *Store) Key(purpose string) []byte

Key derives a 32-byte purpose-specific key from the master key.

func (*Store) Purge

func (s *Store) Purge(ctx context.Context) error

Purge deletes expired codes, requests and tokens.

func (*Store) RevokeToken

func (s *Store) RevokeToken(ctx context.Context, token, clientID string) error

RevokeToken deletes a token of clientID; revoking a refresh token revokes its whole family.

func (*Store) RotateRefresh

func (s *Store) RotateRefresh(ctx context.Context, token, clientID string) (*Grant, error)

RotateRefresh marks a refresh token of clientID as used and returns its grant. Presenting an already rotated token revokes every token of its family (RFC 9700 §4.14.2).

func (*Store) SaveAccount

func (s *Store) SaveAccount(ctx context.Context, a *Account) (string, error)

SaveAccount creates or replaces the account (user, email) and returns its id.

func (*Store) SaveAuthRequest

func (s *Store) SaveAuthRequest(ctx context.Context, id string, v any, ttl time.Duration) error

SaveAuthRequest stores (or replaces) an encrypted pending authorization request.

func (*Store) SaveClient

func (s *Store) SaveClient(ctx context.Context, c *Client) error

SaveClient stores a new client.

func (*Store) SaveCode

func (s *Store) SaveCode(ctx context.Context, code string, g *Grant, ttl time.Duration) error

SaveCode stores an authorization code by its hash.

func (*Store) SaveHost

func (s *Store) SaveHost(ctx context.Context, issuer, kind string, h *Host) error

SaveHost stores the metadata and registration of issuer.

func (*Store) SaveToken

func (s *Store) SaveToken(ctx context.Context, token, kind string, g *Grant, ttl time.Duration) error

SaveToken stores an issued token by its hash.

func (*Store) Seal

func (s *Store) Seal(aad string, v any) ([]byte, error)

Seal encrypts v as JSON, bound to aad.

func (*Store) SetDefault

func (s *Store) SetDefault(ctx context.Context, userID, email string) error

SetDefault marks one account of the user as the default.

func (*Store) StaticHosts

func (s *Store) StaticHosts(ctx context.Context, kind string) ([]string, error)

StaticHosts lists the issuers whose registration was entered by the operator.

func (*Store) Token

func (s *Store) Token(ctx context.Context, token, kind string) (*Grant, error)

Token returns a valid token of the given kind.

func (*Store) Unseal

func (s *Store) Unseal(aad string, data []byte, v any) error

Unseal decrypts what Seal produced with the same aad.

func (*Store) UpdateToken

func (s *Store) UpdateToken(ctx context.Context, accountID string, t *oauth2.Token) error

UpdateToken replaces the stored token of an account after a refresh.

func (*Store) UpsertUser

func (s *Store) UpsertUser(ctx context.Context, issuer, subject string) (string, error)

UpsertUser returns the id of the user identified by (issuer, subject), creating it if needed.

func (*Store) UserExists

func (s *Store) UserExists(ctx context.Context, id string) bool

UserExists reports whether a user id is known.

Jump to

Keyboard shortcuts

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