Documentation
¶
Overview ¶
Package token manages API tokens used for authenticating external clients.
Index ¶
- Variables
- type APIToken
- type APITokenResponse
- type APITokensListResponse
- type CreateAPITokenRequest
- type CreatedAPITokenResponse
- type Handler
- type Repository
- func (r *Repository) AddTopicToAPIToken(ctx context.Context, tokenID, topicID string) error
- func (r *Repository) CreateAPIToken(ctx context.Context, userID, name, tokenHash, description string) (string, error)
- func (r *Repository) CreateAPITokenWithTopics(ctx context.Context, userID, name, tokenHash, description string, ...) (string, error)
- func (r *Repository) DeleteTopicFromAPIToken(ctx context.Context, tokenID, topicID string) error
- func (r *Repository) GetAPITokenByID(ctx context.Context, tokenID, userID string) (*APIToken, error)
- func (r *Repository) GetAPITokenTopicIDs(ctx context.Context, tokenID string) ([]string, error)
- func (r *Repository) GetAPITokenTopicNames(ctx context.Context, tokenID string) ([]string, error)
- func (r *Repository) ListAPITokens(ctx context.Context, userID string) ([]APIToken, error)
- func (r *Repository) RevokeAPIToken(ctx context.Context, userID, tokenID string) error
- func (r *Repository) UpdateAPIToken(ctx context.Context, userID, tokenID, name, description string) error
- func (r *Repository) UpdateAPITokenWithTopics(ctx context.Context, userID, tokenID, name, description string, ...) error
- func (r *Repository) ValidateAPIToken(ctx context.Context, tokenHash string) (string, error)
- func (r *Repository) ValidateAPITokenForTopic(ctx context.Context, tokenHash, topicName string) (userID, topicID string, err error)
- type Service
- func (s *Service) CreateAPIToken(ctx context.Context, userID, name, description string, topicIDs []string) (string, string, error)
- func (s *Service) ListAPITokens(ctx context.Context, userID string) ([]APITokenResponse, error)
- func (s *Service) RevokeAPIToken(ctx context.Context, userID, tokenID string) error
- func (s *Service) UpdateAPIToken(ctx context.Context, userID, tokenID, name, description string, ...) error
- func (s *Service) ValidateAPIToken(ctx context.Context, token string) (string, error)
- func (s *Service) ValidateAPITokenForTopic(ctx context.Context, token, topicName string) (userID, topicID string, err error)
- type TopicValidator
- type UpdateAPITokenRequest
Constants ¶
This section is empty.
Variables ¶
var ErrAtLeastOneTopic = errors.New("at least one topic is required")
ErrAtLeastOneTopic is returned when an API token request contains no topics.
var ErrInvalidTopicSelection = errors.New("invalid topic selection")
ErrInvalidTopicSelection is returned when one or more topics are invalid for the user.
var ErrTokenNotFound = errors.New("api token not found")
ErrTokenNotFound is returned when an API token does not exist or does not belong to the user.
Functions ¶
This section is empty.
Types ¶
type APIToken ¶
type APIToken struct {
ID string `db:"id"`
UserID string `db:"user_id"`
TokenHash string `db:"token_hash"`
Name string `db:"name"`
Description *string `db:"description"`
ExpiresAt *int64 `db:"expires_at"`
RevokedAt *int64 `db:"revoked_at"`
CreatedAt int64 `db:"created_at"`
LastUsedAt *int64 `db:"last_used_at"`
IsActive bool `db:"is_active"`
}
APIToken is the DB struct — db tags only.
type APITokenResponse ¶
type APITokenResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Description *string `json:"description,omitempty"`
LastFour string `json:"last_four"`
TopicIDs []string `json:"topic_ids,omitempty"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
IsActive bool `json:"is_active"`
}
APITokenResponse is the HTTP response struct — json tags only. LastFour shows the last 4 chars of the token hash (never the full hash).
func ToAPITokenResponse ¶
func ToAPITokenResponse(t *APIToken, topicIDs []string) APITokenResponse
ToAPITokenResponse converts an APIToken and its topic IDs to its HTTP response representation.
type APITokensListResponse ¶
type APITokensListResponse struct {
Data []APITokenResponse `json:"data"`
}
APITokensListResponse wraps a collection.
type CreateAPITokenRequest ¶
type CreateAPITokenRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Topics []string `json:"topics"`
}
CreateAPITokenRequest is the request body for POST /tokens.
func (*CreateAPITokenRequest) Validate ¶
func (r *CreateAPITokenRequest) Validate() []error
Validate validates the create API token request fields.
type CreatedAPITokenResponse ¶
CreatedAPITokenResponse is returned on POST /tokens (one-time token reveal).
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles API token HTTP requests.
func NewHandler ¶
NewHandler creates a new token handler.
func (*Handler) CreateAPIToken ¶
func (h *Handler) CreateAPIToken(w http.ResponseWriter, r *http.Request)
CreateAPIToken handles POST /tokens.
func (*Handler) ListAPITokens ¶
func (h *Handler) ListAPITokens(w http.ResponseWriter, r *http.Request)
ListAPITokens handles GET /tokens.
func (*Handler) RevokeAPIToken ¶
func (h *Handler) RevokeAPIToken(w http.ResponseWriter, r *http.Request)
RevokeAPIToken handles DELETE /tokens/{tokenID}.
func (*Handler) UpdateAPIToken ¶
func (h *Handler) UpdateAPIToken(w http.ResponseWriter, r *http.Request)
UpdateAPIToken handles PATCH /tokens/{tokenID}.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository provides data access for the token domain.
func NewRepository ¶
func NewRepository(db *sqlx.DB) *Repository
NewRepository creates a new token repository.
func (*Repository) AddTopicToAPIToken ¶
func (r *Repository) AddTopicToAPIToken(ctx context.Context, tokenID, topicID string) error
AddTopicToAPIToken associates a topic with an API token.
func (*Repository) CreateAPIToken ¶
func (r *Repository) CreateAPIToken(ctx context.Context, userID, name, tokenHash, description string) (string, error)
CreateAPIToken creates a new API token.
func (*Repository) CreateAPITokenWithTopics ¶
func (r *Repository) CreateAPITokenWithTopics(ctx context.Context, userID, name, tokenHash, description string, topicIDs []string) (string, error)
CreateAPITokenWithTopics atomically creates an API token and its topic associations.
func (*Repository) DeleteTopicFromAPIToken ¶
func (r *Repository) DeleteTopicFromAPIToken(ctx context.Context, tokenID, topicID string) error
DeleteTopicFromAPIToken removes a topic association from an API token.
func (*Repository) GetAPITokenByID ¶
func (r *Repository) GetAPITokenByID(ctx context.Context, tokenID, userID string) (*APIToken, error)
GetAPITokenByID retrieves an API token by ID and user.
func (*Repository) GetAPITokenTopicIDs ¶
GetAPITokenTopicIDs retrieves topic IDs associated with an API token.
func (*Repository) GetAPITokenTopicNames ¶
GetAPITokenTopicNames retrieves topic names associated with an API token.
func (*Repository) ListAPITokens ¶
ListAPITokens retrieves all active API tokens for a user.
func (*Repository) RevokeAPIToken ¶
func (r *Repository) RevokeAPIToken(ctx context.Context, userID, tokenID string) error
RevokeAPIToken revokes an API token.
func (*Repository) UpdateAPIToken ¶
func (r *Repository) UpdateAPIToken(ctx context.Context, userID, tokenID, name, description string) error
UpdateAPIToken updates an API token's name and description.
func (*Repository) UpdateAPITokenWithTopics ¶
func (r *Repository) UpdateAPITokenWithTopics(ctx context.Context, userID, tokenID, name, description string, topicIDs []string) error
UpdateAPITokenWithTopics atomically updates an API token and replaces topic associations.
func (*Repository) ValidateAPIToken ¶
ValidateAPIToken validates an API token and returns the associated user ID.
func (*Repository) ValidateAPITokenForTopic ¶
func (r *Repository) ValidateAPITokenForTopic(ctx context.Context, tokenHash, topicName string) (userID, topicID string, err error)
ValidateAPITokenForTopic validates an API token for a specific topic. Returns the user ID and topic ID if the token is valid, active, not expired, and authorized for the topic.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service provides token business logic.
func NewService ¶
func NewService(repo *Repository, topicValidator TopicValidator) *Service
NewService creates a new token service.
func (*Service) CreateAPIToken ¶
func (s *Service) CreateAPIToken(ctx context.Context, userID, name, description string, topicIDs []string) (string, string, error)
CreateAPIToken creates a new API token for the user and associates the given topics.
func (*Service) ListAPITokens ¶
ListAPITokens lists all active API tokens for the user.
func (*Service) RevokeAPIToken ¶
RevokeAPIToken revokes an API token.
func (*Service) UpdateAPIToken ¶
func (s *Service) UpdateAPIToken(ctx context.Context, userID, tokenID, name, description string, topicIDs []string) error
UpdateAPIToken updates an API token's name, description, and topic associations.
func (*Service) ValidateAPIToken ¶
ValidateAPIToken validates an API token and returns the user ID.
func (*Service) ValidateAPITokenForTopic ¶
func (s *Service) ValidateAPITokenForTopic(ctx context.Context, token, topicName string) (userID, topicID string, err error)
ValidateAPITokenForTopic validates an API token and checks authorization for a specific topic. Returns the user ID and topic ID on success.
type TopicValidator ¶
type TopicValidator interface {
ValidateTopicIDs(ctx context.Context, userID string, topicIDs []string) error
}
TopicValidator verifies that topic IDs belong to the given user.
type UpdateAPITokenRequest ¶
type UpdateAPITokenRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Topics []string `json:"topics"`
}
UpdateAPITokenRequest is the request body for PATCH /tokens/{tokenID}.
func (*UpdateAPITokenRequest) Validate ¶
func (r *UpdateAPITokenRequest) Validate() []error
Validate validates the update API token request fields.