-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
184 lines (160 loc) · 5.2 KB
/
index.js
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
175
176
177
178
179
180
181
182
183
184
const { KEYGEN_ACCOUNT_ID } = process.env
if (!KEYGEN_ACCOUNT_ID) {
throw Error('Environment variable KEYGEN_ACCOUNT_ID is required')
}
const { machineId } = require('node-machine-id')
const fetch = require('node-fetch')
const readline = require('readline')
const chalk = require('chalk')
const rl = readline.createInterface(
process.stdin,
process.stdout,
)
const prompt = msg =>
new Promise(resolve => rl.question(chalk.cyan(`${msg}: `), resolve))
async function activateMachine(fingerprint, { key } = {}) {
// Validate the license key before activation, so we can be sure it supports
// another machine. Notice that this validation is scoped to the current
// machine via its fingerprint - this ensures that license activation is
// not performed for machines that are already activated.
const validation = await fetch(`https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT_ID}/licenses/actions/validate-key`, {
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
},
body: JSON.stringify({
meta: {
scope: { fingerprint },
key,
}
})
})
const { meta, errors, data: license } = await validation.json()
if (errors) {
throw new Error(JSON.stringify(errors, null, 2))
}
// If the license is valid, that means the current machine is already
// activated. We can safely return.
if (meta.valid) {
const machine = await retrieveMachine(fingerprint, { key })
return {
activated: false,
machine,
}
}
// If we've gotten this far, our license is not valid for the current
// machine and we should attempt to activate it.
switch (meta.code) {
// This means the license already has at least 1 machine associated with
// it, but none match the current machine's fingerprint. We're breaking
// on this case because, for this example, we want to support activating
// more than 1 machine.
case 'FINGERPRINT_SCOPE_MISMATCH':
// You will receive a NO_MACHINES status when the license IS floating,
// and it does not currently have any associated machines.
case 'NO_MACHINES':
// You will receive a NO_MACHINE status when the license IS NOT floating
// i.e. it's node-locked, and it does not currently have any
// associated machines.
case 'NO_MACHINE': {
break
}
default: {
throw new Error(`license ${meta.detail} (${meta.code})`)
}
}
// Attempt to activate the current machine for the license, using the
// license ID that we received from the validation response and the
// current machine's fingerprint.
const activation = await fetch(`https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT_ID}/machines`, {
method: 'POST',
headers: {
'Authorization': `License ${key}`,
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
},
body: JSON.stringify({
data: {
type: 'machines',
attributes: {
fingerprint
},
relationships: {
license: {
data: { type: 'licenses', id: license.id }
}
}
}
})
})
const { data: machine, errors: errs } = await activation.json()
if (errs) {
throw new Error(JSON.stringify(errs, null, 2))
}
// All is good - the machine was successfully activated.
return {
activated: true,
machine,
}
}
async function deactivateMachine(id, { key } = {}) {
const deactivation = await fetch(`https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT_ID}/machines/${encodeURIComponent(id)}`, {
method: 'DELETE',
headers: {
'Authorization': `License ${key}`,
'Accept': 'application/vnd.api+json',
}
})
if (deactivation.status === 204) {
return
}
const { errors } = await deactivation.json()
if (errors) {
throw new Error(JSON.stringify(errors, null, 2))
}
}
async function retrieveMachine(id, { key } = {}) {
const retrieval = await fetch(`https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT_ID}/machines/${encodeURIComponent(id)}`, {
method: 'GET',
headers: {
'Authorization': `License ${key}`,
'Accept': 'application/vnd.api+json',
}
})
const { data: machine, errors } = await retrieval.json()
if (errors) {
throw new Error(JSON.stringify(errors, null, 2))
}
return machine
}
async function main() {
const key = await prompt('Enter a license key')
const fingerprint = await machineId()
try {
const { machine, activated } = await activateMachine(fingerprint, { key })
if (activated) {
console.log(
chalk.green(`The machine was successfully activated (${machine.id})`)
)
} else {
console.log(
chalk.yellow(`The machine has already been activated (${machine.id})`)
)
const answer = await prompt('Do you want to deactivate this machine? [y/N]')
if (`${answer}`.toLowerCase() === 'y') {
await deactivateMachine(fingerprint, { key })
console.log(
chalk.green(`The machine was successfully deactivated (${machine.id})`)
)
}
}
process.exit(0)
} catch (err) {
console.error(
chalk.red(`An error has occurred:\n${err.message}`)
)
process.exit(1)
}
}
main()