-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add ability to generate a security token
- Loading branch information
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters