internal/auth/auth.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
package auth import ( "errors" "fmt" "time" "github.com/golang-jwt/jwt/v5" "github.com/google/uuid" "golang.org/x/crypto/bcrypt" "argus-core/internal/database" ) // Request types type RegisterRequest struct { Email string `json:"email"` Password string `json:"password"` } type LoginRequest struct { Email string `json:"email"` Password string `json:"password"` } type CreateAPIKeyRequest struct { Name string `json:"name"` ExpiresAt *time.Time `json:"expires_at,omitempty"` } var ( ErrInvalidCredentials = errors.New("invalid credentials") ErrUserExists = errors.New("user already exists") ErrInvalidToken = errors.New("invalid token") ErrAPIKeyNotFound = errors.New("API key not found") ) type Service interface { // User authentication Register(email, password string) (*database.User, error) Login(email, password string) (string, *database.User, error) // Returns JWT token and user ValidateToken(token string) (*database.User, error) // API Key management CreateAPIKey(userID uuid.UUID, name string, expiresAt *time.Time) (*database.APIKey, string, error) // Returns APIKey and the actual key ValidateAPIKey(key string) (*database.APIKey, error) ListAPIKeys(userID uuid.UUID) ([]database.APIKey, error) RevokeAPIKey(userID, keyID uuid.UUID) error DeleteAPIKey(userID, keyID uuid.UUID) error } type service struct { db database.Service jwtSecret []byte tokenDuration time.Duration } type Config struct { JWTSecret string TokenDuration time.Duration } func NewService(db database.Service, config Config) Service { return &service{ db: db, jwtSecret: []byte(config.JWTSecret), tokenDuration: config.TokenDuration, } } func (s *service) Register(email, password string) (*database.User, error) { // Check if user already exists existingUser, err := s.db.GetUserByEmail(email) if err == nil && existingUser != nil { return nil, ErrUserExists } // Hash password hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return nil, err } // Create user user, err := s.db.CreateUser(email, string(hashedPassword)) if err != nil { return nil, err } return user, nil } func (s *service) Login(email, password string) (string, *database.User, error) { // Get user user, err := s.db.GetUserByEmail(email) if err != nil { return "", nil, ErrInvalidCredentials } // Check password if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)); err != nil { return "", nil, ErrInvalidCredentials } // Generate JWT token token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "sub": user.ID.String(), "exp": time.Now().Add(s.tokenDuration).Unix(), }) tokenString, err := token.SignedString(s.jwtSecret) if err != nil { return "", nil, err } return tokenString, user, nil } func (s *service) ValidateToken(tokenString string) (*database.User, error) { // Parse token token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, ErrInvalidToken } return s.jwtSecret, nil }) if err != nil || !token.Valid { return nil, ErrInvalidToken } // Extract claims claims, ok := token.Claims.(jwt.MapClaims) if !ok { return nil, ErrInvalidToken } // Parse user ID userID, err := uuid.Parse(claims["sub"].(string)) if err != nil { return nil, ErrInvalidToken } // Get user from database user, err := s.db.GetUserByID(userID) if err != nil { return nil, err } return user, nil } func (s *service) CreateAPIKey(userID uuid.UUID, name string, expiresAt *time.Time) (*database.APIKey, string, error) { // Generate random API key apiKeyStr, err := generateAPIKey() if err != nil { return nil, "", fmt.Errorf("failed to generate API key: %w", err) } // Hash the API key keyHash := hashAPIKey(apiKeyStr) // Create API key in database apiKey, err := s.db.CreateAPIKey(userID, name, keyHash, expiresAt) if err != nil { return nil, "", fmt.Errorf("failed to create API key: %w", err) } return apiKey, apiKeyStr, nil } func (s *service) ValidateAPIKey(key string) (*database.APIKey, error) { // Validate key format if !validateAPIKeyFormat(key) { return nil, ErrAPIKeyNotFound } keyHash := hashAPIKey(key) apiKey, err := s.db.GetAPIKeyByHash(keyHash) if err != nil { return nil, ErrAPIKeyNotFound } // Check if key is expired if apiKey.ExpiresAt != nil && time.Now().After(*apiKey.ExpiresAt) { return nil, ErrAPIKeyNotFound } // Check if key is active if !apiKey.IsActive { return nil, ErrAPIKeyNotFound } // Update last used timestamp if err := s.db.UpdateAPIKeyLastUsed(apiKey.ID); err != nil { // Log error but don't fail the request // log.Printf("Failed to update API key last used: %v", err) } return apiKey, nil } func (s *service) ListAPIKeys(userID uuid.UUID) ([]database.APIKey, error) { return s.db.ListAPIKeys(userID) } func (s *service) RevokeAPIKey(userID, keyID uuid.UUID) error { return s.db.RevokeAPIKey(userID, keyID) } func (s *service) DeleteAPIKey(userID, keyID uuid.UUID) error { return s.db.DeleteAPIKey(userID, keyID) } |