Documentation
¶
Overview ¶
Package mongodb provides a native MongoDB adapter for auth stores.
Index ¶
- Constants
- func DeleteData(ctx context.Context, db *mongo.Database) error
- func Migrate(ctx context.Context, db *mongo.Database) error
- func Migrations() []migrate.Migration
- type Connection
- func (c *Connection) Client() *mongo.Client
- func (c *Connection) Close(ctx context.Context) error
- func (c *Connection) Database() *mongo.Database
- func (c *Connection) DeleteData(ctx context.Context) error
- func (c *Connection) Migrate(ctx context.Context) error
- func (c *Connection) Store() *Store
- func (c *Connection) TransactionalStore() *TransactionalStore
- type MigrationDriver
- type Store
- func (s *Store) CreateAPIKey(ctx context.Context, key auth.APIKey) error
- func (s *Store) CreatePrincipal(ctx context.Context, principal auth.Principal) error
- func (s *Store) DeleteData(ctx context.Context) error
- func (s *Store) GetAPIKeyByID(ctx context.Context, keyID string) (auth.APIKey, error)
- func (s *Store) GetAPIKeyByPrefix(ctx context.Context, prefix string) (auth.APIKey, error)
- func (s *Store) GetPrincipal(ctx context.Context, principalType auth.PrincipalType, principalID string) (auth.Principal, error)
- func (s *Store) ListAPIKeys(ctx context.Context, ownerType auth.PrincipalType, ownerID string, ...) (auth.Page[auth.APIKey], error)
- func (s *Store) RecordAuditEvent(ctx context.Context, event auth.AuditEvent) error
- func (s *Store) RevokeAPIKey(ctx context.Context, keyID string, revokedAt time.Time) error
- func (s *Store) TouchAPIKey(ctx context.Context, keyID string, usedAt time.Time) error
- type TransactionalStore
Constants ¶
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 ¶
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 Migrations ¶
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 (*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 ¶
func (d *MigrationDriver) Applied(ctx context.Context) (map[int64]migrate.AppliedMigration, error)
Applied returns migrations already applied, keyed by version.
func (*MigrationDriver) Apply ¶
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 (*Store) CreateAPIKey ¶
CreateAPIKey stores API key metadata.
func (*Store) CreatePrincipal ¶
CreatePrincipal stores a user or group principal.
func (*Store) DeleteData ¶
DeleteData deletes all auth adapter data while keeping MongoDB indexes and migration records.
func (*Store) GetAPIKeyByID ¶
GetAPIKeyByID returns API key metadata by ID.
func (*Store) GetAPIKeyByPrefix ¶
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 ¶
RecordAuditEvent stores an audit event.
func (*Store) RevokeAPIKey ¶
RevokeAPIKey marks an API key as revoked.
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.