Documentation
¶
Overview ¶
Package authkit is the root of the goravel-authkit package: a batteries-included, session-based authentication + user-management module for Goravel apps.
Install is a single step:
./artisan package:install github.com/freshost/goravel-authkit
which registers this ServiceProvider and publishes config/authkit.go (and config/hashing.go). The app declares its auth domains as guards under authkit.guards in config/authkit.go; the provider then auto-registers a Goravel session guard and the migrations for each guard (no config/auth.go needed). The app mounts the HTTP routes from its own routing callback with one line:
authkitroutes "github.com/freshost/goravel-authkit/routes"
// inside foundation.Setup().WithRouting(func(){ ... })
authkitroutes.RegisterAll(facades.Route())
Routes remain an explicit app-side mount so configured dynamic guards are registered from the host's routing callback and existing integrations keep a stable, duplicate-free contract. The package starts its own session on each guard's /auth group, so no global session middleware is required.
See the README and docs/installation.md.
Index ¶
- Constants
- Variables
- func RegisterImpersonationPolicy(p Impersonator)
- func RegisterRateLimitStore(store RateLimitStore)
- type Authkit
- func (a *Authkit) Authenticate(ctx context.Context, email, password string) (*models.User, error)
- func (a *Authkit) ChangePassword(ctx context.Context, id uuid.UUID, currentPassword, newPassword string) error
- func (a *Authkit) ConfirmTwoFactor(ctx context.Context, id uuid.UUID, code string) ([]string, error)
- func (a *Authkit) CreateUser(ctx context.Context, email, name, password, role string) (*models.User, error)
- func (a *Authkit) DeleteUser(ctx context.Context, id uuid.UUID) error
- func (a *Authkit) DisableTwoFactor(ctx context.Context, id uuid.UUID) error
- func (a *Authkit) EnableTwoFactor(ctx context.Context, id uuid.UUID) (secret, otpauthURL string, err error)
- func (a *Authkit) GetUser(ctx context.Context, id uuid.UUID) (*models.User, error)
- func (a *Authkit) IssueAPIToken(ctx context.Context, input contracts.IssueAPITokenInput) (*contracts.IssuedAPIToken, error)
- func (a *Authkit) ListAPITokens(ctx context.Context, userID uuid.UUID) ([]models.APIToken, error)
- func (a *Authkit) ListUsers(ctx context.Context) ([]models.User, error)
- func (a *Authkit) RevokeAPIToken(ctx context.Context, userID, tokenID uuid.UUID) error
- func (a *Authkit) RevokeAllAPITokens(ctx context.Context, userID uuid.UUID) error
- func (a *Authkit) SetPassword(ctx context.Context, id uuid.UUID, newPassword string) (*models.User, error)
- func (a *Authkit) VerifyTwoFactor(ctx context.Context, id uuid.UUID, code string) (bool, error)
- type Config
- type Impersonator
- type Principal
- type RateLimitResult
- type RateLimitStore
- type ServiceProvider
Constants ¶
const Binding = "authkit"
Binding is the service-container key under which the Authkit service is bound; the facades.Authkit() accessor resolves it.
const Name = "Authkit"
Name is the human-readable module name.
const PackageName = "github.com/freshost/goravel-authkit"
PackageName is the module path, used as the first argument to Publishes.
Variables ¶
var App foundation.Application
App holds the application instance, used by the facade to resolve the service.
Functions ¶
func RegisterImpersonationPolicy ¶ added in v0.3.0
func RegisterImpersonationPolicy(p Impersonator)
RegisterImpersonationPolicy registers the host's fine-grained impersonation authorization hook (optional — authkit's config gate decides when none is set). Call it once at boot. The hook runs only after the config gate has passed, so it can only tighten the decision.
func RegisterRateLimitStore ¶ added in v0.4.0
func RegisterRateLimitStore(store RateLimitStore)
RegisterRateLimitStore installs a process-wide store before routes are registered. Use a shared implementation such as Redis in multi-instance apps.
Types ¶
type Authkit ¶
type Authkit struct {
// contains filtered or unexported fields
}
Authkit is the concrete implementation behind facades.Authkit() and the value returned by New. It wraps the auth + user-management + two-factor services so app code can drive them programmatically against a specific user table.
func New ¶ added in v0.2.0
New builds a programmatic authkit instance bound to cfg.UsersTable. Zero-valued fields fall back to the package defaults, so authkit.New(authkit.Config{}) behaves exactly like the default single-instance service.
func NewAuthkit ¶
func NewAuthkit(app foundation.Application) *Authkit
NewAuthkit builds the default instance from the published authkit.* config (read lazily at resolve time, so config is available). It is the thin wrapper behind facades.Authkit(); multi-table hosts use New directly.
func (*Authkit) Authenticate ¶
func (*Authkit) ChangePassword ¶
func (*Authkit) ConfirmTwoFactor ¶
func (*Authkit) CreateUser ¶
func (*Authkit) DisableTwoFactor ¶
func (*Authkit) EnableTwoFactor ¶
func (*Authkit) IssueAPIToken ¶ added in v0.4.0
func (a *Authkit) IssueAPIToken(ctx context.Context, input contracts.IssueAPITokenInput) (*contracts.IssuedAPIToken, error)
func (*Authkit) ListAPITokens ¶ added in v0.4.0
func (*Authkit) RevokeAPIToken ¶ added in v0.4.0
func (*Authkit) RevokeAllAPITokens ¶ added in v0.4.0
func (*Authkit) SetPassword ¶
type Config ¶ added in v0.2.0
type Config struct {
// Guard is the Goravel guard this instance represents (e.g. "client"). It is
// recorded for symmetry with routes.Options; the programmatic methods operate
// directly on UsersTable and do not resolve a guard.
Guard string
// UsersTable is the table this instance's users live in (default "" → "users").
UsersTable string
// MinPasswordLength is the minimum accepted new-password length (default → DefaultMinPasswordLength).
MinPasswordLength int
// TwoFactorIssuer is the issuer shown in the authenticator app.
TwoFactorIssuer string
// RecoveryCodeCount is how many recovery codes confirmation generates (default → DefaultRecoveryCodeCount).
RecoveryCodeCount int
// Roles, when non-empty, is the set of role values accepted on create/update.
Roles []string
// UserManagementRoles gates the management invariants (default → AdminRole).
UserManagementRoles []string
// APITokensTable is the per-guard personal access token table.
APITokensTable string
APITokenAllowedScopes []string
APITokenMaxLifetime time.Duration
APITokenMaxPerUser int
KeepAPITokensOnPasswordChange bool
}
Config describes one programmatic authkit instance: which user table it operates on plus the password / two-factor / role policy. Every field is optional — the zero value reproduces the single-instance defaults — so a host running two user domains builds one instance per domain:
client := authkit.New(authkit.Config{Guard: "client", UsersTable: "accounts"})
admin := authkit.New(authkit.Config{Guard: "admin", UsersTable: "admin_users"})
type Impersonator ¶ added in v0.3.0
type Impersonator = contracts.Impersonator
Impersonator is the host-implemented, fine-grained impersonation authorization hook. See contracts.Impersonator.
type Principal ¶ added in v0.3.0
Principal identifies a party (actor or target) in an impersonation decision.
type RateLimitResult ¶ added in v0.4.0
type RateLimitResult = contracts.RateLimitResult
RateLimitResult describes whether an attempt was accepted and, when denied, how long the caller should wait.
type RateLimitStore ¶ added in v0.4.0
type RateLimitStore = contracts.RateLimitStore
RateLimitStore is the backend contract for atomic rate-limit buckets.
type ServiceProvider ¶
type ServiceProvider struct{}
ServiceProvider registers the goravel-authkit migrations, commands, and publishable config. HTTP routes are mounted app-side via routes.Register (see the package doc), preserving Authkit's explicit dynamic-route contract.
func (*ServiceProvider) Boot ¶
func (r *ServiceProvider) Boot(app foundation.Application)
Boot registers the package migrations, artisan commands, and the publishable config (for `vendor:publish`). HTTP routes are NOT registered here — the app calls routes.Register from its routing callback (see the package doc).
func (*ServiceProvider) Register ¶
func (r *ServiceProvider) Register(app foundation.Application)
Register stores the application instance and binds the Authkit service so facades.Authkit() (and any app code) can resolve it.
func (*ServiceProvider) Relationship ¶
func (r *ServiceProvider) Relationship() binding.Relationship
Relationship declares the framework services the package depends on so it boots after them. It registers no container bindings of its own.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package commands holds the goravel-authkit artisan commands.
|
Package commands holds the goravel-authkit artisan commands. |
|
Package contracts defines the public interface exposed by the goravel-authkit facade.
|
Package contracts defines the public interface exposed by the goravel-authkit facade. |
|
Package facades exposes the goravel-authkit programmatic API as a Goravel facade.
|
Package facades exposes the goravel-authkit programmatic API as a Goravel facade. |
|
Package helpers holds small HTTP utilities shared by the goravel-authkit controllers and middleware: route-param parsing, the authenticated-user context key, and the session-regeneration workaround.
|
Package helpers holds small HTTP utilities shared by the goravel-authkit controllers and middleware: route-param parsing, the authenticated-user context key, and the session-regeneration workaround. |
|
http
|
|
|
controllers
Package controllers holds the goravel-authkit HTTP controllers: the auth endpoints (login/logout/me/change-password) and the admin user-management CRUD.
|
Package controllers holds the goravel-authkit HTTP controllers: the auth endpoints (login/logout/me/change-password) and the admin user-management CRUD. |
|
middleware
Package middleware holds the goravel-authkit HTTP middleware: the session guard (Authenticated), request-origin verification, and authentication rate limits.
|
Package middleware holds the goravel-authkit HTTP middleware: the session guard (Authenticated), request-origin verification, and authentication rate limits. |
|
responses
Package responses holds the request/response DTOs for the goravel-authkit HTTP endpoints.
|
Package responses holds the request/response DTOs for the goravel-authkit HTTP endpoints. |
|
Package migrations holds the code-based migrations owned by goravel-authkit.
|
Package migrations holds the code-based migrations owned by goravel-authkit. |
|
Package models holds the canonical GORM entities owned by the goravel-authkit package: the single User table backing authentication and the AuditLog table.
|
Package models holds the canonical GORM entities owned by the goravel-authkit package: the single User table backing authentication and the AuditLog table. |
|
Package repositories owns the GORM data access for goravel-authkit.
|
Package repositories owns the GORM data access for goravel-authkit. |
|
Package routes registers the goravel-authkit HTTP endpoints onto a consuming app's router.
|
Package routes registers the goravel-authkit HTTP endpoints onto a consuming app's router. |
|
Package services holds the goravel-authkit business logic: credential verification, password changes with other-session invalidation, user management, and audit writes.
|
Package services holds the goravel-authkit business logic: credential verification, password changes with other-session invalidation, user management, and audit writes. |
|
Command setup implements `./artisan package:install github.com/freshost/goravel-authkit`.
|
Command setup implements `./artisan package:install github.com/freshost/goravel-authkit`. |
|
config
This file is published into the consuming app's config/ directory by `./artisan package:install github.com/freshost/goravel-authkit`.
|
This file is published into the consuming app's config/ directory by `./artisan package:install github.com/freshost/goravel-authkit`. |