2025-10-03 08:38:12 +00:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-10-06 08:28:39 +00:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"backend/pkg/permission/domain/entity"
|
2025-10-03 08:38:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
2025-10-06 08:28:39 +00:00
|
|
|
|
// TokenRepository 定義了與 Redis 相關的 Token 操作方法
|
|
|
|
|
//nolint:interfacebloat
|
2025-10-03 08:38:12 +00:00
|
|
|
|
type TokenRepository interface {
|
2025-10-06 08:28:39 +00:00
|
|
|
|
// Create 建立新的 Token 並存儲至 Redis
|
|
|
|
|
Create(ctx context.Context, token entity.Token) error
|
|
|
|
|
// CreateOneTimeToken 建立臨時(一次性)Token,並指定有效期限
|
|
|
|
|
CreateOneTimeToken(ctx context.Context, key string, ticket entity.Ticket, dt time.Duration) error
|
|
|
|
|
// GetAccessTokenByOneTimeToken 根據一次性 Token 獲取對應的存取 Token
|
|
|
|
|
GetAccessTokenByOneTimeToken(ctx context.Context, oneTimeToken string) (entity.Token, error)
|
|
|
|
|
// GetAccessTokenByID 根據 Token ID 獲取對應的存取 Token
|
|
|
|
|
GetAccessTokenByID(ctx context.Context, id string) (entity.Token, error)
|
|
|
|
|
// GetAccessTokensByUID 根據用戶 ID 獲取該用戶的所有存取 Token
|
|
|
|
|
GetAccessTokensByUID(ctx context.Context, uid string) ([]entity.Token, error)
|
|
|
|
|
// GetAccessTokenCountByUID 根據用戶 ID 獲取該用戶的存取 Token 數量
|
|
|
|
|
GetAccessTokenCountByUID(ctx context.Context, uid string) (int, error)
|
|
|
|
|
// GetAccessTokensByDeviceID 根據裝置 ID 獲取該裝置的所有存取 Token
|
|
|
|
|
GetAccessTokensByDeviceID(ctx context.Context, deviceID string) ([]entity.Token, error)
|
|
|
|
|
// GetAccessTokenCountByDeviceID 根據裝置 ID 獲取該裝置的存取 Token 數量
|
|
|
|
|
GetAccessTokenCountByDeviceID(ctx context.Context, deviceID string) (int, error)
|
|
|
|
|
// Delete 刪除指定的 Token
|
|
|
|
|
Delete(ctx context.Context, token entity.Token) error
|
|
|
|
|
// DeleteOneTimeToken 批量刪除一次性 Token
|
|
|
|
|
DeleteOneTimeToken(ctx context.Context, ids []string, tokens []entity.Token) error
|
|
|
|
|
// DeleteAccessTokenByID 根據 Token ID 批量刪除存取 Token
|
|
|
|
|
DeleteAccessTokenByID(ctx context.Context, ids []string) error
|
|
|
|
|
// DeleteAccessTokensByUID 根據用戶 ID 刪除該用戶的所有存取 Token
|
|
|
|
|
DeleteAccessTokensByUID(ctx context.Context, uid string) error
|
|
|
|
|
// DeleteAccessTokensByDeviceID 根據裝置 ID 刪除該裝置的所有存取 Token
|
|
|
|
|
DeleteAccessTokensByDeviceID(ctx context.Context, deviceID string) error
|
|
|
|
|
|
|
|
|
|
// Blacklist operations
|
|
|
|
|
// AddToBlacklist 將 JWT token 加入黑名單
|
|
|
|
|
AddToBlacklist(ctx context.Context, entry *entity.BlacklistEntry, ttl time.Duration) error
|
|
|
|
|
// IsBlacklisted 檢查 JWT token 是否在黑名單中
|
|
|
|
|
IsBlacklisted(ctx context.Context, jti string) (bool, error)
|
|
|
|
|
// RemoveFromBlacklist 從黑名單中移除 JWT token
|
|
|
|
|
RemoveFromBlacklist(ctx context.Context, jti string) error
|
|
|
|
|
// GetBlacklistedTokensByUID 獲取用戶的所有黑名單 token
|
|
|
|
|
GetBlacklistedTokensByUID(ctx context.Context, uid string) ([]*entity.BlacklistEntry, error)
|
|
|
|
|
}
|