Skip to content

Instantly share code, notes, and snippets.

@kosaikham
Forked from ezekg/example.html
Created January 29, 2018 12:25
Show Gist options
  • Save kosaikham/fc817eeb8ee8f3b9d8cf11aea7f11772 to your computer and use it in GitHub Desktop.
Save kosaikham/fc817eeb8ee8f3b9d8cf11aea7f11772 to your computer and use it in GitHub Desktop.
Example of validating a node-locked license from an Electron app
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Node-locked License Validation Example</title>
</head>
<body>
<button type='button' id='validate-license'>
Validate License
</button>
<script>
const KEYGEN_ACCOUNT = 'YOUR_KEYGEN_ACCOUNT_ID'
// Make sure these npm packages are installed:
const { prompt, confirm } = require('smalltalk')
const { getMac } = require('getmac')
// Valid license key and machine fingerprint when this button is clicked
const button = document.getElementById('validate-license')
button.addEventListener('click', event => {
event.preventDefault()
// Get the current device's MAC address
getMac((err, fingerprint) => {
if (err) {
console.error(err)
return
}
console.log(`Current device fingerprint: ${fingerprint}`)
// Prompt the current user for their license key
prompt('License Required', 'Please enter your license key:', '')
.catch(err => console.error(err))
// Validate the license key within the scope of the machine's fingerprint
.then(key => {
return fetch(`https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT}/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
}
})
})
})
// Parse JSON response body
.then(res => res.json())
// Handle the license validation result
.then(json => {
const { meta, data, errors } = json
if (errors) {
alert('There was an error while validating your license key.')
console.error(errors)
return
}
if (meta.valid) {
alert(`License ${data.id} is valid!`)
return
}
switch (meta.constant) {
case 'NOT_FOUND': {
alert('That license key does not exist.')
break
}
case 'FINGERPRINT_SCOPE_MISMATCH': {
alert(`License ${data.id} is not valid on the current machine: ${fingerprint}.`)
break
}
case 'NO_MACHINES':
case 'NO_MACHINE': {
alert(`License ${data.id} has not been activated yet.`)
break
}
default: {
alert('That license key is not valid.')
}
}
})
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment