@@ -115,57 +115,6 @@ var defaultLicense = LicensePayload{
115115 // Domain, ValidUntil are free for all
116116}
117117
118- // NewEvaluator produces a new license evaluator from a license key
119- func NewEvaluator (key []byte , domain string ) (res * Evaluator ) {
120- if len (key ) == 0 {
121- // fallback to the default license
122- return & Evaluator {
123- lic : defaultLicense ,
124- }
125- }
126-
127- deckey := make ([]byte , base64 .StdEncoding .DecodedLen (len (key )))
128- n , err := base64 .StdEncoding .Decode (deckey , key )
129- if err != nil {
130- return & Evaluator {invalid : fmt .Sprintf ("cannot decode key: %q" , err )}
131- }
132- deckey = deckey [:n ]
133-
134- var lic licensePayload
135- err = json .Unmarshal (deckey , & lic )
136- if err != nil {
137- return & Evaluator {invalid : fmt .Sprintf ("cannot unmarshal key: %q" , err )}
138- }
139-
140- keyWoSig , err := json .Marshal (lic .LicensePayload )
141- if err != nil {
142- return & Evaluator {invalid : fmt .Sprintf ("cannot remarshal key: %q" , err )}
143- }
144- hashed := sha256 .Sum256 (keyWoSig )
145-
146- for _ , k := range publicKeys {
147- err = rsa .VerifyPKCS1v15 (k , crypto .SHA256 , hashed [:], lic .Signature )
148- if err == nil {
149- break
150- }
151- }
152- if err != nil {
153- return & Evaluator {invalid : fmt .Sprintf ("cannot verify key: %q" , err )}
154- }
155-
156- if ! matchesDomain (lic .Domain , domain ) {
157- return & Evaluator {invalid : "wrong domain" }
158- }
159-
160- if lic .ValidUntil .Before (time .Now ()) {
161- return & Evaluator {invalid : "not valid anymore" }
162- }
163-
164- return & Evaluator {
165- lic : lic .LicensePayload ,
166- }
167- }
168-
169118func matchesDomain (pattern , domain string ) bool {
170119 if pattern == "" {
171120 return true
@@ -184,46 +133,11 @@ func matchesDomain(pattern, domain string) bool {
184133 return false
185134}
186135
187- // Evaluator determines what a license allows for
188- type Evaluator struct {
189- invalid string
190- lic LicensePayload
191- }
192-
193- // Validate returns false if the license isn't valid and a message explaining why that is.
194- func (e * Evaluator ) Validate () (msg string , valid bool ) {
195- if e .invalid == "" {
196- return "" , true
197- }
198-
199- return e .invalid , false
200- }
201-
202- // Enabled determines if a feature is enabled by the license
203- func (e * Evaluator ) Enabled (feature Feature ) bool {
204- if e .invalid != "" {
205- return false
206- }
207-
208- _ , ok := e .lic .Level .allowance ().Features [feature ]
209- return ok
210- }
211-
212- // HasEnoughSeats returns true if the license supports at least the give amount of seats
213- func (e * Evaluator ) HasEnoughSeats (seats int ) bool {
214- if e .invalid != "" {
215- return false
216- }
217-
218- return e .lic .Seats == 0 || seats <= e .lic .Seats
219- }
220-
221- // Inspect returns the license information this evaluator holds.
222- // This function is intended for transparency/debugging purposes only and must
223- // never be used to determine feature eligibility under a license. All code making
224- // those kinds of decisions must be part of the Evaluator.
225- func (e * Evaluator ) Inspect () LicensePayload {
226- return e .lic
136+ type Evaluator interface {
137+ Enabled (feature Feature ) bool
138+ HasEnoughSeats (seats int ) bool
139+ Inspect () LicensePayload
140+ Validate () (msg string , valid bool )
227141}
228142
229143// Sign signs a license so that it can be used with the evaluator
0 commit comments