legacy_api_key.go 762 B

1234567891011121314151617181920212223242526272829303132
  1. package service
  2. import (
  3. "encoding/hex"
  4. "strings"
  5. )
  6. // This marker is an internal invalidation handle, never an authentication key.
  7. // Custom API keys disallow ':', so it cannot collide with a created credential.
  8. const legacyAPIKeyInvalidationPrefix = "legacy-sha256:"
  9. func (k *APIKey) AuthCacheInvalidationKey() string {
  10. if k == nil {
  11. return ""
  12. }
  13. if k.Key != "" {
  14. return k.Key
  15. }
  16. if k.KeyHash != "" {
  17. return legacyAPIKeyInvalidationPrefix + k.KeyHash
  18. }
  19. return ""
  20. }
  21. func (s *APIKeyService) authInvalidationCacheKey(key string) string {
  22. if digest, ok := strings.CutPrefix(key, legacyAPIKeyInvalidationPrefix); ok && len(digest) == 64 {
  23. if _, err := hex.DecodeString(digest); err == nil {
  24. return digest
  25. }
  26. }
  27. return s.authCacheKey(key)
  28. }