cambios para worker-remoto
This commit is contained in:
parent
98e0cd0e46
commit
be56de7dd4
19 changed files with 1544 additions and 216 deletions
55
backend/internal/auth/jwt.go
Normal file
55
backend/internal/auth/jwt.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
var jwtSecret []byte
|
||||
|
||||
func SetJWTSecret(secret string) {
|
||||
jwtSecret = []byte(secret)
|
||||
}
|
||||
|
||||
func GetJWTSecret() []byte {
|
||||
return jwtSecret
|
||||
}
|
||||
|
||||
type Claims struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Email string `json:"email"`
|
||||
Username string `json:"username"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func GenerateToken(userID int64, email, username string, isAdmin bool) (string, error) {
|
||||
expirationTime := time.Now().Add(24 * time.Hour)
|
||||
claims := &Claims{
|
||||
UserID: userID,
|
||||
Email: email,
|
||||
Username: username,
|
||||
IsAdmin: isAdmin,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(expirationTime),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString(jwtSecret)
|
||||
}
|
||||
|
||||
func ValidateToken(tokenString string) (*Claims, error) {
|
||||
claims := &Claims{}
|
||||
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
||||
return jwtSecret, nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
96
backend/internal/auth/jwt_test.go
Normal file
96
backend/internal/auth/jwt_test.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGenerateAndValidateToken(t *testing.T) {
|
||||
SetJWTSecret("test-secret-key-12345")
|
||||
|
||||
token, err := GenerateToken(1, "test@example.com", "testuser", true)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateToken failed: %v", err)
|
||||
}
|
||||
|
||||
if token == "" {
|
||||
t.Fatal("token should not be empty")
|
||||
}
|
||||
|
||||
claims, err := ValidateToken(token)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateToken failed: %v", err)
|
||||
}
|
||||
|
||||
if claims.UserID != 1 {
|
||||
t.Errorf("expected userID 1, got %d", claims.UserID)
|
||||
}
|
||||
if claims.Email != "test@example.com" {
|
||||
t.Errorf("expected email test@example.com, got %s", claims.Email)
|
||||
}
|
||||
if claims.Username != "testuser" {
|
||||
t.Errorf("expected username testuser, got %s", claims.Username)
|
||||
}
|
||||
if !claims.IsAdmin {
|
||||
t.Error("expected isAdmin true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateInvalidToken(t *testing.T) {
|
||||
SetJWTSecret("test-secret-key-12345")
|
||||
|
||||
_, err := ValidateToken("invalid-token")
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateTokenWithWrongSecret(t *testing.T) {
|
||||
SetJWTSecret("secret-1")
|
||||
|
||||
token, err := GenerateToken(1, "test@example.com", "testuser", false)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateToken failed: %v", err)
|
||||
}
|
||||
|
||||
SetJWTSecret("secret-2")
|
||||
|
||||
_, err = ValidateToken(token)
|
||||
if err == nil {
|
||||
t.Error("expected error for token with different secret")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaimsHaveExpiration(t *testing.T) {
|
||||
SetJWTSecret("test-secret-key-12345")
|
||||
|
||||
token, err := GenerateToken(1, "test@example.com", "testuser", false)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateToken failed: %v", err)
|
||||
}
|
||||
|
||||
claims, err := ValidateToken(token)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateToken failed: %v", err)
|
||||
}
|
||||
|
||||
if claims.ExpiresAt == nil {
|
||||
t.Error("expected expiration time to be set")
|
||||
}
|
||||
|
||||
expTime := claims.ExpiresAt.Time
|
||||
expectedExpiration := time.Now().Add(24 * time.Hour)
|
||||
|
||||
if expTime.Sub(expectedExpiration) > time.Minute {
|
||||
t.Errorf("expiration time should be ~24 hours from now, got %v", expTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyToken(t *testing.T) {
|
||||
SetJWTSecret("test-secret-key-12345")
|
||||
|
||||
_, err := ValidateToken("")
|
||||
if err == nil {
|
||||
t.Error("expected error for empty token")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue