Documentation
¶
Index ¶
- Constants
- Variables
- type AccessClaims
- type ChallengeClaims
- type Client
- type EventPublisher
- type LoginRequest
- type LogoutEvent
- type LogoutRequest
- type MemoryStore
- type RedisStore
- type RefreshClaims
- type RefreshRequest
- type Service
- type Session
- func (s *Session) CreateChallenge() (Token, error)
- func (s *Session) CreateTokens() (Token, Token, error)
- func (s *Session) InvalidateRefreshToken(ctx context.Context, refreshToken Token) error
- func (s *Session) RotateTokens(ctx context.Context, refreshToken Token) (Token, Token, error)
- func (s *Session) VerifyAccessToken(ctx context.Context, accessToken Token) error
- func (s *Session) VerifyChallenge(ctx context.Context, challengeToken Token, signature string, address string) error
- type Store
- type Token
- func (t Token) Claims() jwt.Claims
- func (t Token) GetExpiresAt() (time.Time, error)
- func (t Token) GetJTI() (string, error)
- func (t Token) GetNonce() (string, error)
- func (t Token) GetRefreshID() (string, error)
- func (t Token) GetSubject() (string, error)
- func (t *Token) SetRefreshID(refreshID string) error
- func (t Token) String() string
- func (t Token) Type() TokenType
- func (t Token) Validate() error
- type TokenResponse
- type TokenType
- type UserResponse
Constants ¶
const ( // TokenTypeChallenge represents a challenge token TokenTypeChallenge TokenType = "session:challenge" // TokenTypeAccess represents an access token TokenTypeAccess TokenType = "session:access" // TokenTypeRefresh represents a refresh token TokenTypeRefresh TokenType = "session:refresh" // DefaultChallengeExpiry is the default expiration time for challenge tokens DefaultChallengeExpiry = 5 * time.Minute // DefaultAccessExpiry is the default expiration time for access tokens DefaultAccessExpiry = 5 * time.Minute // DefaultRefreshExpiry is the default expiration time for refresh tokens DefaultRefreshExpiry = 120 * time.Hour // 5 days )
const (
// LogoutTopic is the topic for logout events
LogoutTopic = "auth.logout"
)
Variables ¶
var ( // ErrTokenExpired is returned when a token has expired ErrTokenExpired = errors.New("token has expired") // ErrInvalidToken is returned when a token is invalid ErrInvalidToken = errors.New("invalid token") // ErrInvalidSignature is returned when a signature is invalid ErrInvalidSignature = errors.New("invalid signature") // ErrInvalidSigningMethod is returned when the signing method is not ES256 ErrInvalidSigningMethod = errors.New("unexpected signing method") // ErrInvalidAudience is returned when the token audience is not as expected ErrInvalidAudience = errors.New("invalid audience") // ErrInvalidClaims is returned when the token claims are invalid ErrInvalidClaims = errors.New("invalid claims") // ErrTokenRevoked is returned when a token has been revoked ErrTokenRevoked = errors.New("token has been revoked") // ErrInvalidNonce is returned when the nonce is invalid ErrInvalidNonce = errors.New("invalid nonce") // ErrInvalidAddress is returned when the address is invalid ErrInvalidAddress = errors.New("invalid ethereum address") // ErrStoreOperationFailed is returned when a store operation fails ErrStoreOperationFailed = errors.New("store operation failed") // ErrSessionInvalid is returned when a session is invalid ErrSessionInvalid = errors.New("session is invalid") )
Functions ¶
This section is empty.
Types ¶
type AccessClaims ¶
type AccessClaims struct { jwt.RegisteredClaims RefreshID string `json:"rid,omitempty"` }
AccessClaims represents the claims for an access token
type ChallengeClaims ¶
type ChallengeClaims struct { jwt.RegisteredClaims Nonce string `json:"nonce"` }
ChallengeClaims represents the claims for a challenge token
type Client ¶
type Client interface { // Challenge returns a challenge token Challenge() (Token, error) // Login verifies the challenge token and signature, and returns new tokens Login(challenge Token, signature, address string) (access Token, refresh Token, err error) // Refresh rotates the refresh token and returns new tokens Refresh(refresh Token) (access Token, newRefresh Token, err error) // Logout invalidates the provided tokens Logout(refresh Token, access Token) error }
Client represents the public interface for interacting with the session service
type EventPublisher ¶
type EventPublisher interface { // Publish publishes an event to a topic Publish(topic string, data interface{}) error }
EventPublisher represents an interface for publishing events
type LoginRequest ¶
type LoginRequest struct { Challenge string `json:"challenge" binding:"required"` Signature string `json:"signature" binding:"required"` Address string `json:"address" binding:"required"` }
LoginRequest represents a login request
type LogoutEvent ¶
type LogoutEvent struct { UserAddress string `json:"user_address"` TokenID string `json:"token_id"` }
LogoutEvent represents a logout event
type LogoutRequest ¶
type LogoutRequest struct { RefreshToken string `json:"refresh_token" binding:"required"` AccessToken string `json:"access_token" binding:"required"` }
LogoutRequest represents a logout request
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore implements the Store interface using an in-memory map This is primarily intended for testing purposes
func NewMemoryStore ¶
func NewMemoryStore(ctx context.Context) *MemoryStore
NewMemoryStore creates a new MemoryStore
func (*MemoryStore) Clear ¶
func (s *MemoryStore) Clear()
Clear removes all data from the store This is useful for testing to reset the store between tests
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore implements the Store interface using Redis
func NewRedisStore ¶
func NewRedisStore(ctx context.Context, redisURL string) (*RedisStore, error)
NewRedisStore creates a new RedisStore
func (*RedisStore) GetClient ¶
func (s *RedisStore) GetClient() *redis.Client
GetClient returns the Redis client This is used by the main application to share the Redis client with the Watermill publisher
type RefreshClaims ¶
type RefreshClaims struct {
jwt.RegisteredClaims
}
RefreshClaims represents the claims for a refresh token
type RefreshRequest ¶
type RefreshRequest struct {
RefreshToken string `json:"refresh_token" binding:"required"`
}
RefreshRequest represents a refresh request
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service provides HTTP handlers for the session service
func NewService ¶
NewService creates a new service
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a user session
func NewSession ¶
NewSession creates a new session
func (*Session) CreateChallenge ¶
CreateChallenge creates a new challenge token
func (*Session) CreateTokens ¶
CreateTokens creates new access and refresh tokens
func (*Session) InvalidateRefreshToken ¶
InvalidateRefreshToken invalidates a refresh token
func (*Session) RotateTokens ¶
RotateTokens verifies the refresh token and creates new tokens
func (*Session) VerifyAccessToken ¶
VerifyAccessToken verifies an access token
type Store ¶
type Store interface { // Set adds a key with a value and expiration time Set(ctx context.Context, key, value string, ttl time.Duration) error // Get retrieves a value by key Get(ctx context.Context, key string) (string, error) }
Store represents the interface for storing and retrieving refresh token JTIs
type Token ¶
type Token struct {
// contains filtered or unexported fields
}
Token represents a JWT token
func ParseToken ¶
ParseToken parses a JWT string and returns a Token
func (Token) GetExpiresAt ¶
GetExpiresAt returns the expiration time of the token
func (Token) GetRefreshID ¶
GetRefreshID returns the refresh ID from an access token
func (Token) GetSubject ¶
GetSubject returns the subject from a token
func (*Token) SetRefreshID ¶
SetRefreshID sets the refresh ID for an access token
type TokenResponse ¶
type TokenResponse struct { AccessToken string `json:"access_token"` RefreshToken string `json:"refresh_token,omitempty"` }
TokenResponse represents a token response
type UserResponse ¶
type UserResponse struct {
Address string `json:"address"`
}
UserResponse represents a user response