2
0

token_cache.go 1.7 KB

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