Documentation
¶
Overview ¶
Example usage of creating an Auth instance
Example demonstrating authorization concepts ¶
Example usage of the Auth module configuration ¶
Example demonstrating context utilities for authentication ¶
Example demonstrating error handling in the auth module ¶
Example usage of the Auth middleware ¶
Example usage of the Auth module for a quick start ¶
Example demonstrating the concept of token handling in authentication ¶
Example demonstrating how to get user information from the context
Index ¶
- Variables
- func AddUserIDToContext(ctx context.Context, userID string) context.Context
- func AddUserIDToCtx(ctx context.Context, userID string) context.Context
- func AddUserRolesToCtx(ctx context.Context, roles []string) context.Context
- func GetContext(err error, key string) (string, bool)
- func GetMessage(err error) (string, bool)
- func GetOp(err error) (string, bool)
- func GetUserIDFromContext(ctx context.Context) (string, bool)
- func GetUserIDFromCtx(ctx context.Context) (string, bool)
- func GetUserRolesFromCtx(ctx context.Context) ([]string, bool)
- func IsUserAuthenticated(ctx context.Context) bool
- func SimulateTokenValidation(token string) error
- func WithUserID(ctx context.Context, userID string) context.Context
- type AuthService
- type ContextError
- type ContextKey
- type MockAuth
- type MockClaims
- type UserInfo
- type UserInfoService
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidToken = errors.New("invalid token") ErrTokenExpired = errors.New("token expired") ErrInvalidSignature = errors.New("invalid signature") ErrMissingClaims = errors.New("missing claims") )
Define error types
Functions ¶
func AddUserIDToContext ¶
AddUserIDToContext adds a user ID to the context
func AddUserIDToCtx ¶
AddUserIDToCtx adds a user ID to the context
func AddUserRolesToCtx ¶
AddUserRolesToCtx adds user roles to the context
func GetContext ¶
GetContext gets context information from an error
func GetUserIDFromContext ¶
GetUserIDFromContext gets the user ID from the context
func GetUserIDFromCtx ¶
GetUserIDFromCtx gets the user ID from the context
func GetUserRolesFromCtx ¶
GetUserRolesFromCtx gets the user roles from the context
func IsUserAuthenticated ¶
IsUserAuthenticated checks if the user is authenticated
func SimulateTokenValidation ¶
SimulateTokenValidation simulates token validation with error handling
Types ¶
type AuthService ¶
type AuthService struct {
// contains filtered or unexported fields
}
AuthService simulates an auth service with authorization capabilities
func (*AuthService) IsAdmin ¶
func (a *AuthService) IsAdmin(ctx context.Context) (bool, error)
IsAdmin checks if the user has admin role
func (*AuthService) IsAuthorized ¶
IsAuthorized checks if the user is authorized to perform an operation
type ContextError ¶
type ContextError struct {
Op string // Operation that caused the error
Err error // Original error
Message string // Human-readable error message
Context map[string]string // Additional context information
}
ContextError represents an error with context information
func NewError ¶
func NewError(op string, err error, message string) *ContextError
NewError creates a new context error
func (*ContextError) Error ¶
func (e *ContextError) Error() string
Error implements the error interface
func (*ContextError) Unwrap ¶
func (e *ContextError) Unwrap() error
Unwrap returns the wrapped error
func (*ContextError) WithContext ¶
func (e *ContextError) WithContext(key, value string) *ContextError
WithContext adds context information to the error
type ContextKey ¶
type ContextKey string
ContextKey is a type for context keys to avoid collisions
const ( UserIDKey ContextKey = "userID" UserRolesKey ContextKey = "userRoles" )
Context keys
type MockAuth ¶
type MockAuth struct {
// contains filtered or unexported fields
}
MockAuth simulates an auth service
func NewMockAuth ¶
NewMockAuth creates a new mock auth service
func (*MockAuth) GenerateToken ¶
GenerateToken creates a mock token
func (*MockAuth) ValidateToken ¶
func (a *MockAuth) ValidateToken(token string) (*MockClaims, error)
ValidateToken validates a mock token and returns claims
type MockClaims ¶
MockClaims represents the data stored in a JWT token
type UserInfoService ¶
type UserInfoService struct {
// contains filtered or unexported fields
}
UserInfoService simulates a user information service
func NewUserInfoService ¶
func NewUserInfoService() *UserInfoService
NewUserInfoService creates a new user information service
func (*UserInfoService) GetUserEmail ¶
func (a *UserInfoService) GetUserEmail(ctx context.Context) (string, error)
GetUserEmail gets the user email from the context
func (*UserInfoService) GetUserID ¶
func (a *UserInfoService) GetUserID(ctx context.Context) (string, error)
GetUserID gets the user ID from the context
func (*UserInfoService) GetUserRoles ¶
func (a *UserInfoService) GetUserRoles(ctx context.Context) ([]string, error)
GetUserRoles gets the user roles from the context