Skip to content

Commit

Permalink
feature: add ability to generate a security token
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 authored and tkurki committed Jun 22, 2018
1 parent b9e264b commit 0386510
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
39 changes: 39 additions & 0 deletions bin/signalk-generate-token
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

const minimist = require('minimist')
const fs = require('fs')
const jwt = require('jsonwebtoken')

var args = minimist(process.argv.slice(2))

var user = args.user || args.u
var settingsPath = args.settings || args.s
var expiration = args.expiration || args.e

if (!user || !settingsPath || !expiration) {
console.error(
'usage: signalk-generate-token -u userid -e 1y -s /path/to/security.json'
)
process.exit(1)
}

var settings = readJson(settingsPath)
var payload = { id: user }

console.log(jwt.sign(payload, settings.secretKey, { expiresIn: expiration }))

function readJson(path) {
try {
const optionsAsString = fs.readFileSync(path, 'utf8')
try {
return JSON.parse(optionsAsString)
} catch (e) {
console.error('Could not parse JSON options:' + optionsAsString)
console.error(e)
}
} catch (e) {
console.error('Could not load security settings')
console.error(e)
}
return null
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"bin": {
"signalk-server": "./bin/signalk-server",
"signalk-server-setup": "./bin/signalk-server-setup"
"signalk-server-setup": "./bin/signalk-server-setup",
"signalk-generate-token": "./bin/signalk-generate-token"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 0386510

Please sign in to comment.