2
0

token_cache.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package integrations
  2. import (
  3. "time"
  4. "gorm.io/gorm"
  5. )
  6. // TokenCache stores a token and an expiration for the token for a
  7. // service account. This will never be shared over REST, so no need
  8. // to externalize.
  9. type TokenCache struct {
  10. gorm.Model
  11. Expiry time.Time `json:"expiry,omitempty"`
  12. // ------------------------------------------------------------------
  13. // All fields below this line are encrypted before storage
  14. // ------------------------------------------------------------------
  15. Token []byte `json:"access_token"`
  16. }
  17. // GetTokenCacheFunc is a function that retrieves the token and expiry
  18. // time from the db
  19. type GetTokenCacheFunc func() (tok *TokenCache, err error)
  20. // SetTokenCacheFunc is a function that updates the token cache
  21. // with a new token and expiry time
  22. type SetTokenCacheFunc func(token string, expiry time.Time) error
  23. // IsExpired returns true if a token is expired, false otherwise
  24. func (t *TokenCache) IsExpired() bool {
  25. return time.Now().After(t.Expiry)
  26. }
  27. // ClusterTokenCache is a token cache that clusters can use; a foreign
  28. // key constraint between a Cluster and ClusterTokenCache is created
  29. type ClusterTokenCache struct {
  30. TokenCache
  31. ClusterID uint `json:"cluster_id"`
  32. }
  33. // RegTokenCache stores a token and an expiration for the JWT token for a
  34. // Docker registry. This will never be shared over REST, so no need
  35. // to externalize.
  36. type RegTokenCache struct {
  37. TokenCache
  38. RegistryID uint `json:"registry_id"`
  39. }
  40. // HelmRepoTokenCache is a token cache that helm repos can use; a foreign
  41. // key constraint between a HelmRepo and HelmRepoTokenCache is created
  42. type HelmRepoTokenCache struct {
  43. TokenCache
  44. HelmRepoID uint `json:"helm_repo_id"`
  45. }