-
Notifications
You must be signed in to change notification settings - Fork 543
/
Copy pathauthorizationServer.js
858 lines (713 loc) · 25.6 KB
/
authorizationServer.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
var express = require("express");
var url = require("url");
var bodyParser = require('body-parser');
var randomstring = require("randomstring");
var cons = require('consolidate');
var nosql = require('nosql').load('database.nosql');
var querystring = require('querystring');
var qs = require("qs");
var __ = require('underscore');
__.string = require('underscore.string');
var base64url = require('base64url');
var jose = require('jsrsasign');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // support form-encoded bodies (for the token endpoint)
app.engine('html', cons.underscore);
app.set('view engine', 'html');
app.set('views', 'files/authorizationServer');
app.set('json spaces', 4);
// authorization server information
var authServer = {
authorizationEndpoint: 'http://localhost:9001/authorize',
tokenEndpoint: 'http://localhost:9001/token'
};
// client information
var clients = [
{
"client_id": "oauth-client-1",
"client_secret": "oauth-client-secret-1",
"redirect_uris": ["http://localhost:9000/callback"],
"scope": "openid profile email phone address"
},
{
"client_id": "oauth-client-2",
"client_secret": "oauth-client-secret-1",
"redirect_uris": ["http://localhost:9000/callback"],
"scope": "bar"
},
{
"client_id": "native-client-1",
"client_secret": "oauth-native-secret-1",
"redirect_uris": ["mynativeapp://"],
"scope": "openid profile email phone address"
}
];
var sharedTokenSecret = "shared token secret!";
var rsaKey = {
"alg": "RS256",
"d": "ZXFizvaQ0RzWRbMExStaS_-yVnjtSQ9YslYQF1kkuIoTwFuiEQ2OywBfuyXhTvVQxIiJqPNnUyZR6kXAhyj__wS_Px1EH8zv7BHVt1N5TjJGlubt1dhAFCZQmgz0D-PfmATdf6KLL4HIijGrE8iYOPYIPF_FL8ddaxx5rsziRRnkRMX_fIHxuSQVCe401hSS3QBZOgwVdWEb1JuODT7KUk7xPpMTw5RYCeUoCYTRQ_KO8_NQMURi3GLvbgQGQgk7fmDcug3MwutmWbpe58GoSCkmExUS0U-KEkHtFiC8L6fN2jXh1whPeRCa9eoIK8nsIY05gnLKxXTn5-aPQzSy6Q",
"e": "AQAB",
"n": "p8eP5gL1H_H9UNzCuQS-vNRVz3NWxZTHYk1tG9VpkfFjWNKG3MFTNZJ1l5g_COMm2_2i_YhQNH8MJ_nQ4exKMXrWJB4tyVZohovUxfw-eLgu1XQ8oYcVYW8ym6Um-BkqwwWL6CXZ70X81YyIMrnsGTyTV6M8gBPun8g2L8KbDbXR1lDfOOWiZ2ss1CRLrmNM-GRp3Gj-ECG7_3Nx9n_s5to2ZtwJ1GS1maGjrSZ9GRAYLrHhndrL_8ie_9DS2T-ML7QNQtNkg2RvLv4f0dpjRYI23djxVtAylYK4oiT_uEMgSkc4dxwKwGuBxSO0g9JOobgfy0--FUHHYtRi0dOFZw",
"kty": "RSA",
"kid": "authserver"
};
var protectedResources = [
{
"resource_id": "protected-resource-1",
"resource_secret": "protected-resource-secret-1"
}
];
var userInfo = {
"alice": {
"sub": "9XE3-JI34-00132A",
"preferred_username": "alice",
"name": "Alice",
"email": "[email protected]",
"email_verified": true
},
"bob": {
"sub": "1ZT5-OE63-57383B",
"preferred_username": "bob",
"name": "Bob",
"email": "[email protected]",
"email_verified": false
},
"carol": {
"sub": "F5Q1-L6LGG-959FS",
"preferred_username": "carol",
"name": "Carol",
"email": "[email protected]",
"email_verified": true,
"username" : "clewis",
"password" : "user password!"
}
};
var codes = {};
var requests = {};
var getClient = function(clientId) {
return __.find(clients, function(client) { return client.client_id == clientId; });
};
var getProtectedResource = function(resourceId) {
return __.find(protectedResources, function(resource) { return resource.resource_id == resourceId; });
};
var getUser = function(username) {
return __.find(userInfo, function (user, key) { return user.username == username; });
};
app.get('/', function(req, res) {
res.render('index', {clients: clients, authServer: authServer});
});
app.get("/authorize", function(req, res){
var client = getClient(req.query.client_id);
if (!client) {
console.log('Unknown client %s', req.query.client_id);
res.render('error', {error: 'Unknown client'});
return;
} else if (!__.contains(client.redirect_uris, req.query.redirect_uri)) {
console.log('Mismatched redirect URI, expected %s got %s', client.redirect_uris, req.query.redirect_uri);
res.render('error', {error: 'Invalid redirect URI'});
return;
} else {
var rscope = req.query.scope ? req.query.scope.split(' ') : undefined;
var cscope = client.scope ? client.scope.split(' ') : undefined;
if (__.difference(rscope, cscope).length > 0) {
// client asked for a scope it couldn't have
var urlParsed = url.parse(req.query.redirect_uri);
delete urlParsed.search; // this is a weird behavior of the URL library
urlParsed.query = urlParsed.query || {};
urlParsed.query.error = 'invalid_scope';
res.redirect(url.format(urlParsed));
return;
}
var reqid = randomstring.generate(8);
var code_challenge = req.query.code_challenge;
requests[reqid] = req.query;
res.render('approve', {client: client, reqid: reqid, scope: rscope, code_challenge: code_challenge});
return;
}
});
app.post('/approve', function(req, res) {
var reqid = req.body.reqid;
var query = requests[reqid];
delete requests[reqid];
if (!query) {
// there was no matching saved request, this is an error
res.render('error', {error: 'No matching authorization request'});
return;
}
if (req.body.approve) {
if (query.response_type == 'code') {
// user approved access
var code = randomstring.generate(8);
var user = req.body.user;
var code_challenge = req.body.code_challenge;
var scope = __.filter(__.keys(req.body), function(s) { return __.string.startsWith(s, 'scope_'); })
.map(function(s) { return s.slice('scope_'.length); });
var client = getClient(query.client_id);
var cscope = client.scope ? client.scope.split(' ') : undefined;
if (__.difference(scope, cscope).length > 0) {
// client asked for a scope it couldn't have
var urlParsed = url.parse(query.redirect_uri);
delete urlParsed.search; // this is a weird behavior of the URL library
urlParsed.query = urlParsed.query || {};
urlParsed.query.error = 'invalid_scope';
res.redirect(url.format(urlParsed));
return;
}
// save the code and request for later
codes[code] = { authorizationEndpointRequest: query, scope: scope, user: user, code_challenge:code_challenge };
var urlParsed =url.parse(query.redirect_uri);
delete urlParsed.search; // this is a weird behavior of the URL library
urlParsed.query = urlParsed.query || {};
urlParsed.query.code = code;
urlParsed.query.state = query.state;
res.redirect(url.format(urlParsed));
return;
} else if (query.response_type == 'token') {
var user = req.body.user;
var scope = __.filter(__.keys(req.body), function(s) { return __.string.startsWith(s, 'scope_'); })
.map(function(s) { return s.slice('scope_'.length); });
var client = getClient(query.client_id);
var cscope = client.scope ? client.scope.split(' ') : undefined;
if (__.difference(scope, cscope).length > 0) {
// client asked for a scope it couldn't have
var urlParsed = url.parse(query.redirect_uri);
delete urlParsed.search; // this is a weird behavior of the URL library
urlParsed.query = urlParsed.query || {};
urlParsed.query.error = 'invalid_scope';
res.redirect(url.format(urlParsed));
return;
}
var user = userInfo[user];
if (!user) {
console.log('Unknown user %s', user)
res.status(500).render('error', {error: 'Unknown user ' + user});
return;
}
console.log("User %j", user);
var token_response = generateTokens(req, res, query.clientId, user, cscope);
var urlParsed = url.parse(query.redirect_uri);
delete urlParsed.search; // this is a weird behavior of the URL library
if (query.state) {
token_response.state = query.state;
}
urlParsed.hash = qs.stringify(token_response);
res.redirect(url.format(urlParsed));
return;
} else {
// we got a response type we don't understand
var urlParsed =url.parse(query.redirect_uri);
delete urlParsed.search; // this is a weird behavior of the URL library
urlParsed.query = urlParsed.query || {};
urlParsed.query.error = 'unsupported_response_type';
res.redirect(url.format(urlParsed));
return;
}
} else {
// user denied access
var urlParsed =url.parse(query.redirect_uri);
delete urlParsed.search; // this is a weird behavior of the URL library
urlParsed.query = urlParsed.query || {};
urlParsed.query.error = 'access_denied';
res.redirect(url.format(urlParsed));
return;
}
});
var generateTokens = function (req, res, clientId, user, scope, nonce, generateRefreshToken) {
var access_token = randomstring.generate();
var refresh_token = null;
if (generateRefreshToken) {
refresh_token = randomstring.generate();
}
/*
var header = { 'typ': 'JWT', 'alg': 'RS256', 'kid': 'authserver'};
var payload = {};
payload.iss = 'http://localhost:9001/';
payload.sub = user;
payload.aud = 'http://localhost:9002/';
payload.iat = Math.floor(Date.now() / 1000);
payload.exp = Math.floor(Date.now() / 1000) + (5 * 60);
payload.jti = randomstring.generate();
console.log(payload);
var stringHeader = JSON.stringify(header);
var stringPayload = JSON.stringify(payload);
//var encodedHeader = base64url.encode(JSON.stringify(header));
//var encodedPayload = base64url.encode(JSON.stringify(payload));
//var access_token = encodedHeader + '.' + encodedPayload + '.';
//var access_token = jose.jws.JWS.sign('HS256', stringHeader, stringPayload, Buffer.from(sharedTokenSecret).toString('hex'));
var privateKey = jose.KEYUTIL.getKey(rsaKey);
var access_token = jose.jws.JWS.sign('RS256', stringHeader, stringPayload, privateKey);
*/
var header = { 'typ': 'JWT', 'alg': 'RS256', 'kid': 'authserver'};
var payload = {};
payload.iss = 'http://localhost:9001/';
payload.sub = user.sub;
payload.aud = clientId;
payload.iat = Math.floor(Date.now() / 1000);
payload.exp = Math.floor(Date.now() / 1000) + (5 * 60);
if (nonce) {
payload.nonce = nonce;
}
var stringHeader = JSON.stringify(header);
var stringPayload = JSON.stringify(payload);
var privateKey = jose.KEYUTIL.getKey(rsaKey);
var id_token = jose.jws.JWS.sign('RS256', stringHeader, stringPayload, privateKey);
nosql.insert({ access_token: access_token, client_id: clientId, scope: scope, user: user });
if (refresh_token) {
nosql.insert({ refresh_token: refresh_token, client_id: clientId, scope: scope, user: user });
}
console.log('Issuing access token %s', access_token);
if (refresh_token) {
console.log('and refresh token %s', refresh_token);
}
console.log('with scope %s', access_token, scope);
console.log('Iussing ID token %s', id_token);
var cscope = null;
if (scope) {
cscope = scope.join(' ')
}
var token_response = { access_token: access_token, token_type: 'Bearer', refresh_token: refresh_token, scope: cscope, id_token: id_token };
return token_response;
};
app.post("/token", function(req, res){
var auth = req.headers['authorization'];
if (auth) {
// check the auth header
var clientCredentials = Buffer.from(auth.slice('basic '.length), 'base64').toString().split(':');
var clientId = querystring.unescape(clientCredentials[0]);
var clientSecret = querystring.unescape(clientCredentials[1]);
}
// otherwise, check the post body
if (req.body.client_id) {
if (clientId) {
// if we've already seen the client's credentials in the authorization header, this is an error
console.log('Client attempted to authenticate with multiple methods');
res.status(401).json({error: 'invalid_client'});
return;
}
var clientId = req.body.client_id;
var clientSecret = req.body.client_secret;
}
var client = getClient(clientId);
if (!client) {
console.log('Unknown client %s', clientId);
res.status(401).json({error: 'invalid_client'});
return;
}
if (client.client_secret != clientSecret) {
console.log('Mismatched client secret, expected %s got %s', client.client_secret, clientSecret);
res.status(401).json({error: 'invalid_client'});
return;
}
if (req.body.grant_type == 'authorization_code') {
var code = codes[req.body.code];
if (code) {
delete codes[req.body.code]; // burn our code, it's been used
if (code.authorizationEndpointRequest.client_id == clientId) {
console.log("code challenge %s", code.code_challenge);
if (code.code_challenge) {
if (code.code_challenge != req.body.code_verifier ) {
res.status(400).json({error: 'iInvalid code verifier'});
return;
}
}
var user = userInfo[code.user];
if (!user) {
console.log('Unknown user %s', user)
res.status(500).render('error', {error: 'Unknown user ' + code.user});
return;
}
console.log("User %j", user);
var token_response = generateTokens(req, res, clientId, user, code.scope, code.authorizationEndpointRequest.nonce, true);
res.status(200).json(token_response);
console.log('Issued tokens for code %s', req.body.code);
return;
} else {
console.log('Client mismatch, expected %s got %s', code.authorizationEndpointRequest.client_id, clientId);
res.status(400).json({error: 'invalid_grant'});
return;
}
} else {
console.log('Unknown code, %s', req.body.code);
res.status(400).json({error: 'invalid_grant'});
return;
}
} else if (req.body.grant_type == 'client_credentials') {
var scope = req.body.scope ? req.body.scope.split(' ') : undefined;
var client = getClient(query.client_id);
var cscope = client.scope ? client.scope.split(' ') : undefined;
if (__.difference(scope, cscope).length > 0) {
// client asked for a scope it couldn't have
res.status(400).json({error: 'invalid_scope'});
return;
}
var access_token = randomstring.generate();
var token_response = { access_token: access_token, token_type: 'Bearer', scope: scope.join(' ') };
nosql.insert({ access_token: access_token, client_id: clientId, scope: scope });
console.log('Issuing access token %s', access_token);
res.status(200).json(token_response);
return;
} else if (req.body.grant_type == 'refresh_token') {
nosql.find().make(function(builder) {
builder.where('refresh_token', req.body.refresh_token);
builder.callback(function(err, tokens) {
if (tokens.length == 1) {
var token = tokens[0];
if (token.client_id != clientId) {
console.log('Invalid client using a refresh token, expected %s got %s', token.client_id, clientId);
nosql.remove().make(function(builder) { builder.where('refresh_token', req.body.refresh_token); });
res.status(400).end();
return
}
console.log("We found a matching token: %s", req.body.refresh_token);
var access_token = randomstring.generate();
var token_response = { access_token: access_token, token_type: 'Bearer', refresh_token: req.body.refresh_token };
nosql.insert({ access_token: access_token, client_id: clientId });
console.log('Issuing access token %s for refresh token %s', access_token, req.body.refresh_token);
res.status(200).json(token_response);
return;
} else {
console.log('No matching token was found.');
res.status(401).end();
}
})
});
} else if (req.body.grant_type == 'password') {
var username = req.body.username;
var user = getUser(username);
if (!user) {
console.log('Unknown user %s', user);
res.status(401).json({error: 'invalid_grant'});
return;
}
console.log("user is %j ", user)
var password = req.body.password;
if (user.password != password) {
console.log('Mismatched resource owner password, expected %s got %s', user.password, password);
res.status(401).json({error: 'invalid_grant'});
return;
}
var scope = req.body.scope;
var token_response = generateTokens(req, res, clientId, user, scope);
res.status(200).json(token_response);
return;
} else {
console.log('Unknown grant type %s', req.body.grant_type);
res.status(400).json({error: 'unsupported_grant_type'});
}
});
app.post('/revoke', function(req, res) {
var auth = req.headers['authorization'];
if (auth) {
// check the auth header
var clientCredentials = Buffer.from(auth.slice('basic '.length), 'base64').toString().split(':');
var clientId = querystring.unescape(clientCredentials[0]);
var clientSecret = querystring.unescape(clientCredentials[1]);
}
// otherwise, check the post body
if (req.body.client_id) {
if (clientId) {
// if we've already seen the client's credentials in the authorization header, this is an error
console.log('Client attempted to authenticate with multiple methods');
res.status(401).json({error: 'invalid_client'});
return;
}
var clientId = req.body.client_id;
var clientSecret = req.body.client_secret;
}
var client = getClient(clientId);
if (!client) {
console.log('Unknown client %s', clientId);
res.status(401).json({error: 'invalid_client'});
return;
}
if (client.client_secret != clientSecret) {
console.log('Mismatched client secret, expected %s got %s', client.client_secret, clientSecret);
res.status(401).json({error: 'invalid_client'});
return;
}
var inToken = req.body.token;
nosql.remove().make(function(builder) {
builder.and();
builder.where('access_token', inToken);
builder.where('client_id', clientId);
builder.callback(function(err, count) {
console.log("Removed %s tokens", count);
res.status(204).end();
return;
});
});
});
app.post('/introspect', function(req, res) {
var auth = req.headers['authorization'];
var resourceCredentials = Buffer.from(auth.slice('basic '.length), 'base64').toString().split(':');
var resourceId = querystring.unescape(resourceCredentials[0]);
var resourceSecret = querystring.unescape(resourceCredentials[1]);
var resource = getProtectedResource(resourceId);
if (!resource) {
console.log('Unknown resource %s', resourceId);
res.status(401).end();
return;
}
if (resource.resource_secret != resourceSecret) {
console.log('Mismatched secret, expected %s got %s', resource.resource_secret, resourceSecret);
res.status(401).end();
return;
}
var inToken = req.body.token;
console.log('Introspecting token %s', inToken);
nosql.one().make(function(builder) {
builder.where('access_token', inToken);
builder.callback(function(err, token) {
if (token) {
console.log("We found a matching token: %s", inToken);
var introspectionResponse = {};
introspectionResponse.active = true;
introspectionResponse.iss = 'http://localhost:9001/';
introspectionResponse.sub = token.user;
introspectionResponse.scope = token.scope.join(' ');
introspectionResponse.client_id = token.client_id;
res.status(200).json(introspectionResponse);
return;
} else {
console.log('No matching token was found.');
var introspectionResponse = {};
introspectionResponse.active = false;
res.status(200).json(introspectionResponse);
return;
};
})
});
});
var checkClientMetadata = function (req, res) {
var reg = {};
if (!req.body.token_endpoint_auth_method) {
reg.token_endpoint_auth_method = 'secret_basic';
} else {
reg.token_endpoint_auth_method = req.body.token_endpoint_auth_method;
}
if (!__.contains(['secret_basic', 'secret_post', 'none'], reg.token_endpoint_auth_method)) {
res.status(400).json({error: 'invalid_client_metadata'});
return;
}
if (!req.body.grant_types) {
if (!req.body.response_types) {
reg.grant_types = ['authorization_code'];
reg.response_types = ['code'];
} else {
reg.response_types = req.body.response_types;
if (__.contains(req.body.response_types, 'code')) {
reg.grant_types = ['authorization_code'];
} else {
reg.grant_types = [];
}
}
} else {
if (!req.body.response_types) {
reg.grant_types = req.body.grant_types;
if (__.contains(req.body.grant_types, 'authorization_code')) {
reg.response_types =['code'];
} else {
reg.response_types = [];
}
} else {
reg.grant_types = req.body.grant_types;
reg.response_types = req.body.response_types;
if (__.contains(req.body.grant_types, 'authorization_code') && !__.contains(req.body.response_types, 'code')) {
reg.response_types.push('code');
}
if (!__.contains(req.body.grant_types, 'authorization_code') && __.contains(req.body.response_types, 'code')) {
reg.grant_types.push('authorization_code');
}
}
}
if (!__.isEmpty(__.without(reg.grant_types, 'authorization_code', 'refresh_token')) ||
!__.isEmpty(__.without(reg.response_types, 'code'))) {
res.status(400).json({error: 'invalid_client_metadata'});
return;
}
if (!req.body.redirect_uris || !__.isArray(req.body.redirect_uris) || __.isEmpty(req.body.redirect_uris)) {
res.status(400).json({error: 'invalid_redirect_uri'});
return;
} else {
reg.redirect_uris = req.body.redirect_uris;
}
if (typeof(req.body.client_name) == 'string') {
reg.client_name = req.body.client_name;
}
if (typeof(req.body.client_uri) == 'string') {
reg.client_uri = req.body.client_uri;
}
if (typeof(req.body.logo_uri) == 'string') {
reg.logo_uri = req.body.logo_uri;
}
if (typeof(req.body.scope) == 'string') {
reg.scope = req.body.scope;
}
return reg;
};
app.post('/register', function (req, res){
var reg = checkClientMetadata(req, res);
if (!reg) {
return;
}
reg.client_id = randomstring.generate();
if (__.contains(['client_secret_basic', 'client_secret_post']), reg.token_endpoint_auth_method) {
reg.client_secret = randomstring.generate();
}
reg.client_id_created_at = Math.floor(Date.now() / 1000);
reg.client_secret_expires_at = 0;
reg.registration_access_token = randomstring.generate();
reg.registration_client_uri = 'http://localhost:9001/register/' + reg.client_id;
clients.push(reg);
res.status(201).json(reg);
return;
});
var validateConfigurationEndpointRequest = function (req, res, next) {
var clientId = req.params.clientId;
var client = getClient(clientId);
if (!client) {
res.status(404).end();
return;
}
var auth = req.headers['authorization'];
if (auth && auth.toLowerCase().indexOf('bearer') == 0) {
var regToken = auth.slice('bearer '.length);
if (regToken == client.registration_access_token) {
req.client = client;
next();
return;
} else {
res.status(403).end();
return;
}
} else {
res.status(401).end();
return;
}
};
app.get('/register/:clientId', validateConfigurationEndpointRequest, function(req, res) {
res.status(200).json(client);
});
app.put('/register/:clientId', validateConfigurationEndpointRequest, function(req, res) {
if (req.body.client_id != client.client_id) {
res.status(400).json({error: 'invalid_client_metadata'});
return;
}
if (req.body.client_secret && req.body.client_secret != client.client_secret) {
res.status(400).json({error: 'invalid_client_metadata'});
}
var reg = checkClientMetadata(req, res);
if (!reg) {
return;
}
__.each(client, function(value, key, list) {
client[key] = reg[key];
});
__.each(reg, function(value, key, list) {
client[key] = reg[key];
});
res.status(200).json(client);
});
app.delete('/register/:clientId', validateConfigurationEndpointRequest, function(req, res) {
clients = __.reject(clients, __.matches({client_id: client.client_id}));
nosql.remove().make(function(builder) {
builder.where('client_id', clientId);
builder.callback(function(err, count) {
console.log("Removed %s tokens", count);
});
});
res.status(204).end();
return;
});
var getAccessToken = function(req, res, next) {
// check the auth header first
var auth = req.headers['authorization'];
var inToken = null;
if (auth && auth.toLowerCase().indexOf('bearer') == 0) {
inToken = auth.slice('bearer '.length);
} else if (req.body && req.body.access_token) {
// not in the header, check in the form body
inToken = req.body.access_token;
} else if (req.query && req.query.access_token) {
inToken = req.query.access_token
}
console.log('Incoming token: %s', inToken);
nosql.one().make(function(builder) {
builder.where('access_token', inToken);
builder.callback(function(err, token) {
if (token) {
console.log("We found a matching token: %s", inToken);
} else {
console.log('No matching token was found.');
};
req.access_token = token;
next();
return;
});
});
};
var requireAccessToken = function(req, res, next) {
if (req.access_token) {
next();
} else {
res.status(401).end();
}
};
var userInfoEndpoint = function(req, res) {
if (!__.contains(req.access_token.scope, 'openid')) {
res.status(403).end();
return;
}
var user = userInfo[req.access_token.user];
if (!user) {
res.status(404).end();
return;
}
var out = {};
__.each(req.access_token.scope, function (scope) {
if (scope == 'openid') {
__.each(['sub'], function(claim) {
if (user[claim]) {
out[claim] = user[claim];
}
});
} else if (scope == 'profile') {
__.each(['name', 'family_name', 'given_name', 'middle_name', 'nickname', 'preferred_username', 'profile', 'picture', 'website', 'gender', 'birthdate', 'zoneinfo', 'locale', 'updated_at'], function(claim) {
if (user[claim]) {
out[claim] = user[claim];
}
});
} else if (scope == 'email') {
__.each(['email', 'email_verified'], function(claim) {
if (user[claim]) {
out[claim] = user[claim];
}
});
} else if (scope == 'address') {
__.each(['address'], function(claim) {
if (user[claim]) {
out[claim] = user[claim];
}
});
} else if (scope == 'phone') {
__.each(['phone_number', 'phone_number_verified'], function(claim) {
if (user[claim]) {
out[claim] = user[claim];
}
});
}
});
res.status(200).json(out);
return;
};
app.get('/userinfo', getAccessToken, requireAccessToken, userInfoEndpoint);
app.post('/userinfo', getAccessToken, requireAccessToken, userInfoEndpoint);
app.use('/', express.static('files/authorizationServer'));
// clear the database
nosql.clear();
var server = app.listen(9001, 'localhost', function () {
var host = server.address().address;
var port = server.address().port;
console.log('OAuth Authorization Server is listening at http://%s:%s', host, port);
});