This repository has been archived by the owner on Apr 16, 2019. It is now read-only.
/
ticket.js
executable file
·325 lines (241 loc) · 8.41 KB
/
ticket.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Load modules
var Boom = require('boom');
var Iron = require('iron');
var Cryptiles = require('cryptiles');
var Hawk = require('hawk');
var Hoek = require('hoek');
var Scope = require('./scope');
// Declare internals
var internals = {};
internals.defaults = {
ticketTTL: 60 * 60 * 1000, // 1 hour
rsvpTTL: 1 * 60 * 1000, // 1 minute
keyBytes: 32, // Ticket secret size in bytes
hmacAlgorithm: 'sha256'
};
/*
var app = {
id: '123', // Application id
scope: ['a', 'b'], // Grant scope
*secret: 'secret' // Used in endpoints for Basic auth authentication
};
var grant = {
id: 'd832d9283hd9823dh', // Persitant identifier used to issue additional tickets or revoke access
user: '456', // User id
exp: 1352535473414, // Grant expiration
scope: ['b'] // Grant scope
};
var options = {
ttl: 60 * 1000, // 1 min
scope: ['b'], // Ticket scope
ext: { // Server-specific extension data
tos: '0.0.1',
private: { x: 1 } // Anything inside 'private' is only included in the encrypted portion
},
iron: {} // Override Iron defaults
keyBytes: 32, // Hawk key length
hmacAlgorithm: 'sha256' // Hawk algorithm
};
*/
exports.issue = function (app, grant, encryptionPassword, options, callback) {
if (!app || !app.id) {
return callback(Boom.internal('Invalid application object'));
}
if (grant && (!grant.id || !grant.user || !grant.exp)) {
return callback(Boom.internal('Invalid grant object'));
}
if (!encryptionPassword) {
return callback(Boom.internal('Invalid encryption password'));
}
if (!options) {
return callback(Boom.internal('Invalid options object'));
}
var scope = options.scope || (grant ? grant.scope : null) || app.scope || [];
var error = Scope.validate(scope);
if (error) {
return callback(error);
}
// Construct ticket
var exp = (Hawk.utils.now() + (options.ttl || internals.defaults.ticketTTL));
if (grant) {
exp = Math.min(exp, grant.exp);
}
var ticket = {
exp: exp,
app: app.id,
scope: scope
};
if (options.ext) {
ticket.ext = options.ext;
}
if (grant) {
ticket.grant = grant.id;
ticket.user = grant.user;
}
exports.generate(ticket, encryptionPassword, options, callback);
};
// Reissue ticket
/*
var options = {
ttl: 60 * 1000, // 1 min
scope: ['b'], // Ticket scope (must be equal or lesser than original)
grantExp: 1352535473414, // Grant expiration timestamp
issueTo: '123', // Delegated to application id
ext: { // Server-specific extension data
tos: '0.0.1',
private: { x: 1 } // Anything inside 'private' is only included in the encrypted portion
},
iron: {} // Override Iron defaults
keyBytes: 32, // Hawk key length
hmacAlgorithm: 'sha256' // Hawk algorithm
};
*/
exports.reissue = function (parentTicket, encryptionPassword, options, callback) {
if (!parentTicket) {
return callback(Boom.internal('Invalid parent ticket object'));
}
if (!encryptionPassword) {
return callback(Boom.internal('Invalid encryption password'));
}
if (!options) {
return callback(Boom.internal('Invalid options object'));
}
if (options.scope && !Scope.isSubset(parentTicket.scope, options.scope)) {
return callback(Boom.forbidden('New scope is not a subset of the parent ticket scope'));
}
if (options.issueTo && parentTicket.dlg) {
return callback(Boom.badRequest('Cannot re-delegate'));
}
// Construct ticket
var exp = (Hawk.utils.now() + (options.ttl || internals.defaults.ticketTTL));
if (options.grantExp) {
exp = Math.min(exp, options.grantExp);
}
var ticket = {
exp: exp,
app: options.issueTo || parentTicket.app,
scope: options.scope || parentTicket.scope
};
if (options.ext || parentTicket.ext) {
ticket.ext = options.ext || parentTicket.ext;
}
if (parentTicket.grant) {
ticket.grant = parentTicket.grant;
ticket.user = parentTicket.user;
}
if (options.issueTo) {
ticket.dlg = parentTicket.app;
}
else if (parentTicket.dlg) {
ticket.dlg = parentTicket.dlg;
}
exports.generate(ticket, encryptionPassword, options, callback);
};
/*
// The requesting application
var app = {
id: '123', // Application id
};
// The resource owner
var grant = {
id: 'd832d9283hd9823dh' // Persitant identifier used to issue additional tickets or revoke access
};
var options = {
ttl: 1 * 60 * 10000, // Rsvp TTL
iron: {} // Override Iron defaults
};
*/
exports.rsvp = function (app, grant, encryptionPassword, options, callback) {
if (!app || !app.id) {
return callback(Boom.internal('Invalid application object'));
}
if (!grant || !grant.id) {
return callback(Boom.internal('Invalid grant object'));
}
if (!encryptionPassword) {
return callback(Boom.internal('Invalid encryption password'));
}
if (!options) {
return callback(Boom.internal('Invalid options object'));
}
options.ttl = options.ttl || internals.defaults.rsvpTTL;
// Construct envelope
var envelope = {
app: app.id,
exp: Hawk.utils.now() + options.ttl,
grant: grant.id
};
// Stringify and encrypt
Iron.seal(envelope, encryptionPassword, options.iron || Iron.defaults, function (err, sealed) {
if (err) {
return callback(err);
}
var rsvp = sealed;
return callback(null, rsvp);
});
};
/*
var ticket = {
// Inputs into generate()
exp: time in msec
app: app id ticked is issued to
scope: ticket scope
ext: application data (child key 'private' has special meaning)
grant: grand id
user: user id
dlg: app id of the delegating party
// Added by generate()
key: ticket secret key (Hawk)
algorithm: ticket hmac algorithm (Hawk)
id: ticket key id (Hawk)
};
var options = {
iron: {}, // Override Iron defaults
keyBytes: 32, // Hawk key length
hmacAlgorithm: 'sha256' // Hawk algorithm
};
*/
exports.generate = function (ticket, encryptionPassword, options, callback) {
// Generate ticket secret
var random = Cryptiles.randomString(options.keyBytes || internals.defaults.keyBytes);
if (random instanceof Error) {
return callback(random);
}
ticket.key = random;
ticket.algorithm = options.hmacAlgorithm || internals.defaults.hmacAlgorithm;
// Seal ticket
Iron.seal(ticket, encryptionPassword, options.iron || Iron.defaults, function (err, sealed) {
if (err) {
return callback(err);
}
ticket.id = sealed;
// Hide private ext data
if (ticket.ext &&
ticket.ext.private) {
delete ticket.ext.private;
}
return callback(null, ticket);
});
};
// Parse ticket id
/*
var options = {
iron: {} // Override Iron defaults
};
*/
exports.parse = function (id, encryptionPassword, options, callback) {
if (!encryptionPassword) {
return callback(Boom.internal('Invalid encryption password'));
}
if (!options) {
return callback(Boom.internal('Invalid options object'));
}
Iron.unseal(id, encryptionPassword, options.iron || Iron.defaults, function (err, object) {
if (err) {
return callback(err);
}
var ticket = object;
ticket.id = id;
return callback(null, ticket);
});
};