-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
94 lines (76 loc) · 1.9 KB
/
util.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
86
87
88
89
90
91
92
93
94
package license
import (
"compress/gzip"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"strings"
"time"
"go.devnw.com/ooxml/common"
)
// Defaults to the open source license.
var licenseKey = MakeUnlicensedKey()
// SetLicenseKey sets and validates the license key.
func SetLicenseKey(content string, customerName string) error {
lk, err := licenseKeyDecode(content)
if err != nil {
return err
}
if strings.ToLower(lk.CustomerName) != strings.ToLower(customerName) {
return fmt.Errorf("customer name mismatch, expected '%s', but got '%s'", customerName, lk.CustomerName)
}
err = lk.Validate()
if err != nil {
return err
}
licenseKey = &lk
return nil
}
// SetLegacyLicenseKey installs a legacy license code. License codes issued prior to June 2019.
// Will be removed at some point in a future major version.
func SetLegacyLicenseKey(s string) error {
re := regexp.MustCompile("\\s")
s = re.ReplaceAllString(s, "")
var r io.Reader
r = strings.NewReader(s)
r = base64.NewDecoder(base64.RawURLEncoding, r)
r, err := gzip.NewReader(r)
if err != nil {
return err
}
dec := json.NewDecoder(r)
l := &LegacyLicense{}
if err := dec.Decode(l); err != nil {
return err
}
// check signature
if err := l.Verify(legacyPubKey); err != nil {
return errors.New("license validatin error")
}
if l.Expiration.Before(common.ReleasedAt) {
return errors.New("license expired")
}
utcNow := time.Now().UTC()
newLicense := LicenseKey{}
newLicense.CreatedAt = utcNow
newLicense.CustomerId = "Legacy"
newLicense.CustomerName = l.Name
newLicense.Tier = LicenseTierBusiness
newLicense.ExpiresAt = l.Expiration
newLicense.CreatorName = "UniDoc support"
newLicense.CreatorEmail = "support@unidoc.io"
newLicense.UniOffice = true
licenseKey = &newLicense
return nil
}
func GetLicenseKey() *LicenseKey {
if licenseKey == nil {
return nil
}
// Copy.
lk2 := *licenseKey
return &lk2
}