-
Notifications
You must be signed in to change notification settings - Fork 62
/
examples_test.go
174 lines (141 loc) · 5.02 KB
/
examples_test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package lk_test
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/hyperboloide/lk"
)
type MyLicence struct {
Email string `json:"email"`
End time.Time `json:"end"`
}
// Example_complete creates a new license and validate it.
func Example_complete() {
// create a new Private key:
privateKey, err := lk.NewPrivateKey()
if err != nil {
log.Fatal(err)
}
// create a license document:
doc := MyLicence{
"test@example.com",
time.Now().Add(time.Hour * 24 * 365), // 1 year
}
// marshall the document to json bytes:
docBytes, err := json.Marshal(doc)
if err != nil {
log.Fatal(err)
}
// generate your license with the private key and the document:
license, err := lk.NewLicense(privateKey, docBytes)
if err != nil {
log.Fatal(err)
}
// encode the new license to b64, this is what you give to your customer.
str64, err := license.ToB64String()
if err != nil {
log.Fatal(err)
}
fmt.Println(str64)
// get the public key. The public key should be hardcoded in your app
// to check licences. Do not distribute the private key!
publicKey := privateKey.GetPublicKey()
// validate the license:
if ok, err := license.Verify(publicKey); err != nil {
log.Fatal(err)
} else if !ok {
log.Fatal("Invalid license signature")
}
// unmarshal the document and check the end date:
res := MyLicence{}
if err := json.Unmarshal(license.Data, &res); err != nil {
log.Fatal(err)
} else if res.End.Before(time.Now()) {
log.Fatalf("License expired on: %s", res.End.String())
} else {
fmt.Printf(`Licensed to %s until %s \n`, res.Email, res.End.Format("2006-01-02"))
}
}
// Example_licenseGeneration shows how to create a license file from a private
// key.
func Example_licenseGeneration() {
// a base32 encoded private key generated by `lkgen gen`
// note that you might prefer reading it from a file...
const privateKeyBase32 = "FD7YCAYBAEFXA22DN5XHIYLJNZSXEAP7QIAACAQBANIHKYQBBIAACAKEAH7YIAAAAAFP7AYFAEBP7BQAAAAP7GP7QIAWCBCRKQVWKPT7UJDNP4LB5TXEQMO7EYEGDCE42KVBDNEGRIYIIJFBIWIVB6T6ZTKLSYSGK54DZ5VX6M5SJHBYZU2JXUFXJI25L2JJKJW4RL7UL2XBDT4GKYZ5IS6IWBCN7CWTMVBCBHJMH3RHZ5BVGVAY66MQAEYQEPSS2ANTYZIWXWSGIUJW3MDOO335JK3D4N3IV4L5UTAQMLS5YC7QASCAAUOHTZ5ZCCCYIBNCWBELBMAA===="
// Here we use a struct that is marshalled to json,
// but ultimatly all you need is a []byte.
doc := struct {
Email string `json:"email"`
End time.Time `json:"end"`
}{
"test@example.com",
time.Now().Add(time.Hour * 24 * 365), // 1 year
}
// marshall the document to []bytes (this is the data that our license
// will contain):
docBytes, err := json.Marshal(doc)
if err != nil {
log.Fatal(err)
}
// Unmarshal the private key:
privateKey, err := lk.PrivateKeyFromB32String(privateKeyBase32)
if err != nil {
log.Fatal(err)
}
// generate your license with the private key and the document:
license, err := lk.NewLicense(privateKey, docBytes)
if err != nil {
log.Fatal(err)
}
// the b32 representation of our license, this is what you give to
// your customer.
licenseB32, err := license.ToB32String()
if err != nil {
log.Fatal(err)
}
fmt.Println(licenseB32)
}
// Example_licenseVerification validates a previously generated license with
// a public key.
func Example_licenseVerification() {
// A previously generated licence b32 encoded. In real life you should read
// it from a file at the beginning of your program and check it
// before doing anything else...
const licenseB32 = "FT7YOAYBAEDUY2LDMVXHGZIB76EAAAIDAECEIYLUMEAQUAABAFJAD74EAAAQCUYB76CAAAAABL7YGBIBAL7YMAAAAD73H74IAFEHWITFNVQWS3BCHIRHIZLTORAGK6DBNVYGYZJOMNXW2IRMEJSW4ZBCHIRDEMBRHAWTCMBNGI3FIMJSHIYTSORTGMXDOMBZG43TIMJYHAVTAMR2GAYCE7IBGEBAPXB37ROJCUOYBVG4LAL3MSNKJKPGIKNT564PYK5X542NH62V7TAUEYHGLEOPZHRBAPH7M4SC55OHAEYQEXMKGG3JPO6BSHTDF3T5H6T42VUD7YAJ3TY5AP5MDE5QW4ZYWMSAPEK24HZOUXQ3LJ5YY34XYPVXBUAA===="
// the public key b32 encoded from the private key using:
// `lkgen pub my_private_key_file`. It should be
// hardcoded somewhere in your app.
const publicKeyBase32 = "ARIVIK3FHZ72ERWX6FQ6Z3SIGHPSMCDBRCONFKQRWSDIUMEEESQULEKQ7J7MZVFZMJDFO6B46237GOZETQ4M2NE32C3UUNOV5EUVE3OIV72F5LQRZ6DFMM6UJPELARG7RLJWKQRATUWD5YT46Q2TKQMPPGIA===="
// Unmarshal the public key
publicKey, err := lk.PublicKeyFromB32String(publicKeyBase32)
if err != nil {
log.Fatal(err)
}
// Unmarshal the customer license:
license, err := lk.LicenseFromB32String(licenseB32)
if err != nil {
log.Fatal(err)
}
// validate the license signature:
if ok, err := license.Verify(publicKey); err != nil {
log.Fatal(err)
} else if !ok {
log.Fatal("Invalid license signature")
}
result := struct {
Email string `json:"email"`
End time.Time `json:"end"`
}{}
// unmarshal the document:
if err := json.Unmarshal(license.Data, &result); err != nil {
log.Fatal(err)
}
// Now you just have to check the end date and if it before time.Now(),
// then you can continue!
// if result.End.Before(time.Now()) {
// log.Fatal("License expired on: %s", result.End.Format("2006-01-02"))
// } else {
// fmt.Printf(`Licensed to %s until %s`, result.Email, result.End.Format("2006-01-02"))
// }
}