Documentation
¶
Overview ¶
local.go: Handles login, registration, password validation.
mfa.go: Uses TOTP to generate QR codes/secrets and verify codes.
session.go: Handles signed cookies or token generation/validation.
Index ¶
- Variables
- func AccessLogMiddleware(next http.Handler) http.Handler
- func AuthMiddleware(userStore userstore.UserStore) mux.MiddlewareFunc
- func CheckPasswordHash(password, hash string) bool
- func CheckRememberMFA(r *http.Request, userID string) bool
- func ClearCookie(w http.ResponseWriter, name string)
- func ClearRememberMFA(w http.ResponseWriter)
- func ExtractRoleNames(roles []usermodel.Role) []string
- func FlattenPermissions(roles []usermodel.Role) []string
- func GenerateTOTPSecret(email string) (string, error)
- func GenerateToken(userID string, roles []string, traceID string) (string, error)
- func GetSessionToken(r *http.Request) (string, error)
- func GetSessionUserID(r *http.Request) (string, error)
- func HasAnyPermission(ctx context.Context, required ...string) bool
- func HasAnyRole(roles []string, requiredRoles ...string) bool
- func HasPermission(ctx context.Context, required string) bool
- func HasRole(roles []string, required string) bool
- func HashPassword(password string) (string, error)
- func InitJWTSecret(encoded string) error
- func InitMFAKey(encoded string) error
- func InjectSessionContext(ctx context.Context, user *usermodel.User) context.Context
- func LoadPendingMFA(r *http.Request) (string, error)
- func RequireAnyPermissionWithStore(store userstore.UserStore, required ...string) func(http.Handler) http.Handler
- func RequirePermission(required string, next http.Handler, userStore userstore.UserStore) http.Handler
- func SavePendingMFA(userID string, w http.ResponseWriter)
- func SetRememberMFA(w http.ResponseWriter, userID string, r *http.Request)
- func SetSessionCookie(w http.ResponseWriter, token string)
- func ValidateTOTP(secret, code string) bool
- type AuthProvider
- type GoogleAuth
- type LocalAuth
- type SessionClaims
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidPassword = errors.New("invalid password") ErrInvalidTOTP = errors.New("invalid TOTP code") ErrUserNotFound = errors.New("user not found") )
var ErrNoSession = errors.New("no session token found")
Functions ¶
func AuthMiddleware ¶
func AuthMiddleware(userStore userstore.UserStore) mux.MiddlewareFunc
func CheckPasswordHash ¶
func ClearCookie ¶
func ClearCookie(w http.ResponseWriter, name string)
ClearCookie clears a cookie by setting its MaxAge to -1 and Expiration to the past
func ClearRememberMFA ¶
func ClearRememberMFA(w http.ResponseWriter)
func ExtractRoleNames ¶
func FlattenPermissions ¶
func GenerateToken ¶
func GetSessionToken ¶
GetSessionToken retrieves the session token from cookie or header
func GetSessionUserID ¶
Convenience: get user ID from session token in request
func HasAnyRole ¶
func HasPermission ¶
HasPermission checks if the current context includes the given permission
func InitJWTSecret ¶
func InitMFAKey ¶
func InjectSessionContext ¶
func LoadPendingMFA ¶
LoadPendingMFA retrieves the pending MFA cookie
func RequirePermission ¶
func SavePendingMFA ¶
func SavePendingMFA(userID string, w http.ResponseWriter)
SavePendingMFA sets a cookie to remember the pending MFA for 5 minutes
func SetRememberMFA ¶
func SetRememberMFA(w http.ResponseWriter, userID string, r *http.Request)
SetRememberMFA sets a cookie to remember the MFA for 30 days - bind it to device
func SetSessionCookie ¶
func SetSessionCookie(w http.ResponseWriter, token string)
func ValidateTOTP ¶
Types ¶
type AuthProvider ¶
type GoogleAuth ¶
func (*GoogleAuth) HandleCallback ¶
func (g *GoogleAuth) HandleCallback(w http.ResponseWriter, r *http.Request) (*usermodel.User, error)
func (*GoogleAuth) StartLogin ¶
func (g *GoogleAuth) StartLogin(w http.ResponseWriter, r *http.Request)
type LocalAuth ¶
func (*LocalAuth) HandleCallback ¶
func (*LocalAuth) StartLogin ¶
func (l *LocalAuth) StartLogin(w http.ResponseWriter, r *http.Request)
type SessionClaims ¶
type SessionClaims struct {
UserID string `json:"sub"`
Roles []string `json:"roles,omitempty"`
TraceID string `json:"trace_id,omitempty"`
RolesRefreshedAt int64 `json:"roles_refreshed_at"`
jwt.RegisteredClaims
}
func GetSessionClaims ¶
func GetSessionClaims(r *http.Request) (*SessionClaims, error)
GetSessionClaims retrieves the session claims from the request
func ValidateToken ¶
func ValidateToken(tokenStr string) (*SessionClaims, error)