This commit is contained in:
59
internal/infrastructure/security/jwt.go
Normal file
59
internal/infrastructure/security/jwt.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
// JWTService interface defines the contract for JWT operations
|
||||
type JWTService interface {
|
||||
GenerateToken(userID int, username string) (string, error)
|
||||
ValidateToken(tokenString string) (jwt.MapClaims, error)
|
||||
}
|
||||
|
||||
// JWTServiceImpl implements JWTService using golang-jwt
|
||||
type JWTServiceImpl struct {
|
||||
secret []byte
|
||||
}
|
||||
|
||||
// NewJWTService creates a new JWT service
|
||||
func NewJWTService(secret string) JWTService {
|
||||
return &JWTServiceImpl{
|
||||
secret: []byte(secret),
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateToken generates a JWT token for a user
|
||||
func (j *JWTServiceImpl) GenerateToken(userID int, username string) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
"username": username,
|
||||
"exp": time.Now().Add(time.Hour * 24 * 7).Unix(), // 7 days
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString(j.secret)
|
||||
}
|
||||
|
||||
// ValidateToken validates a JWT token and returns the claims
|
||||
func (j *JWTServiceImpl) ValidateToken(tokenString string) (jwt.MapClaims, error) {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return j.secret, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
29
internal/infrastructure/security/password.go
Normal file
29
internal/infrastructure/security/password.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package security
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// PasswordService interface defines the contract for password operations
|
||||
type PasswordService interface {
|
||||
HashPassword(password string) (string, error)
|
||||
CheckPasswordHash(password, hash string) bool
|
||||
}
|
||||
|
||||
// PasswordServiceImpl implements PasswordService using bcrypt
|
||||
type PasswordServiceImpl struct{}
|
||||
|
||||
// NewPasswordService creates a new password service
|
||||
func NewPasswordService() PasswordService {
|
||||
return &PasswordServiceImpl{}
|
||||
}
|
||||
|
||||
// HashPassword hashes a password using bcrypt
|
||||
func (p *PasswordServiceImpl) HashPassword(password string) (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
// CheckPasswordHash compares a password with its hash
|
||||
func (p *PasswordServiceImpl) CheckPasswordHash(password, hash string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user