security

package
v1.0.77 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("invalid token")
	ErrExpiredToken = errors.New("token expired")
)

Functions

func ExtractAPIKey

func ExtractAPIKey(authHeader string) string

Types

type APIKeyService

type APIKeyService struct {
	// contains filtered or unexported fields
}

func NewAPIKeyService

func NewAPIKeyService(secret string) *APIKeyService

func (*APIKeyService) GenerateAPIKey

func (s *APIKeyService) GenerateAPIKey() (string, error)

func (*APIKeyService) ValidateAPIKey

func (s *APIKeyService) ValidateAPIKey(key string) bool

type APIKeyServiceV2

type APIKeyServiceV2 struct {
	// contains filtered or unexported fields
}

func NewAPIKeyServiceV2

func NewAPIKeyServiceV2(repo storage.APIKeyRepositoryInterface, secret string) *APIKeyServiceV2

func (*APIKeyServiceV2) CreateAPIKey

func (s *APIKeyServiceV2) CreateAPIKey(ctx context.Context, userID uuid.UUID, name string, rateLimitPerMinute, rateLimitPerDay int, expiresAt *time.Time) (string, *storage.APIKey, error)

func (*APIKeyServiceV2) DeleteAPIKey

func (s *APIKeyServiceV2) DeleteAPIKey(ctx context.Context, keyID uuid.UUID) error

func (*APIKeyServiceV2) DeleteAPIKeyPermanently added in v1.0.63

func (s *APIKeyServiceV2) DeleteAPIKeyPermanently(ctx context.Context, keyID uuid.UUID) error

func (*APIKeyServiceV2) ListAPIKeysByUser

func (s *APIKeyServiceV2) ListAPIKeysByUser(ctx context.Context, userID uuid.UUID) ([]*storage.APIKey, error)

func (*APIKeyServiceV2) ValidateAPIKey

func (s *APIKeyServiceV2) ValidateAPIKey(ctx context.Context, key string) (*storage.APIKey, error)

type AuthRateLimiter

type AuthRateLimiter struct {
	// contains filtered or unexported fields
}

func NewAuthRateLimiter

func NewAuthRateLimiter(redis *storage.RedisClient) *AuthRateLimiter

func (*AuthRateLimiter) CheckAuthRateLimit

func (r *AuthRateLimiter) CheckAuthRateLimit(ctx context.Context, identifier string, maxAttempts int) (allowed bool, waitTime int, blocked bool, err error)

identifier can be IP-based ("ip:...") or email-based ("email:..."). Returns: allowed, waitTime (seconds), blocked, error. - waitTime: how long to wait before next attempt (if not allowed) - blocked: whether the account is temporarily blocked

func (*AuthRateLimiter) GetWaitTime

func (r *AuthRateLimiter) GetWaitTime(ctx context.Context, identifier string) (waitTime int, blocked bool, err error)

func (*AuthRateLimiter) RecordFailedAttempt

func (r *AuthRateLimiter) RecordFailedAttempt(ctx context.Context, identifier string) error

func (*AuthRateLimiter) RecordSuccess

func (r *AuthRateLimiter) RecordSuccess(ctx context.Context, identifier string) error

type Claims

type Claims struct {
	UserID string   `json:"user_id"`
	Email  string   `json:"email"`
	Roles  []string `json:"roles"` // Array of roles: ['user'], ['admin'], or ['user', 'admin']
	jwt.RegisteredClaims
}

type JWTService

type JWTService struct {
	// contains filtered or unexported fields
}

func NewJWTService

func NewJWTService(secret string) *JWTService

func (*JWTService) GenerateToken

func (j *JWTService) GenerateToken(userID, email string, roles []string, expirationTime time.Duration) (string, error)

func (*JWTService) ValidateToken

func (j *JWTService) ValidateToken(tokenString string) (*Claims, error)

type ProviderKeyRepositoryInterface

type ProviderKeyRepositoryInterface interface {
	Create(ctx context.Context, key *storage.UserProviderKey) error
	FindByUserAndProvider(ctx context.Context, userID uuid.UUID, provider string) (*storage.UserProviderKey, error)
	ListByUserID(ctx context.Context, userID uuid.UUID) ([]*storage.UserProviderKey, error)
	Update(ctx context.Context, key *storage.UserProviderKey) error
	Delete(ctx context.Context, userID uuid.UUID, provider string) error
	DeleteByID(ctx context.Context, userID uuid.UUID, keyID uuid.UUID) error
}

type ProviderKeyService

type ProviderKeyService struct {
	// contains filtered or unexported fields
}

func NewProviderKeyService

func NewProviderKeyService(repo ProviderKeyRepositoryInterface, encryptionKey string) (*ProviderKeyService, error)

func (*ProviderKeyService) AddProviderKey

func (s *ProviderKeyService) AddProviderKey(ctx context.Context, userID uuid.UUID, provider string, apiKey string) error

func (*ProviderKeyService) DeleteProviderKey

func (s *ProviderKeyService) DeleteProviderKey(ctx context.Context, userID uuid.UUID, provider string) error

DeleteProviderKey removes a provider key (soft delete)

func (*ProviderKeyService) DeleteProviderKeyByID

func (s *ProviderKeyService) DeleteProviderKeyByID(ctx context.Context, userID uuid.UUID, keyID uuid.UUID) error

DeleteProviderKeyByID removes a provider key by ID

func (*ProviderKeyService) GetProviderKey

func (s *ProviderKeyService) GetProviderKey(ctx context.Context, userID uuid.UUID, provider string) (string, error)

func (*ProviderKeyService) ListProviderKeys

func (s *ProviderKeyService) ListProviderKeys(ctx context.Context, userID uuid.UUID) ([]*storage.UserProviderKey, error)

func (*ProviderKeyService) UpdateProviderKey

func (s *ProviderKeyService) UpdateProviderKey(ctx context.Context, userID uuid.UUID, provider string, apiKey string) error

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

func NewRateLimiter

func NewRateLimiter(redis *storage.RedisClient) *RateLimiter

func (*RateLimiter) CheckRateLimit

func (r *RateLimiter) CheckRateLimit(ctx context.Context, key string, limitPerMinute, limitPerDay int) (bool, error)

func (*RateLimiter) GetRemainingRequests

func (r *RateLimiter) GetRemainingRequests(ctx context.Context, key string, limitPerMinute, limitPerDay int) (minuteRemaining, dayRemaining int64, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL