user

package
v0.9.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CacheExpirationTime  = 5  // 5 minutes
	CacheCleanupInterval = 10 // 10 minutes
)

Variables

View Source
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

func GenerateToken(username, role string, expirationMinutes int) (string, error)

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.

func RetryOperation

func RetryOperation(operation func() error, maxRetries int, retryDelay time.Duration) error

RetryOperation retries the given operation function up to maxRetries times with a delay between retries.

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

type UserService struct {
	DB    *sql.DB
	Cache *cache.Cache
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL