Documentation
¶
Index ¶
- Constants
- Variables
- func IsValidationError(err error) bool
- type AuthKey
- type AuthenticationResult
- type CreateInput
- type IssuedKey
- type MongoDBStore
- type PostgreSQLStore
- type Result
- type SQLiteStore
- type Service
- func (s *Service) ActiveCount() int
- func (s *Service) Authenticate(_ context.Context, token string) (AuthenticationResult, error)
- func (s *Service) Create(ctx context.Context, input CreateInput) (*IssuedKey, error)
- func (s *Service) Deactivate(ctx context.Context, id string) error
- func (s *Service) Enabled() bool
- func (s *Service) ListViews() []View
- func (s *Service) Refresh(ctx context.Context) error
- func (s *Service) StartBackgroundRefresh(interval time.Duration) func()
- func (s *Service) Total() int
- type Store
- type ValidationError
- type View
Constants ¶
const (
// TokenPrefix is the managed API key prefix returned to clients.
TokenPrefix = "sk_gom_"
)
Variables ¶
var ( // ErrNotFound indicates a requested auth key record does not exist. ErrNotFound = errors.New("auth key not found") // ErrInvalidToken indicates the presented token does not match a known key. ErrInvalidToken = errors.New("invalid API key") // ErrInactive indicates the presented token belongs to an inactive key. ErrInactive = errors.New("API key is inactive") // ErrExpired indicates the presented token belongs to an expired key. ErrExpired = errors.New("API key expired") )
Functions ¶
func IsValidationError ¶
IsValidationError reports whether err is a validation error.
Types ¶
type AuthKey ¶
type AuthKey struct {
ID string `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
UserPath string `json:"user_path,omitempty" bson:"user_path,omitempty"`
RedactedValue string `json:"redacted_value" bson:"redacted_value"`
SecretHash string `json:"-" bson:"secret_hash"`
Enabled bool `json:"enabled" bson:"enabled"`
ExpiresAt *time.Time `json:"expires_at,omitempty" bson:"expires_at,omitempty"`
DeactivatedAt *time.Time `json:"deactivated_at,omitempty" bson:"deactivated_at,omitempty"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
}
AuthKey is the persisted auth key record.
type AuthenticationResult ¶
AuthenticationResult describes one successful managed auth key lookup.
type CreateInput ¶
CreateInput captures the admin request for issuing a new auth key.
type MongoDBStore ¶
type MongoDBStore struct {
// contains filtered or unexported fields
}
MongoDBStore stores auth keys in MongoDB.
func NewMongoDBStore ¶
func NewMongoDBStore(database *mongo.Database) (*MongoDBStore, error)
NewMongoDBStore creates collection indexes if needed.
func (*MongoDBStore) Close ¶
func (s *MongoDBStore) Close() error
func (*MongoDBStore) Deactivate ¶
type PostgreSQLStore ¶
type PostgreSQLStore struct {
// contains filtered or unexported fields
}
PostgreSQLStore stores auth keys in PostgreSQL.
func NewPostgreSQLStore ¶
NewPostgreSQLStore creates the auth_keys table and indexes if needed.
func (*PostgreSQLStore) Close ¶
func (s *PostgreSQLStore) Close() error
func (*PostgreSQLStore) Create ¶
func (s *PostgreSQLStore) Create(ctx context.Context, key AuthKey) error
func (*PostgreSQLStore) Deactivate ¶
type Result ¶
type Result struct {
Service *Service
Store Store
Storage storage.Storage
// contains filtered or unexported fields
}
Result holds the initialized auth key service and any owned resources.
func NewWithSharedStorage ¶
NewWithSharedStorage creates an auth key subsystem using an existing storage connection.
type SQLiteStore ¶
type SQLiteStore struct {
// contains filtered or unexported fields
}
SQLiteStore stores auth keys in SQLite.
func NewSQLiteStore ¶
func NewSQLiteStore(db *sql.DB) (*SQLiteStore, error)
NewSQLiteStore creates the auth_keys table and indexes if needed.
func (*SQLiteStore) Close ¶
func (s *SQLiteStore) Close() error
func (*SQLiteStore) Deactivate ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service keeps managed auth keys cached in memory for request authentication.
func NewService ¶
NewService creates a managed auth key service backed by storage.
func (*Service) ActiveCount ¶
ActiveCount returns the number of currently active auth keys.
func (*Service) Authenticate ¶
Authenticate validates a presented bearer token against the in-memory snapshot and returns the matched auth key metadata on success.
func (*Service) Create ¶
Create issues a new managed auth key, persists it, updates the in-memory snapshot immediately, and then best-effort reconciles from storage.
func (*Service) Deactivate ¶
Deactivate marks a managed auth key inactive while preserving its record and best-effort reconciles the snapshot from storage afterward.
func (*Service) Refresh ¶
Refresh reloads keys from storage and atomically swaps the in-memory snapshot.
func (*Service) StartBackgroundRefresh ¶
StartBackgroundRefresh periodically reloads auth keys from storage until stopped.
type Store ¶
type Store interface {
List(ctx context.Context) ([]AuthKey, error)
Create(ctx context.Context, key AuthKey) error
Deactivate(ctx context.Context, id string, now time.Time) error
Close() error
}
Store defines persistence operations for managed auth keys.
type ValidationError ¶
ValidationError indicates invalid auth key input or state.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error