token_cache.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  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. Expiry time.Time `json:"expiry,omitempty"`
  19. // ------------------------------------------------------------------
  20. // All fields below this line are encrypted before storage
  21. // ------------------------------------------------------------------
  22. Token []byte `json:"access_token"`
  23. }
  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. }