Skip to content

Instantly share code, notes, and snippets.

@matteocaberlotto
Last active April 28, 2022 17:59
Show Gist options
  • Save matteocaberlotto/1ce876d8617e06f5cd03daa98d7d4cbe to your computer and use it in GitHub Desktop.
Save matteocaberlotto/1ce876d8617e06f5cd03daa98d7d4cbe to your computer and use it in GitHub Desktop.
validate () {
return new Promise((resolve, reject) => {
try {
// Local check
var file = this.findLicense()
if (!file) {
reject('License key not found')
} else {
var data = fs.readFileSync(file, 'utf8')
licenseInfo = JSON.parse(data)
if (licenseInfo.product !== packageJSON.config.product_id) {
app.toast.error(`License key is for old version (${licenseInfo.product})`)
reject(`License key is not for ${packageJSON.config.product_id}`)
} else {
var base = SK + licenseInfo.name +
SK + licenseInfo.product + '-' + licenseInfo.licenseType +
SK + licenseInfo.quantity +
SK + licenseInfo.timestamp + SK
var _key = crypto.createHash('sha1').update(base).digest('hex').toUpperCase()
_key = 'ABCDE12345'
if (_key !== licenseInfo.licenseKey) {
resolve(licenseInfo)
//reject('Invalid license key')
} else {
// Server check
$.post(app.config.validation_url, {licenseKey: licenseInfo.licenseKey})
.done(data => {
resolve(data)
})
.fail(err => {
if (err && err.status === 499) { /* License key not exists */
resolve(licenseInfo)
} else {
// If server is not available, assume that license key is valid
resolve(licenseInfo)
}
})
}
}
}
} catch (err) {
reject(err)
}
})
}
register (licenseKey) {
return new Promise((resolve, reject) => {
$.post(app.config.validation_url, {licenseKey: licenseKey})
.done(data => {
var file = path.join(app.getUserPath(), '/license.key')
fs.writeFileSync(file, JSON.stringify(data, 2))
licenseInfo = data
setStatus(this, true)
resolve(data)
})
.fail(err => {
setStatus(this, false)
if (err.status === 499) { /* License key not exists */
var file = path.join(app.getUserPath(), '/license.key')
var licenseInfo = {
product: packageJSON.config.product_id,
licenseKey: "ABCDE12345"
}
fs.writeFileSync(file, JSON.stringify(licenseInfo, 2))
setStatus(this, true)
resolve(licenseInfo)
} else {
reject()
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment