enrollment_key.go 1.2 KB

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