-
Notifications
You must be signed in to change notification settings - Fork 5
/
guestissuer-quick.js
63 lines (54 loc) · 2.45 KB
/
guestissuer-quick.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
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
const debug = require('debug')('guest:quick')
const program = require('commander')
program
.description('generates an access token for the specified user of the "Guest Issuer" organization"\n\
note that:\n\
- the "Guest Issuer" identifier can be passed either via an ISSUER env variable, or the -i option\n\
- the secret can be passed either via a SECRET env variable, or the -s option')
.option('-i, --issuer [issuer_id]', 'Guest Issuer identifier')
.option('-s, --secret [secret]', 'secret for the developer organization')
.arguments('<userid> <username>')
.action(function (userid, username) {
// Check issuer id & secret
let issuer = program.issuer || process.env.ISSUER
if (!issuer) {
console.error('missing Guest Issuer identifier, exiting...')
console.error('please specify either via an ISSUER env variable, or the -i option')
process.exit(1)
}
let secret = program.secret || process.env.SECRET
if (!secret) {
console.error('missing organization secret, exiting...')
console.error('please specify either via a SECRET env variable, or the -s option')
process.exit(1)
}
debug('successfully collected Guest Issuer details')
// Check user info
if (typeof userid === 'undefined') {
console.error('no userid specified, exiting...');
process.exit(1);
}
if (typeof username === 'undefined') {
console.error('no full name specified of "Guest" user, exiting...');
process.exit(1);
}
debug('successfully collected guest user info');
// Forge issuer token
const GuestUtil = require('./guestissuer-util');
const expiresInSeconds = Math.round(Date.now()/1000) + 60; // Guest token will expire in 60 seconds
const guestToken = GuestUtil.createGuestToken(issuer, secret, userid, username, expiresInSeconds);
// Request access token
GuestUtil.fetchToken(guestToken)
})
.on('--help', function () {
console.log('')
console.log(' Examples:')
console.log('')
console.log(' $ guestissuer quick 123456789 "John Doe" -i "issuer_id" -s "secret"')
console.log(' $ ISSUER="issuer_id" SECRET="secret" guestissuer quick 123456789 "John Doe"')
})
program.parse(process.argv)