token_cache.go 2.0 KB

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