initial commit
This commit is contained in:
commit
6ffc0d2c99
54 changed files with 11900 additions and 0 deletions
75
backend/internal/security/security.go
Normal file
75
backend/internal/security/security.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package security
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var ErrInvalidToken = errors.New("invalid token")
|
||||
|
||||
type Claims struct {
|
||||
UserID uint `json:"userId"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
type ITokenManager interface {
|
||||
Issue(uint, string, string) (string, error)
|
||||
Parse(string) (Claims, error)
|
||||
}
|
||||
|
||||
type TokenManager struct {
|
||||
secret []byte
|
||||
ttl time.Duration
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewTokenManager(secret string, ttl time.Duration) *TokenManager {
|
||||
return &TokenManager{secret: []byte(secret), ttl: ttl, now: time.Now}
|
||||
}
|
||||
|
||||
func (m *TokenManager) Issue(userID uint, email, role string) (string, error) {
|
||||
now := m.now()
|
||||
claims := Claims{
|
||||
UserID: userID, Email: email, Role: role,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Subject: fmt.Sprint(userID), IssuedAt: jwt.NewNumericDate(now),
|
||||
ExpiresAt: jwt.NewNumericDate(now.Add(m.ttl)),
|
||||
},
|
||||
}
|
||||
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(m.secret)
|
||||
}
|
||||
|
||||
func (m *TokenManager) Parse(raw string) (Claims, error) {
|
||||
token, err := jwt.ParseWithClaims(raw, &Claims{}, func(token *jwt.Token) (any, error) {
|
||||
if token.Method != jwt.SigningMethodHS256 {
|
||||
return nil, ErrInvalidToken
|
||||
}
|
||||
return m.secret, nil
|
||||
})
|
||||
if err != nil || !token.Valid {
|
||||
return Claims{}, ErrInvalidToken
|
||||
}
|
||||
claims, ok := token.Claims.(*Claims)
|
||||
if !ok {
|
||||
return Claims{}, ErrInvalidToken
|
||||
}
|
||||
return *claims, nil
|
||||
}
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
value, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("hash password: %w", err)
|
||||
}
|
||||
return string(value), nil
|
||||
}
|
||||
|
||||
func CheckPassword(hash, password string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
||||
}
|
||||
33
backend/internal/security/security_test.go
Normal file
33
backend/internal/security/security_test.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package security
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTokenRoundTrip(t *testing.T) {
|
||||
manager := NewTokenManager("01234567890123456789012345678901", time.Hour)
|
||||
raw, err := manager.Issue(42, "interviewer@example.com", "interviewer")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
claims, err := manager.Parse(raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if claims.UserID != 42 || claims.Email != "interviewer@example.com" || claims.Role != "interviewer" {
|
||||
t.Fatalf("unexpected claims: %#v", claims)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRejectsTokenSignedWithAnotherSecret(t *testing.T) {
|
||||
issuer := NewTokenManager("01234567890123456789012345678901", time.Hour)
|
||||
verifier := NewTokenManager("abcdefghijklmnopqrstuvwxyz123456", time.Hour)
|
||||
raw, err := issuer.Issue(1, "user@example.com", "interviewer")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := verifier.Parse(raw); err == nil {
|
||||
t.Fatal("expected token validation to fail")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue