Auth

Functions

Public API reference for the Auth extension

Extension Entry Points

FunctionDescription
NewExtension(opts ...ConfigOption) forge.ExtensionCreate the auth extension with functional options
NewExtensionWithConfig(config Config) forge.ExtensionCreate with a complete config
DefaultConfig() ConfigReturns the default configuration

DI Helpers

FunctionDescription
GetRegistry(c forge.Container) (*Registry, error)Resolve the auth registry from the container
MustGetRegistry(c forge.Container) *RegistrySame but panics on error
GetRegistryFromApp(app forge.App) (*Registry, error)Resolve from an app instance
MustGetRegistryFromApp(app forge.App) *RegistrySame but panics on error

Registry Interface

MethodDescription
Register(provider) errorRegister an auth provider
Unregister(name) errorRemove an auth provider
Get(name) (AuthProvider, error)Get a provider by name
Has(name) boolCheck if a provider exists
List() []AuthProviderList all registered providers
Middleware() forge.MiddlewareMiddleware that succeeds if any provider authenticates
MiddlewareAnd() forge.MiddlewareMiddleware that requires all providers to pass
MiddlewareWithScopes(scopes...) forge.MiddlewareMiddleware with scope requirements
OpenAPISchemes() map[string]anyGet OpenAPI security schemes from all providers

AuthProvider Interface

MethodDescription
Name() stringProvider name
Type() stringProvider type
Authenticate(ctx, r) (*AuthContext, error)Authenticate a request
OpenAPIScheme() map[string]anyOpenAPI security scheme
Middleware() forge.MiddlewareProvider-specific middleware

Context Helpers

FunctionDescription
FromContext(ctx) (*AuthContext, error)Get auth context from request context
MustFromContext(ctx) *AuthContextSame but panics on error
WithContext(ctx, authCtx) context.ContextStore auth context in context
GetAuthContext(ctx forge.Context) *AuthContextGet from Forge context
MustGetAuthContext(ctx forge.Context) *AuthContextSame but panics

AuthContext Methods

MethodDescription
HasScope(scope) boolCheck if user has a scope
HasScopes(scopes...) boolCheck if user has all scopes
GetClaim(key) (any, bool)Get a claim value
GetClaimString(key) (string, bool)Get a claim as string

How is this guide?

On this page