auth.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package auth
  2. import (
  3. "context"
  4. "errors"
  5. )
  6. // TokenResponse represents the response containing both access and refresh tokens
  7. type TokenResponse struct {
  8. AccessToken string `json:"access_token"`
  9. RefreshToken string `json:"refresh_token"`
  10. ExpiresIn int64 `json:"expires_in"` // Expiration time in seconds for the access token
  11. TokenType string `json:"token_type"` // Usually "Bearer"
  12. UserID string `json:"user_id"` // User identifier
  13. UserRole string `json:"user_role"` // User role
  14. }
  15. var (
  16. ErrTokenExpired = errors.New("token has expired")
  17. ErrInvalidToken = errors.New("token is invalid")
  18. ErrTokenBlacklisted = errors.New("token has been revoked or blacklisted")
  19. ErrRefreshTokenNotFound = errors.New("refresh token not found or has been invalidated")
  20. ErrUserNotFound = errors.New("user not found")
  21. ErrInvalidCredentials = errors.New("invalid credentials")
  22. )
  23. // Service defines the interface for authentication operations
  24. type Service interface {
  25. // Login authenticates a user with email and password, returning tokens upon success.
  26. Login(ctx context.Context, email string, password string) (*TokenResponse, error)
  27. // GenerateToken creates new access and refresh tokens for a user.
  28. // userID is the unique identifier for the user (e.g., from the database).
  29. // role is the user's role.
  30. GenerateToken(ctx context.Context, userID string, role string) (*TokenResponse, error)
  31. // ValidateToken verifies an access token and returns the userID and role if valid.
  32. ValidateToken(ctx context.Context, tokenString string) (userID string, role string, err error)
  33. // RefreshToken creates a new access token (and potentially a new refresh token)
  34. // based on a valid refresh token.
  35. RefreshToken(ctx context.Context, refreshTokenString string) (*TokenResponse, error)
  36. // Logout invalidates the given token (typically an access token, and its associated refresh token if applicable).
  37. // The exact mechanism (e.g., blacklisting) depends on the implementation.
  38. Logout(ctx context.Context, tokenString string) error
  39. }