Documentation
¶
Index ¶
- Constants
- Variables
- func GetRoles(c *gin.Context) ([]string, bool)
- func GetUserID(c *gin.Context) (string, bool)
- func GetUsername(c *gin.Context) (string, bool)
- func HasRole(c *gin.Context, role string) bool
- func Module(scope string) fx.Option
- type AccessTokenClaims
- type AuthConfig
- type AuthManager
- func (m *AuthManager) CleanupExpiredTokens(ctx context.Context) (int64, error)
- func (m *AuthManager) GetActiveRefreshTokens(ctx context.Context, userID string) ([]*RefreshTokenInfo, error)
- func (m *AuthManager) GetMiddleware(name string) interface{}
- func (m *AuthManager) InitDefaultConfigs()
- func (m *AuthManager) Login(ctx context.Context, identifier, password string) (*TokenPair, error)
- func (m *AuthManager) Logout(ctx context.Context, refreshToken string) error
- func (m *AuthManager) LogoutAll(ctx context.Context, userID string) error
- func (m *AuthManager) OnStart(ctx context.Context) error
- func (m *AuthManager) OnStop(ctx context.Context) error
- func (m *AuthManager) RefreshTokens(ctx context.Context, refreshToken string) (*TokenPair, error)
- func (m *AuthManager) ValidateAccessToken(tokenString string) (*AccessTokenClaims, error)
- type CustomClaims
- type MiddlewareFunc
- type MiddlewareWithParamFunc
- type Params
- type RefreshTokenInfo
- type Session
- type TokenPair
Constants ¶
const ( // HeaderAuthorization is the Authorization header name HeaderAuthorization = "Authorization" // HeaderUserInfo is the header name for passing user info (for ingress integration) HeaderUserInfo = "X-User-Info" // ContextKeySession is the key for storing session info in gin context ContextKeySession = "auth_session" // ContextKeyUserID is the key for storing user ID in gin context ContextKeyUserID = "auth_user_id" // ContextKeyUsername is the key for storing username in gin context ContextKeyUsername = "auth_username" // ContextKeyRoles is the key for storing user roles in gin context ContextKeyRoles = "auth_roles" )
const ModuleName = "Auth"
Variables ¶
var ( // ErrInvalidCredentials indicates invalid username/email or password ErrInvalidCredentials = errors.New("invalid credentials") // ErrInvalidToken indicates the token is invalid or malformed ErrInvalidToken = errors.New("invalid token") // ErrTokenExpired indicates the token has expired ErrTokenExpired = errors.New("token expired") // ErrTokenRevoked indicates the refresh token has been revoked ErrTokenRevoked = errors.New("token revoked") // ErrTokenNotFound indicates the refresh token was not found ErrTokenNotFound = errors.New("token not found") // ErrUserInactive indicates the user account is not active ErrUserInactive = errors.New("user account is not active") // ErrOperationFailed indicates a general operation failure ErrOperationFailed = errors.New("operation failed") )
Functions ¶
func GetUsername ¶
GetUsername is a helper function to get username from gin context
Types ¶
type AccessTokenClaims ¶
type AccessTokenClaims struct {
UserID string `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Roles []string `json:"roles"`
DisplayName string `json:"display_name"`
}
AccessTokenClaims represents the claims in an access token
type AuthConfig ¶
type AuthConfig struct {
// JWT secret key for signing tokens
JWTSecret string
// Access token expiration duration
AccessTokenExpiry time.Duration
// Refresh token expiration duration
RefreshTokenExpiry time.Duration
// Token issuer
Issuer string
}
AuthConfig contains configuration for authentication
type AuthManager ¶
func (*AuthManager) CleanupExpiredTokens ¶
func (m *AuthManager) CleanupExpiredTokens(ctx context.Context) (int64, error)
CleanupExpiredTokens removes expired refresh tokens from the database
func (*AuthManager) GetActiveRefreshTokens ¶
func (m *AuthManager) GetActiveRefreshTokens(ctx context.Context, userID string) ([]*RefreshTokenInfo, error)
GetActiveRefreshTokens returns all active refresh tokens for a user
func (*AuthManager) GetMiddleware ¶
func (m *AuthManager) GetMiddleware(name string) interface{}
GetMiddleware returns a middleware by name Supported middlewares:
- "authenticate": validates JWT token and sets X-User-Info header
- "require_permission": reads X-User-Info header and sets session in context
func (*AuthManager) InitDefaultConfigs ¶
func (m *AuthManager) InitDefaultConfigs()
func (*AuthManager) Logout ¶
func (m *AuthManager) Logout(ctx context.Context, refreshToken string) error
Logout revokes a refresh token
func (*AuthManager) LogoutAll ¶
func (m *AuthManager) LogoutAll(ctx context.Context, userID string) error
LogoutAll revokes all refresh tokens for a user
func (*AuthManager) RefreshTokens ¶
RefreshTokens validates a refresh token and returns a new token pair
func (*AuthManager) ValidateAccessToken ¶
func (m *AuthManager) ValidateAccessToken(tokenString string) (*AccessTokenClaims, error)
ValidateAccessToken validates an access token and returns the claims
type CustomClaims ¶
type CustomClaims struct {
jwt.RegisteredClaims
UserID string `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Roles []string `json:"roles"`
DisplayName string `json:"display_name"`
}
CustomClaims represents JWT claims with user information
type MiddlewareFunc ¶
type MiddlewareFunc = gin.HandlerFunc
MiddlewareFunc is the type for middleware functions
type MiddlewareWithParamFunc ¶
type MiddlewareWithParamFunc = func(param string) gin.HandlerFunc
MiddlewareWithParamFunc is the type for middleware functions that accept parameters
type Params ¶
type Params struct {
weedbox.Params
Database database.DatabaseConnector
User *user.UserManager `name:"user"`
RBAC *rbac.RBACManager `name:"rbac"`
}
type RefreshTokenInfo ¶
type RefreshTokenInfo struct {
ID string `json:"id"`
UserID string `json:"user_id"`
ExpiresAt time.Time `json:"expires_at"`
Revoked bool `json:"revoked"`
CreatedAt time.Time `json:"created_at"`
}
RefreshTokenInfo contains information about a refresh token
type Session ¶
type Session struct {
UserID string `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Roles []string `json:"roles"`
DisplayName string `json:"display_name"`
}
Session represents the authenticated user session
type TokenPair ¶
type TokenPair struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"` // Access token expiry in seconds
ExpiresAt time.Time `json:"expires_at"` // Access token expiry timestamp
RefreshExpiresAt time.Time `json:"refresh_expires_at"` // Refresh token expiry timestamp
}
TokenPair contains both access and refresh tokens