netmaker/models/enrollment_key.go

60 lines
1.6 KiB
Go
Raw Normal View History

2023-02-15 06:21:51 +08:00
package models
2023-02-16 04:27:26 +08:00
import (
"time"
)
2023-02-15 06:21:51 +08:00
2023-02-16 05:32:16 +08:00
// EnrollmentToken - the tokenized version of an enrollmentkey;
// to be used for host registration
type EnrollmentToken struct {
2023-02-16 23:56:13 +08:00
Server string `json:"server"`
2023-02-16 05:32:16 +08:00
Value string `json:"value"`
}
2023-02-17 03:27:57 +08:00
// EnrollmentKeyLength - the length of an enrollment key - 62^16 unique possibilities
2023-02-15 06:21:51 +08:00
const EnrollmentKeyLength = 32
2023-02-17 03:27:57 +08:00
// EnrollmentKey - the key used to register hosts and join them to specific networks
2023-02-15 06:21:51 +08:00
type EnrollmentKey struct {
Expiration time.Time `json:"expiration"`
UsesRemaining int `json:"uses_remaining"`
Value string `json:"value"`
Networks []string `json:"networks"`
Unlimited bool `json:"unlimited"`
Tags []string `json:"tags"`
2023-02-16 05:32:16 +08:00
Token string `json:"token,omitempty"` // B64 value of EnrollmentToken
2023-02-15 06:21:51 +08:00
}
2023-02-17 03:27:57 +08:00
// APIEnrollmentKey - used to create enrollment keys via API
type APIEnrollmentKey struct {
Expiration int64 `json:"expiration"`
UsesRemaining int `json:"uses_remaining"`
Networks []string `json:"networks"`
Unlimited bool `json:"unlimited"`
Tags []string `json:"tags"`
}
2023-02-15 06:21:51 +08:00
// EnrollmentKey.IsValid - checks if the key is still valid to use
func (k *EnrollmentKey) IsValid() bool {
if k == nil {
return false
}
if k.UsesRemaining > 0 {
return true
}
2023-02-16 04:27:26 +08:00
if !k.Expiration.IsZero() && time.Now().Before(k.Expiration) {
2023-02-15 06:21:51 +08:00
return true
}
return k.Unlimited
}
// EnrollmentKey.Validate - validate's an EnrollmentKey
// should be used during creation
func (k *EnrollmentKey) Validate() bool {
return k.Networks != nil &&
k.Tags != nil &&
len(k.Value) == EnrollmentKeyLength &&
k.IsValid()
}