authority

package
v0.0.34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrJWTSecretRequired = fmt.Err("JWTSecret", "is", "required")

ErrJWTSecretRequired is returned by any token operation attempted without a secret.

View Source
var PasswordHashCost = bcrypt.DefaultCost

Functions

This section is empty.

Types

type GoogleProvider

type GoogleProvider struct {
	ClientID     string
	ClientSecret string
	RedirectURL  string
}

func (*GoogleProvider) AuthCodeURL

func (p *GoogleProvider) AuthCodeURL(state string) string

func (*GoogleProvider) ExchangeCode

func (p *GoogleProvider) ExchangeCode(code string) (user.OAuthToken, error)

func (*GoogleProvider) GetUserInfo

func (p *GoogleProvider) GetUserInfo(token user.OAuthToken) (user.OAuthUserInfo, error)

func (*GoogleProvider) Name

func (p *GoogleProvider) Name() string

type MicrosoftProvider

type MicrosoftProvider struct {
	ClientID     string
	ClientSecret string
	RedirectURL  string
}

func (*MicrosoftProvider) AuthCodeURL

func (p *MicrosoftProvider) AuthCodeURL(state string) string

func (*MicrosoftProvider) ExchangeCode

func (p *MicrosoftProvider) ExchangeCode(code string) (user.OAuthToken, error)

func (*MicrosoftProvider) GetUserInfo

func (p *MicrosoftProvider) GetUserInfo(token user.OAuthToken) (user.OAuthUserInfo, error)

func (*MicrosoftProvider) Name

func (p *MicrosoftProvider) Name() string

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module is the user/auth/rbac handle. All backend operations are methods on this type. Created exclusively via New().

func New

func New(db *orm.DB, cfg user.Config) (*Module, error)

New initializes the user/rbac schema, warms the cache, and returns a Module handle. This is the ONLY entry point for this package on the backend.

func (*Module) Add

func (m *Module) Add() []any

Add returns all admin-managed CRUDP handlers for registration. The concrete types are private — pass directly to crudp.RegisterHandlers.

Usage: cp.RegisterHandlers(m.Add()...)

func (*Module) AssignLANIP

func (m *Module) AssignLANIP(userID, ip, label string) error

func (*Module) AssignPermission

func (m *Module) AssignPermission(roleID, permissionID string) error

func (*Module) AssignRole

func (m *Module) AssignRole(userID, roleID string) error

func (*Module) Authenticate

func (m *Module) Authenticate() router.Middleware

Authenticate returns a router.Middleware that validates the session (Cookie or Bearer). If valid, it sets the UserId in the context via ctx.SetUserID(id). If invalid, the UserId remains empty ("") indicating an anonymous user.

func (*Module) BeginOAuth

func (m *Module) BeginOAuth(providerName string) (string, error)

func (*Module) Bootstrap

func (m *Module) Bootstrap(s Seed) error

Bootstrap seeds the first user and their initial permissions. NO-OP if the users table is already populated. It does not invent roles or wildcards: it only persists what the Seed declares.

func (*Module) Can

func (m *Module) Can(userID string, resource model.Resource, action model.Action) bool

Can checks if the userID has permission for the resource/action. It also handles security event notification on failure.

func (*Module) CompleteOAuth

func (m *Module) CompleteOAuth(providerName string, ctx router.Context, ip, ua string) (user.User, bool, error)

func (*Module) CreatePermission

func (m *Module) CreatePermission(id, name string, resource model.Resource, action model.Action) error

func (*Module) CreateRole

func (m *Module) CreateRole(id string, code model.RoleCode, name, description string) error

func (*Module) CreateSession

func (m *Module) CreateSession(userID, ip, userAgent string) (user.Session, error)

func (*Module) DeletePermission

func (m *Module) DeletePermission(id string) error

func (*Module) DeleteRole

func (m *Module) DeleteRole(id string) error

func (*Module) DeleteSession

func (m *Module) DeleteSession(id string) error

func (*Module) GenerateAPIToken

func (m *Module) GenerateAPIToken(userID string, ttl int) (string, error)

GenerateAPIToken creates a signed JWT for API access (MCP clients, IDEs, LLMs). Requires Config.JWTSecret — independent of the configured AuthMode. ttl=0 → 50 years (effectively no expiry). Not 100: this module compiles for the edge, where int is 32-bit, and 100 years of seconds overflows int32. The returned token is used as a Bearer token in Authorization headers.

