Documentation
¶
Index ¶
- Constants
- func CheckPassword(password, hash string) bool
- func GetUserFromContext(ctx context.Context) *models.User
- func GetUserIDFromContext(c echo.Context) (int, bool)
- func HashPassword(password string) (string, error)
- type JWTClaims
- type LoginPayload
- type MeResponse
- type Middleware
- func (m *Middleware) Authenticate(next echo.HandlerFunc) echo.HandlerFunc
- func (m *Middleware) AuthenticateOptional(next echo.HandlerFunc) echo.HandlerFunc
- func (m *Middleware) BasicAuth(next echo.HandlerFunc) echo.HandlerFunc
- func (m *Middleware) RequireLibraryAccess(paramName string) echo.MiddlewareFunc
- func (m *Middleware) RequirePermission(resource, operation string) echo.MiddlewareFunc
- type Service
- func (s *Service) Authenticate(ctx context.Context, username, password string) (*models.User, error)
- func (s *Service) CountUsers(ctx context.Context) (int, error)
- func (s *Service) CreateFirstAdmin(ctx context.Context, username string, email *string, password string) (*models.User, error)
- func (s *Service) GenerateToken(user *models.User) (string, error)
- func (s *Service) GetUserByID(ctx context.Context, id int) (*models.User, error)
- func (s *Service) ValidateToken(tokenString string) (*JWTClaims, error)
- type SetupPayload
- type StatusResponse
Constants ¶
const ( // CookieName is the name of the session cookie. CookieName = "shisho_session" // CookieMaxAge is how long the cookie is valid. CookieMaxAge = 7 * 24 * time.Hour // 7 days )
const ( ContextKeyUserID contextKey = "user_id" ContextKeyUsername contextKey = "username" ContextKeyUser contextKey = "user" )
const ( // BcryptCost is the cost factor for bcrypt hashing. BcryptCost = 12 // TokenExpiry is how long JWT tokens are valid. TokenExpiry = 7 * 24 * time.Hour // 7 days )
Variables ¶
This section is empty.
Functions ¶
func CheckPassword ¶
CheckPassword compares a password with a hash.
func GetUserFromContext ¶
GetUserFromContext retrieves the user from the context.
func GetUserIDFromContext ¶
GetUserIDFromContext retrieves the user ID from the Echo context.
func HashPassword ¶
HashPassword hashes a password using bcrypt.
Types ¶
type JWTClaims ¶
type JWTClaims struct {
UserID int `json:"user_id"`
Username string `json:"username"`
jwt.RegisteredClaims
}
JWTClaims represents the claims in a JWT token.
type LoginPayload ¶
type LoginPayload struct {
Username string `json:"username" validate:"required,min=3,max=50"`
Password string `json:"password" validate:"required,min=8"`
}
LoginPayload represents the login request body.
type MeResponse ¶
type MeResponse struct {
ID int `json:"id"`
Username string `json:"username"`
Email *string `json:"email,omitempty"`
RoleID int `json:"role_id"`
RoleName string `json:"role_name"`
Permissions []string `json:"permissions"`
LibraryAccess *[]int `json:"library_access"` // nil = all libraries, empty = none, populated = specific libraries
MustChangePassword bool `json:"must_change_password"`
}
MeResponse represents the current user response.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware provides authentication middleware.
func NewMiddleware ¶
func NewMiddleware(authService *Service) *Middleware
NewMiddleware creates a new auth middleware.
func (*Middleware) Authenticate ¶
func (m *Middleware) Authenticate(next echo.HandlerFunc) echo.HandlerFunc
Authenticate extracts and validates the JWT from the cookie. If valid, it verifies the user is still active and adds user info to the context. If not authenticated, it returns 401.
func (*Middleware) AuthenticateOptional ¶
func (m *Middleware) AuthenticateOptional(next echo.HandlerFunc) echo.HandlerFunc
AuthenticateOptional extracts user info if available but doesn't require authentication. If a valid token is present, it verifies the user is still active.
func (*Middleware) BasicAuth ¶
func (m *Middleware) BasicAuth(next echo.HandlerFunc) echo.HandlerFunc
BasicAuth provides HTTP Basic Auth for OPDS endpoints.
func (*Middleware) RequireLibraryAccess ¶
func (m *Middleware) RequireLibraryAccess(paramName string) echo.MiddlewareFunc
RequireLibraryAccess returns middleware that checks if the user can access the library specified by the :libraryId or :id route parameter. Must be used after Authenticate middleware.
func (*Middleware) RequirePermission ¶
func (m *Middleware) RequirePermission(resource, operation string) echo.MiddlewareFunc
RequirePermission returns middleware that checks if the user has the required permission. Must be used after Authenticate middleware.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles authentication operations.
func NewService ¶
NewService creates a new auth service.
func RegisterRoutes ¶
RegisterRoutes registers all auth routes.
func (*Service) Authenticate ¶
func (s *Service) Authenticate(ctx context.Context, username, password string) (*models.User, error)
Authenticate validates credentials and returns the user if valid.
func (*Service) CountUsers ¶
CountUsers returns the total number of users.
func (*Service) CreateFirstAdmin ¶
func (s *Service) CreateFirstAdmin(ctx context.Context, username string, email *string, password string) (*models.User, error)
CreateFirstAdmin creates the first admin user during setup.
func (*Service) GenerateToken ¶
GenerateToken creates a new JWT token for the user.
func (*Service) GetUserByID ¶
GetUserByID retrieves a user by ID with relations.
type SetupPayload ¶
type SetupPayload struct {
Username string `json:"username" validate:"required,min=3,max=50"`
Email *string `json:"email" validate:"omitempty,email"`
Password string `json:"password" validate:"required,min=8"`
}
SetupPayload represents the initial setup request body.
type StatusResponse ¶
type StatusResponse struct {
NeedsSetup bool `json:"needs_setup"`
}
StatusResponse represents the auth status response.