mongodb

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package mongodb provides a native MongoDB adapter for auth stores.

Index

Constants

View Source
const (
	// PrincipalsCollection is the MongoDB collection for auth principals.
	PrincipalsCollection = "auth_principals"

	// APIKeysCollection is the MongoDB collection for API key metadata.
	APIKeysCollection = "auth_api_keys"

	// AuditEventsCollection is the MongoDB collection for audit events.
	AuditEventsCollection = "auth_audit_events"

	// SchemaMigrationsCollection is the MongoDB collection for migration records.
	SchemaMigrationsCollection = "auth_schema_migrations"
)

Variables

This section is empty.

Functions

func DeleteData

func DeleteData(ctx context.Context, db *mongo.Database) error

DeleteData deletes all auth adapter data while keeping MongoDB indexes and migration records.

This is intentionally separate from Migrate so index creation stays non-destructive unless callers explicitly choose to delete data.

func Migrate

func Migrate(ctx context.Context, db *mongo.Database) error

Migrate applies pending MongoDB migrations.

func Migrations

func Migrations() []migrate.Migration

Migrations returns the MongoDB migrations for the auth adapter.

MongoDB collections are created lazily. This migration creates the adapter indexes if needed and records the applied version.

Types

type Connection

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

Connection owns a MongoDB client opened by this package.

func Open

func Open(ctx context.Context, uri string, databaseName string) (*Connection, error)

Open opens a MongoDB connection and verifies connectivity.

func (*Connection) Client

func (c *Connection) Client() *mongo.Client

Client returns the underlying MongoDB client.

func (*Connection) Close

func (c *Connection) Close(ctx context.Context) error

Close disconnects the underlying MongoDB client.

func (*Connection) Database

func (c *Connection) Database() *mongo.Database

Database returns the configured MongoDB database.

func (*Connection) DeleteData

func (c *Connection) DeleteData(ctx context.Context) error

DeleteData deletes all auth adapter data in the configured database.

func (*Connection) Migrate

func (c *Connection) Migrate(ctx context.Context) error

Migrate applies pending MongoDB migrations to the configured database.

func (*Connection) Store

func (c *Connection) Store() *Store

Store returns a MongoDB auth store backed by the configured database.

func (*Connection) TransactionalStore

func (c *Connection) TransactionalStore() *TransactionalStore

TransactionalStore returns a MongoDB auth store with transaction-backed key/audit operations.

type MigrationDriver

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

MigrationDriver implements migrate.Driver for MongoDB.

func NewMigrationDriver

func NewMigrationDriver(db *mongo.Database) *MigrationDriver

NewMigrationDriver creates a MongoDB migration driver.

func (*MigrationDriver) Applied

Applied returns migrations already applied, keyed by version.

func (*MigrationDriver) Apply

func (d *MigrationDriver) Apply(ctx context.Context, migration migrate.Migration) error

Apply creates MongoDB indexes and records the migration.

MongoDB index creation is idempotent when names and definitions match. The migration record is written last so retries remain safe if recording fails.

func (*MigrationDriver) EnsureSchema

func (d *MigrationDriver) EnsureSchema(ctx context.Context) error

EnsureSchema verifies the MongoDB database handle.

type Store

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

Store implements auth stores using MongoDB.

func NewStore

func NewStore(db *mongo.Database) *Store

NewStore creates a MongoDB store backed by db.

func (*Store) CreateAPIKey

func (s *Store) CreateAPIKey(ctx context.Context, key auth.APIKey) error

CreateAPIKey stores API key metadata.

func (*Store) CreatePrincipal

func (s *Store) CreatePrincipal(ctx context.Context, principal auth.Principal) error

CreatePrincipal stores a user or group principal.

func (*Store) DeleteData

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

DeleteData deletes all auth adapter data while keeping MongoDB indexes and migration records.

func (*Store) GetAPIKeyByID

func (s *Store) GetAPIKeyByID(ctx context.Context, keyID string) (auth.APIKey, error)

GetAPIKeyByID returns API key metadata by ID.

func (*Store) GetAPIKeyByPrefix

func (s *Store) GetAPIKeyByPrefix(ctx context.Context, prefix string) (auth.APIKey, error)

GetAPIKeyByPrefix returns API key metadata by public prefix.

func (*Store) GetPrincipal

func (s *Store) GetPrincipal(ctx context.Context, principalType auth.PrincipalType, principalID string) (auth.Principal, error)

GetPrincipal returns a user or group principal.

func (*Store) ListAPIKeys

func (s *Store) ListAPIKeys(ctx context.Context, ownerType auth.PrincipalType, ownerID string, page auth.PageRequest) (auth.Page[auth.APIKey], error)

ListAPIKeys returns a page of keys for a principal.

func (*Store) RecordAuditEvent

func (s *Store) RecordAuditEvent(ctx context.Context, event auth.AuditEvent) error

RecordAuditEvent stores an audit event.

func (*Store) RevokeAPIKey

func (s *Store) RevokeAPIKey(ctx context.Context, keyID string, revokedAt time.Time) error

RevokeAPIKey marks an API key as revoked.

func (*Store) TouchAPIKey

func (s *Store) TouchAPIKey(ctx context.Context, keyID string, usedAt time.Time) error

TouchAPIKey records successful use.

type TransactionalStore

type TransactionalStore struct {
	*Store
}

TransactionalStore implements atomic key/audit operations using MongoDB transactions.

MongoDB transactions require a replica set or sharded cluster. Use NewStore for standalone deployments or when best-effort audit writes are acceptable.

func NewTransactionalStore

func NewTransactionalStore(db *mongo.Database) *TransactionalStore

NewTransactionalStore creates a MongoDB store with transaction-backed key/audit operations.

func (*TransactionalStore) CreateAPIKeyWithAudit

func (s *TransactionalStore) CreateAPIKeyWithAudit(ctx context.Context, key auth.APIKey, event auth.AuditEvent) error

CreateAPIKeyWithAudit stores API key metadata and its audit event atomically.

func (*TransactionalStore) RevokeAPIKeyWithAudit

func (s *TransactionalStore) RevokeAPIKeyWithAudit(ctx context.Context, keyID string, revokedAt time.Time, event auth.AuditEvent) (auth.APIKey, error)

RevokeAPIKeyWithAudit reads and revokes an API key, then stores its audit event atomically.

Jump to

Keyboard shortcuts

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