Documentation
¶
Index ¶
- Constants
- Variables
- func GenerateToken(username, role string, expirationMinutes int) (string, error)
- func RetryOperation(operation func() error, maxRetries int, retryDelay time.Duration) error
- type Claims
- type UserService
- func (s *UserService) AddUser(user cmn.User) (int, error)
- func (s *UserService) DeleteUser(id int) error
- func (s *UserService) GetUsers() ([]cmn.User, error)
- func (s *UserService) Login(username, password string) (string, bool, error)
- func (s *UserService) Logout(tokenString string) error
- func (s *UserService) UpdateUser(user cmn.User) error
- func (s *UserService) UserServiceTicker()
- func (s *UserService) ValidatePassword(username, password string) error
- func (s *UserService) ValidateToken(token string) (interface{}, error)
- func (s *UserService) ValidateUser(username, password string) (string, bool, error)
Constants ¶
const ( CacheExpirationTime = 5 // 5 minutes CacheCleanupInterval = 10 // 10 minutes )
Variables ¶
var ( ErrTokenExpired = errors.New("token is expired") ErrInvalidToken = errors.New("invalid token") )
jwtKey is the secret key used for signing JWT tokens.
Functions ¶
func GenerateToken ¶
GenerateToken generates a JWT token for a given username with a specified expiration time.
Parameters:
- username: the username to be included in the token claims.
- expirationMinutes: the number of minutes until the token expires.
Returns:
- string: the generated JWT token.
- error: an error if the token generation fails.
Types ¶
type Claims ¶
type Claims struct { Username string `json:"username"` Role string `json:"role"` jwt.StandardClaims }
Claims represents the structure of the JWT claims.
type UserService ¶
func NewUserService ¶
func NewUserService() *UserService
NewUserService creates a new UserService instance.
func (*UserService) AddUser ¶
func (s *UserService) AddUser(user cmn.User) (int, error)
ValidateUser validates the user credentials. It returns the user's role if the credentials are valid, or an error if the credentials are invalid.
func (*UserService) DeleteUser ¶
func (s *UserService) DeleteUser(id int) error
DeleteUser deletes a user from the database.
func (*UserService) GetUsers ¶
func (s *UserService) GetUsers() ([]cmn.User, error)
GetUsers returns all users from the database.
func (*UserService) Login ¶
func (s *UserService) Login(username, password string) (string, bool, error)
func (*UserService) Logout ¶
func (s *UserService) Logout(tokenString string) error
Logout deletes the token from the cache and the database.
func (*UserService) UpdateUser ¶
func (s *UserService) UpdateUser(user cmn.User) error
UpdateUser updates a user in the database.
func (*UserService) UserServiceTicker ¶
func (s *UserService) UserServiceTicker()
UserServiceTicker is a periodic function that runs every 10 seconds.
func (*UserService) ValidatePassword ¶
func (s *UserService) ValidatePassword(username, password string) error
Public wrapper function
func (*UserService) ValidateToken ¶
func (s *UserService) ValidateToken(token string) (interface{}, error)
ValidateToken validates a token using the in-memory cache and the database as a fallback.
func (*UserService) ValidateUser ¶
func (s *UserService) ValidateUser(username, password string) (string, bool, error)
UserService provides user-related operations such as user validation and token generation.