Documentation
¶
Overview ¶
Package goten provides composable, self-hosted authentication for Go.
Index ¶
- Variables
- func DecodeJSON(r *http.Request, dst any) error
- func GetClientIP(r *http.Request, ipHeader string) string
- func SessionFromContext(ctx context.Context) (*models.Session, bool)
- func UserFromContext(ctx context.Context) (*models.User, bool)
- func WithSession(ctx context.Context, sess *models.Session, user *models.User) context.Context
- func WriteError(w http.ResponseWriter, status int, code, message string)
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type APIError
- type Account
- type Adapter
- type Auth
- func (a *Auth) Adapter() Adapter
- func (a *Auth) Config() Config
- func (a *Auth) Handler() http.Handler
- func (a *Auth) InternalAdapter() *InternalAdapter
- func (a *Auth) Plugins() []Plugin
- func (a *Auth) RequireAuth(next http.Handler) http.Handler
- func (a *Auth) RunSessionCreateHooks(w http.ResponseWriter, r *http.Request, userID string) error
- func (a *Auth) RunUserCreateHooks(data map[string]any) map[string]any
- func (a *Auth) Sessions() *session.Manager
- func (a *Auth) SetSessionCookie(w http.ResponseWriter, sess *models.Session)
- type AuthAware
- type Config
- type CookieConfig
- type EmailPasswordConfig
- type Endpoint
- type EndpointProvider
- type FieldDef
- type Initializer
- type InternalAdapter
- func (ia *InternalAdapter) Adapter() adp.Adapter
- func (ia *InternalAdapter) CreateAccount(ctx context.Context, userID, accountID, providerID string, ...) (*models.Account, error)
- func (ia *InternalAdapter) CreateUser(ctx context.Context, email, name string, emailVerified bool) (*models.User, error)
- func (ia *InternalAdapter) CreateUserWithExtra(ctx context.Context, email, name string, emailVerified bool, ...) (*models.User, error)
- func (ia *InternalAdapter) DeleteUser(ctx context.Context, id string) error
- func (ia *InternalAdapter) FindAccountByProviderAndID(ctx context.Context, providerID, accountID string) (*models.Account, error)
- func (ia *InternalAdapter) FindAccountsByUserID(ctx context.Context, userID string) ([]*models.Account, error)
- func (ia *InternalAdapter) FindUserByEmail(ctx context.Context, email string) (*models.User, error)
- func (ia *InternalAdapter) FindUserByID(ctx context.Context, id string) (*models.User, error)
- func (ia *InternalAdapter) UpdatePassword(ctx context.Context, userID, hashedPassword string) error
- func (ia *InternalAdapter) UpdateUser(ctx context.Context, id string, data map[string]any) (*models.User, error)
- type MigrationProvider
- type Plugin
- type Query
- type SchemaProvider
- type Session
- type SessionConfig
- type SessionCreateContext
- type SessionCreateHookFn
- type SessionCreateHookProvider
- type TableSchema
- type User
- type UserCreateHookFn
- type UserCreateHookProvider
- type Where
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidToken = &APIError{Code: "INVALID_TOKEN", Message: "invalid session token", Status: 401} ErrInvalidEmail = &APIError{Code: "INVALID_EMAIL", Message: "invalid email", Status: 400} ErrInvalidPassword = &APIError{Code: "INVALID_CREDENTIALS", Message: "invalid email or password", Status: 400} ErrEmailExists = &APIError{Code: "EMAIL_EXISTS", Message: "email already exists", Status: 409} ErrUserNotFound = &APIError{Code: "USER_NOT_FOUND", Message: "user not found", Status: 404} ErrSessionExpired = &APIError{Code: "SESSION_EXPIRED", Message: "session expired", Status: 401} ErrSessionNotFound = &APIError{Code: "SESSION_NOT_FOUND", Message: "session not found", Status: 401} ErrInternal = &APIError{Code: "INTERNAL", Message: "internal server error", Status: 500} )
var EQ = adp.EQ
EQ constructs an equality Where clause.
var ErrHookHandled = errors.New("hook handled the response")
ErrHookHandled signals that a hook already wrote the HTTP response. Callers must not write again; just return.
Functions ¶
func WithSession ¶
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, code, message string)
Types ¶
type APIError ¶
type APIError struct {
Code string `json:"code"`
Message string `json:"message"`
Status int `json:"status"`
}
func (*APIError) WriteJSON ¶
func (e *APIError) WriteJSON(w http.ResponseWriter)
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth is the composition root. Create with New() and mount Handler() on your server.
func (*Auth) InternalAdapter ¶
func (a *Auth) InternalAdapter() *InternalAdapter
func (*Auth) RequireAuth ¶
RequireAuth middleware validates the session and injects user+session into context. Use this to protect your own application endpoints.
func (*Auth) RunSessionCreateHooks ¶
RunSessionCreateHooks runs all session-create veto hooks. Returns ErrHookHandled if a hook already wrote the response (caller must not write again).
func (*Auth) RunUserCreateHooks ¶
RunUserCreateHooks applies all user-create hooks to data in registration order.
func (*Auth) SetSessionCookie ¶
func (a *Auth) SetSessionCookie(w http.ResponseWriter, sess *models.Session)
SetSessionCookie is a plugin-friendly helper that sets the session cookie using Auth's configured cookie settings. Plugins call this instead of accessing the internal cookieConfig directly.
type AuthAware ¶
type AuthAware interface {
SetAuth(a *Auth)
}
AuthAware — plugin receives a reference to *Auth after New() sets up core state.
type Config ¶
type Config struct {
AppName string
BaseURL string
BasePath string
Secret string
Adapter Adapter
Plugins []Plugin
Session SessionConfig
Cookie CookieConfig
EmailPassword EmailPasswordConfig
TrustedOrigins []string
}
type CookieConfig ¶
type EmailPasswordConfig ¶
type Endpoint ¶
type Endpoint struct {
Method string // "GET", "POST", etc.
Path string // relative path prefixed with BasePath in the router
Handler http.HandlerFunc
}
Endpoint describes a single HTTP endpoint a plugin registers.
type EndpointProvider ¶
type EndpointProvider interface {
Endpoints() []Endpoint
}
EndpointProvider — plugin registers additional HTTP endpoints.
type FieldDef ¶
type FieldDef struct {
Name string
Type string // "text", "boolean", "integer", "timestamp"
Required bool
Unique bool
Ref string // FK reference, e.g. "users.id"
}
FieldDef describes a single column.
type Initializer ¶
type Initializer interface {
Init() error
}
Initializer — plugin needs a one-time init step (validate config, connect to external service, etc.).
type InternalAdapter ¶
type InternalAdapter struct {
// contains filtered or unexported fields
}
InternalAdapter provides typed CRUD methods on top of the raw Adapter interface.
func NewInternalAdapter ¶
func NewInternalAdapter(a adp.Adapter) *InternalAdapter
func (*InternalAdapter) Adapter ¶
func (ia *InternalAdapter) Adapter() adp.Adapter
Adapter returns the raw underlying adapter for plugins that need direct access.
func (*InternalAdapter) CreateAccount ¶
func (*InternalAdapter) CreateUser ¶
func (*InternalAdapter) CreateUserWithExtra ¶
func (*InternalAdapter) DeleteUser ¶
func (ia *InternalAdapter) DeleteUser(ctx context.Context, id string) error
func (*InternalAdapter) FindAccountByProviderAndID ¶
func (*InternalAdapter) FindAccountsByUserID ¶
func (*InternalAdapter) FindUserByEmail ¶
func (*InternalAdapter) FindUserByID ¶
func (*InternalAdapter) UpdatePassword ¶
func (ia *InternalAdapter) UpdatePassword(ctx context.Context, userID, hashedPassword string) error
type MigrationProvider ¶
MigrationProvider — plugin has embedded SQL migration files. The CLI (Issue 006) collects these alongside core migrations, ordered by timestamp.
type Plugin ¶
type Plugin interface {
ID() string
}
Plugin is the base interface — all plugins must implement at minimum ID().
type SchemaProvider ¶
type SchemaProvider interface {
Schema() map[string]TableSchema
}
SchemaProvider — plugin declares the DB columns it adds (for CLI introspection).
type SessionCreateContext ¶
type SessionCreateContext struct {
UserID string
Request *http.Request
Writer http.ResponseWriter
}
SessionCreateContext carries request context for session-create hooks.
type SessionCreateHookFn ¶
type SessionCreateHookFn func(ctx SessionCreateContext) error
SessionCreateHookFn is called before inserting a new session. Return a non-nil error to abort. Return ErrHookHandled if the hook already wrote the response.
type SessionCreateHookProvider ¶
type SessionCreateHookProvider interface {
SessionCreateHooks() []SessionCreateHookFn
}
SessionCreateHookProvider — plugin hooks into session creation.
type TableSchema ¶
type TableSchema struct {
Fields []FieldDef
}
TableSchema describes columns a plugin adds to a table.
type UserCreateHookFn ¶
UserCreateHookFn is called before inserting a new user. Receives and returns the data map so plugins can add or transform fields.
type UserCreateHookProvider ¶
type UserCreateHookProvider interface {
UserCreateHooks() []UserCreateHookFn
}
UserCreateHookProvider — plugin hooks into user creation.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package adapter defines the database adapter interface used by goten core and plugins.
|
Package adapter defines the database adapter interface used by goten core and plugins. |
|
adapters
|
|
|
gorm
module
|
|
|
cmd
|
|
|
goten
module
|
|
|
plugins
|
|
|
admin
module
|
|
|
oauth
module
|
|
|
username
module
|
|
|
Package session provides session lifecycle management and cookie utilities for goten.
|
Package session provides session lifecycle management and cookie utilities for goten. |