forked from keygen-sh/keygen-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.go
85 lines (71 loc) · 3.16 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package keygen
type ValidationCode string
const (
ValidationCodeValid ValidationCode = "VALID"
ValidationCodeNotFound ValidationCode = "NOT_FOUND"
ValidationCodeSuspended ValidationCode = "SUSPENDED"
ValidationCodeExpired ValidationCode = "EXPIRED"
ValidationCodeOverdue ValidationCode = "OVERDUE"
ValidationCodeNoMachine ValidationCode = "NO_MACHINE"
ValidationCodeNoMachines ValidationCode = "NO_MACHINES"
ValidationCodeTooManyMachines ValidationCode = "TOO_MANY_MACHINES"
ValidationCodeTooManyCores ValidationCode = "TOO_MANY_CORES"
ValidationCodeTooManyProcesses ValidationCode = "TOO_MANY_PROCESSES"
ValidationCodeFingerprintScopeRequired ValidationCode = "FINGERPRINT_SCOPE_REQUIRED"
ValidationCodeFingerprintScopeMismatch ValidationCode = "FINGERPRINT_SCOPE_MISMATCH"
ValidationCodeFingerprintScopeEmpty ValidationCode = "FINGERPRINT_SCOPE_EMPTY"
ValidationCodeHeartbeatNotStarted ValidationCode = "HEARTBEAT_NOT_STARTED"
ValidationCodeHeartbeatDead ValidationCode = "HEARTBEAT_DEAD"
ValidationCodeProductScopeRequired ValidationCode = "PRODUCT_SCOPE_REQUIRED"
ValidationCodeProductScopeEmpty ValidationCode = "PRODUCT_SCOPE_MISMATCH"
ValidationCodePolicyScopeRequired ValidationCode = "POLICY_SCOPE_REQUIRED"
ValidationCodePolicyScopeMismatch ValidationCode = "POLICY_SCOPE_MISMATCH"
ValidationCodeMachineScopeRequired ValidationCode = "MACHINE_SCOPE_REQUIRED"
ValidationCodeMachineScopeMismatch ValidationCode = "MACHINE_SCOPE_MISMATCH"
ValidationCodeEntitlementsMissing ValidationCode = "ENTITLEMENTS_MISSING"
ValidationCodeEntitlementsEmpty ValidationCode = "ENTITLEMENTS_SCOPE_EMPTY"
)
type validate struct {
fingerprints []string
}
type meta struct {
Scope scope `json:"scope"`
}
type scope struct {
Fingerprints []string `json:"fingerprints"`
Product string `json:"product"`
}
// GetMeta implements jsonapi.MarshalMeta interface.
func (v validate) GetMeta() interface{} {
return meta{Scope: scope{Fingerprints: v.fingerprints, Product: Product}}
}
type validation struct {
License License `json:"-"`
Result ValidationResult `json:"-"`
}
// SetData implements the jsonapi.UnmarshalData interface.
func (v *validation) SetData(to func(target interface{}) error) error {
return to(&v.License)
}
// SetMeta implements jsonapi.UnmarshalMeta interface.
func (v *validation) SetMeta(to func(target interface{}) error) error {
return to(&v.Result)
}
type ValidationResult struct {
Code ValidationCode `json:"code"`
Valid bool `json:"valid"`
}
// Validate performs a license validation using the current Token, scoped to any
// provided fingerprints. It returns a License, and an error if the license is
// invalid, e.g. ErrLicenseNotActivated or ErrLicenseExpired.
func Validate(fingerprints ...string) (*License, error) {
client := NewClient()
license := &License{}
if _, err := client.Get("me", nil, license); err != nil {
return nil, err
}
if err := license.Validate(fingerprints...); err != nil {
return license, err
}
return license, nil
}