56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Claims struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Role string `json:"role"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func GenerateAccessToken(userID uuid.UUID, role string, secret string, ttlMinutes int) (string, time.Time, error) {
|
|
expirationTime := time.Now().Add(time.Duration(ttlMinutes) * time.Minute)
|
|
claims := &Claims{
|
|
UserID: userID,
|
|
Role: role,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(expirationTime),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
tokenString, err := token.SignedString([]byte(secret))
|
|
return tokenString, expirationTime, err
|
|
}
|
|
|
|
func ValidateToken(tokenString string, secret string) (*Claims, error) {
|
|
claims := &Claims{}
|
|
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(secret), nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !token.Valid {
|
|
return nil, jwt.ErrSignatureInvalid
|
|
}
|
|
|
|
return claims, nil
|
|
}
|
|
|
|
func GenerateRefreshToken(userID uuid.UUID, secret string, ttlDays int) (string, error) {
|
|
// Simple refresh token generation (could be improved with DB storage/validation)
|
|
// For now, let's use a long-lived JWT or just a random string if stored in DB.
|
|
// Since we are storing it in DB (RefreshToken table), a random string is better.
|
|
// But the service code I wrote earlier expects a string.
|
|
// Let's generate a random UUID string.
|
|
return uuid.New().String(), nil
|
|
}
|