2
0

enrollment_key.go 965 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package models
  2. import (
  3. "time"
  4. )
  5. // EnrollmentKeyLength - the length of an enrollment key
  6. const EnrollmentKeyLength = 32
  7. // EnrollmentKey - the
  8. type EnrollmentKey struct {
  9. Expiration time.Time `json:"expiration"`
  10. UsesRemaining int `json:"uses_remaining"`
  11. Value string `json:"value"`
  12. Networks []string `json:"networks"`
  13. Unlimited bool `json:"unlimited"`
  14. Tags []string `json:"tags"`
  15. }
  16. // EnrollmentKey.IsValid - checks if the key is still valid to use
  17. func (k *EnrollmentKey) IsValid() bool {
  18. if k == nil {
  19. return false
  20. }
  21. if k.UsesRemaining > 0 {
  22. return true
  23. }
  24. if !k.Expiration.IsZero() && time.Now().Before(k.Expiration) {
  25. return true
  26. }
  27. return k.Unlimited
  28. }
  29. // EnrollmentKey.Validate - validate's an EnrollmentKey
  30. // should be used during creation
  31. func (k *EnrollmentKey) Validate() bool {
  32. return k.Networks != nil &&
  33. k.Tags != nil &&
  34. len(k.Value) == EnrollmentKeyLength &&
  35. k.IsValid()
  36. }