func (*Module) GetLANIPs

func (m *Module) GetLANIPs(userID string) ([]user.LANIP, error)

func (*Module) GetPermission

func (m *Module) GetPermission(id string) (*user.Permission, error)

func (*Module) GetRole

func (m *Module) GetRole(id string) (*user.Role, error)

func (*Module) GetRoleByCode

func (m *Module) GetRoleByCode(code model.RoleCode) (*user.Role, error)

func (*Module) GetSession

func (m *Module) GetSession(id string) (user.Session, error)

func (*Module) GetUser

func (m *Module) GetUser(id string) (user.User, error)

func (*Module) GetUserByEmail

func (m *Module) GetUserByEmail(email string) (user.User, error)

func (*Module) GetUserIdentities

func (m *Module) GetUserIdentities(userID string) ([]user.Identity, error)

func (*Module) GetUserRoles

func (m *Module) GetUserRoles(userID string) ([]user.Role, error)

func (*Module) HasPermission

func (m *Module) HasPermission(userID string, resource model.Resource, action model.Action) (bool, error)

func (*Module) Login

func (m *Module) Login(email, password string) (user.User, error)

func (*Module) LoginLAN

func (m *Module) LoginLAN(rut string, ctx router.Context) (user.User, error)

func (*Module) ModelName

func (m *Module) ModelName() string

ModelName is the module's identity (model.ModuleNaming), used as the RBAC resource and as the key by which a host registers it.

func (*Module) MountAPI

func (m *Module) MountAPI(r router.Router)

MountAPI publishes the authentication flows on the host router. The module owns its routes; consumers just Mount it like any other APIModule.

func (*Module) PurgeExpiredOAuthStates

func (m *Module) PurgeExpiredOAuthStates() error

func (*Module) PurgeExpiredSessions

func (m *Module) PurgeExpiredSessions() error

func (*Module) PurgeSessionsByUser

func (m *Module) PurgeSessionsByUser(userID string) error

PurgeSessionsByUser deletes all sessions belonging to userID from cache and DB.

func (*Module) ReactivateUser

func (m *Module) ReactivateUser(id string) error

ReactivateUser sets Status = "active". Evicts user from cache.

func (*Module) Register

func (m *Module) Register(handlers ...RBACObject) error

func (*Module) RegisterLAN

func (m *Module) RegisterLAN(userID, rut string) error

func (*Module) RevokeLANIP

func (m *Module) RevokeLANIP(userID, ip string) error

func (*Module) RevokeRole

func (m *Module) RevokeRole(userID, roleID string) error

func (*Module) RotateSession

func (m *Module) RotateSession(oldID, ip, userAgent string) (user.Session, error)

RotateSession atomically deletes the old session and creates a new one with the same userID, updated IP/UserAgent, and a fresh TTL. Prevents session fixation attacks when called post-login.

func (*Module) SetLog

func (m *Module) SetLog(fn func(...any))

SetLog configures optional logging. Call immediately after New(). Default: no-op. Follows the tinywasm ecosystem SetLog convention (same as rbac).

Example:

m.SetLog(func(msg ...any) { log.Println(msg...) })

func (*Module) SetPassword

func (m *Module) SetPassword(userID, password string) error

func (*Module) SuspendUser

func (m *Module) SuspendUser(id string) error

SuspendUser sets Status = "suspended". Evicts user from cache.

func (*Module) Tools

func (m *Module) Tools() []mcp.Tool

Tools returns the list of MCP tools provided by the user module.

func (*Module) UnlinkIdentity

func (m *Module) UnlinkIdentity(userID, provider string) error

func (*Module) UnregisterLAN

func (m *Module) UnregisterLAN(userID string) error

func (*Module) VerifyPassword

func (m *Module) VerifyPassword(userID, password string) error

type RBACObject

type RBACObject interface {
	HandlerName() string
	AllowedRoles(action model.Action) []model.RoleCode
}

type Seed

type Seed struct {
	Email    string
	Password string
	Name     string
	Role     model.RoleCode
	Grants   []model.Grant
}

Seed is the INITIAL security policy of the app: declared by the consumer. This library only persists it with hashing and invariants.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL