-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathciservices.js
1430 lines (1295 loc) · 45.3 KB
/
ciservices.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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ciservices - performs user, group and FIDO2 operations using IBM Cloud Identity APIs
//
const KJUR = require('jsrsasign');
const requestp = require('request-promise-native');
const logger = require('./logging.js');
const tm = require('./oauthtokenmanager.js');
const fido2error = require('./fido2error.js');
const cryptoutils = require('./cryptoutils.js');
const userutils = require('./userutils.js');
//
// Caching logic to reduce number of calls to CI
//
// cache to map rpUuid to rpId
var rpUuidMap = {};
// cache to map rpId to rpUuid
var rpIdMap = {};
//
// All users of this app will end up in this group
// This is useful for when the Cloud Identity tenant
// is used for multiple purposes and we want to see/filter
// users in the context of this application only.
// The applicationGroupSCIMId will be dynamically populated
// first time the group is created/accessed after startup.
//
var applicationGroupName = "FIDOPhotoUsers";
var applicationGroupSCIMId = null;
function getUserDetails(req) {
return userutils.getUserDetails(req);
}
function handleErrorResponse(methodName, rsp, e, genericError) {
// log what we can about this error case
logger.logWithTS("ciservices." + methodName + " e: " +
e + " stringify(e): " + (e != null ? JSON.stringify(e): "null"));
let fidoError = null;
// if e is already a fido2Error, return it, otherwise try to perform discovery of
// the error message, otherwise return a generic error message
if (e != null && e.status == "failed") {
// seems to already be a fido2Error
fidoError = e;
} else if (e != null && e.error != null && e.error.messageId != null && e.error.messageDescription != null) {
// this looks like one of the typical CI error messages
fidoError = new fido2error.fido2Error(e.error.messageId + ": " + e.error.messageDescription);
} else {
// fallback to the generic error
fidoError = new fido2error.fido2Error(genericError);
}
logger.logWithTS("handleErrorResponse sending error response: " + JSON.stringify(fidoError));
rsp.json(fidoError);
}
/**
* Ensure the request contains a "username" attribute, and make sure it's either the
* empty string (if allowed), or is the username of the currently authenticated user.
*/
function validateSelf(fidoRequest, username, allowEmptyUsername) {
if (username != null) {
if (!((fidoRequest.username == username) || (allowEmptyUsername && fidoRequest.username == ""))) {
throw new fido2error.fido2Error("Invalid username in request");
}
} else {
// no currently authenticated user
// only permitted if fidoRequest.username is the empty string and allowEmptyUsername
if (!(fidoRequest.username == "" && allowEmptyUsername)) {
throw new fido2error.fido2Error("Not authenticated");
}
}
return fidoRequest;
}
//
// Takes a CI attestation result payload and converts it to a format
// expected by the client. This comes about because the client was originally
// written to use ISAM-as-a-service. The attributes are actually not used in
// our example app.
//
function coerceAttestationResultToClientFormat(req, attestationResult) {
let result = {
"attributes": {
"responseData": {},
"credentialData": {}
}
};
return result;
}
//
// Takes a CI assertion result payload and converts it to a format
// expected by the client. This comes about because the client was
// originally written to use ISAM-as-a-service. In any case for the
// FIDOPhoto app, assertions are only used by POSTMAN during
// credential testing.
//
function coerceAssertionResultToClientFormat(req, reqBody, assertionResult) {
let result = {
"user": {
"id": assertionResult.userId,
"name": null
},
"attributes": {
"responseData": {},
"credentialData": {
"fidoLoginDetails": JSON.stringify({
"request": reqBody,
"response": assertionResult
}),
"AUTHENTICATOR_FRIENDLY_NAME": assertionResult.attributes.nickname,
"AUTHENTICATION_LEVEL": "2",
"displayName": null,
"email": null
}
}
};
if (assertionResult.attributes["icon"]) {
result.attributes.credentialData["AUTHENTICATOR_ICON"] = assertionResult.attributes["icon"];
}
return tm.getAccessToken(req)
.then((access_token) => {
// look up username from user.id
return getUserAttributes(req, null, result.user.id);
}).then((ua) => {
// fill in user details
result.user.name = ua.username;
result.attributes.credentialData.displayName = ua.displayName;
result.attributes.credentialData.email = ua.username;
// all done
return result;
});
}
/**
* Proxies what is expected to be a valid FIDO2 server request to one of:
* /attestation/options
* /attestation/result
* /assertion/options
* /assertion/result
*
* to the CI server. There is little validation done other than to ensure
* that the client is not sending a request for a user other than the user
* who is currently logged in. CI will perform other payload validation.
*/
function proxyFIDO2ServerRequest(req, rsp, validateUsername, allowEmptyUsername) {
let userDetails = getUserDetails(req);
let bodyToSend = validateUsername ? validateSelf(req.body, userDetails.username, allowEmptyUsername) : req.body;
// the CI body is slightly different from the FIDO server spec.
// instead of username (validity of which has already been checked above),
// we need to provide userId which is the CI IUI for the user.
if (bodyToSend.username != null) {
delete bodyToSend.username;
if (userDetails.userSCIMId) {
bodyToSend.userId = userDetails.userSCIMId;
}
}
// when performing registrations, we want the registration
// enabled immediately so insert this additional option
if (req.url.endsWith("/attestation/result")) {
bodyToSend.enabled = true;
}
let access_token = null;
tm.getAccessToken(req)
.then( (at) => {
access_token = at;
return rpIdTorpUuid(req, process.env.RPID);
}).then((rpUuid) => {
let options = {
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/factors/fido2/relyingparties/" + rpUuid + req.url,
method: "POST",
headers: {
"Content-type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true,
body: bodyToSend
};
logger.logWithTS("proxyFIDO2ServerRequest.options: " + JSON.stringify(options));
return requestp(options);
}).then((proxyResponse) => {
// worked
let rspBody = proxyResponse;
// coerce CI responses to format client expects (comes from original ISAM-based implementation)
if (req.url.endsWith("/attestation/result")) {
return coerceAttestationResultToClientFormat(req, proxyResponse);
} else if (req.url.endsWith("/assertion/result")) {
return coerceAssertionResultToClientFormat(req, bodyToSend, proxyResponse);
} else {
return rspBody;
}
}).then((rspBody) => {
// just add server spec status and error message fields and send it
rspBody.status = "ok";
rspBody.errorMessage = "";
logger.logWithTS("proxyFIDO2ServerRequest.success: " + JSON.stringify(rspBody));
rsp.json(rspBody);
}).catch((e) => {
handleErrorResponse("proxyFIDO2ServerRequest", rsp, e, "Unable to proxy FIDO2 request");
});
}
/*
* Performs just-in-time provisioning of a relying party in cloud identity. Means less manual
* provisioning of the Cloud Identity tenant.
*/
function jitpRelyingParty(req, rpId) {
logger.logWithTS("Just-in-time provisioning the RP with rpId: " + rpId);
return tm.getAccessToken(req)
.then((access_token) => {
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/config/v2.0/factors/fido2/relyingparties",
method: "POST",
headers: {
"Content-type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true,
body: {
"rpId": rpId,
"origins": [
"https://" + rpId,
"https://" + rpId + ":" + process.env.LOCAL_SSL_PORT
],
"name": "Photo Verifier Relying Party: " + rpId,
"allowedAttestationFormats": [
"PACKED",
"TPM",
"FIDO_U2F",
"ANDROID_SAFETYNET",
"ANDROID_KEY",
"NONE"
],
"allowedAttestationTypes": [
"BASIC",
"ATTCA",
"SELF",
"NONE"
],
"metadataConfig": {
"enforcement": false,
"includeAll": true
},
"enabled": true,
// allow UP=false
"webAuthn": false
}
});
}).then(() => {
logger.logWithTS("RP provisioning complete");
return updateRPMaps();
});
}
/**
* Lookup RP's rpUuid from an rpId
*/
function rpIdTorpUuid(req, rpId) {
if (rpIdMap[rpId] != null) {
return rpIdMap[rpId];
} else {
return updateRPMaps()
.then(() => {
// provision RP if we still don't have it
if (rpIdMap[rpId] == null) {
return jitpRelyingParty(req, rpId);
}
}).then(() => {
if (rpIdMap[rpId] != null) {
return rpIdMap[rpId];
} else {
// hmm - no rpId, fatal at this point.
throw new fido2error.fido2Error("rpId: " + rpId + " could not be resolved");
}
});
}
}
/**
* First checks that the registration identified by the provided id is owned by the currently
* logged in user, then Uses a DELETE operation to delete it.
* Returns the remaining registered credentials in the same format as sendUserResponse.
*/
function deleteRegistration(req, rsp) {
let userDetails = getUserDetails(req);
if (userDetails.isAuthenticated()) {
let regId = req.body.id;
if (regId != null) {
let access_token = null;
tm.getAccessToken(req).then((at) => {
access_token = at;
// first search for the suggested registration
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/factors/fido2/registrations/" + regId,
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true
});
}).then((regToDelete) => {
// is it owned by the currenty authenticated user?
if (regToDelete.userId == userDetails.userSCIMId) {
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/factors/fido2/registrations/" + regId,
method: "DELETE",
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true
}).then(() => {
logger.logWithTS("Registration deleted: " + regId);
});
} else {
throw new fido2error.fido2Error("Not owner of registration");
}
}).then((deleteResult) => {
// we care not about the deleteResult - just build and send the user response
sendUserResponse(req, rsp);
}).catch((e) => {
handleErrorResponse("deleteRegistration", rsp, e, "Unable to delete registration");
});
} else {
rsp.json(new fido2error.fido2Error("Invalid id in request"));
}
} else {
rsp.json(new fido2error.fido2Error("Not logged in"));
}
}
/**
* Returns the details of the indicated registration, provided it is owned by the currently
* logged in user.
*/
function registrationDetails(req, rsp) {
let userDetails = getUserDetails(req);
if (userDetails.isAuthenticated()) {
let regId = req.query.id;
if (regId != null) {
let access_token = null;
tm.getAccessToken(req).then((at) => {
access_token = at;
// first retrieve the suggested registration
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/factors/fido2/registrations/" + regId,
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true
});
}).then((reg) => {
// check it is owned by the currenty authenticated user
if (reg.userId == userDetails.userSCIMId) {
rsp.json(reg);
} else {
throw new fido2error.fido2Error("Not owner of registration");
}
}).catch((e) => {
handleErrorResponse("registrationDetails", rsp, e, "Unable to retrieve registration");
});
} else {
rsp.json(new fido2error.fido2Error("Invalid id in request"));
}
} else {
rsp.json(new fido2error.fido2Error("Not logged in"));
}
}
//
// Given a user record from CI, extract a display name for a user. We prefer
// the "formatted" name if it exists, otherwise fallback to userName which
// should always be present.
//
function getDisplayNameFromSCIMResponse(scimResponse) {
let result = scimResponse.userName;
if (scimResponse.name != null && scimResponse.name.formatted != null) {
result = scimResponse.name.formatted;
}
return result;
}
//
// Populates local caches of rpId -> rpUuid, and rpUuid -> rpId. This just helps
// with application performance.
//
function updateRPMaps() {
// reads all relying parties from discovery service and updates local caches
return tm.getAccessToken(null)
.then((access_token) => {
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/factors/discover/fido2",
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true
});
}).then((discoverResponse) => {
rpUuidMap = [];
rpIdMap = [];
discoverResponse.relyingParties.forEach((rp) => {
rpUuidMap[rp.id] = rp.rpId;
rpIdMap[rp.rpId] = rp.id;
});
}).catch((e) => {
logger.logWithTS("ciservices.updateRPMaps e: " + e + " stringify(e): " + (e != null ? JSON.stringify(e): "null"));
});
}
//
// Adds the plain text rpId to each registration. In CI a registration references
// rpId using a separate intermediate identifier.
//
function updateRegistrationsFromMaps(registrationsResponse) {
registrationsResponse.fido2.forEach((reg) => {
// there really shouldn't be any "UNKNOWN" rpIds because if an RP is deleted,
// all related registrations should be deleted at the same time
reg.rpId = (rpUuidMap[reg.references.rpUuid] ? rpUuidMap[reg.references.rpUuid] : "UNKNOWN");
});
return registrationsResponse;
}
//
// Adds rpId to each registration, including lookup of the rpId's if
// needed.
//
function coerceCIRegistrationsToClientFormat(registrationsResponse) {
return new Promise((resolve, reject) => {
// Do this check so we only lookup each unknown rpUuid all at once
let anyUnresolvedRpUuids = false;
for (let i = 0; i < registrationsResponse.fido2.length && !anyUnresolvedRpUuids; i++) {
if (rpUuidMap[registrationsResponse.fido2[i].references.rpUuid] == null) {
anyUnresolvedRpUuids = true;
}
}
// if we need to, refresh the rpUuidMap
if (anyUnresolvedRpUuids) {
updateRPMaps()
.then(() => {
resolve(updateRegistrationsFromMaps(registrationsResponse));
});
} else {
resolve(updateRegistrationsFromMaps(registrationsResponse));
}
});
}
//
// Returns information about the user and all of their FIDO2 registrations for the
// application's rpId. Note that there is an assumption here that all of a user's
// records can be returned in a single response.
//
// TODO - consider future enhancement to deal with pagination of FIDO2 registrations.
//
function getUserResponse(req) {
let userDetails = getUserDetails(req);
let username = userDetails.username;
let userId = userDetails.userSCIMId;
let displayName = userDetails.userDisplayName;
let userAccessToken = userDetails.userAccessToken;
let result = { "authenticated": true, "username": username, "displayName": displayName, "access_token": userAccessToken, "credentials": [], "admin": false};
let search = 'userId="' + userId + '"';
// to futher filter results for just my rpId, add this
search += '&attributes/rpId="'+process.env.RPID+'"';
return tm.getAccessToken(req)
.then((access_token) => {
let options = {
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/factors/fido2/registrations",
method: "GET",
qs: { "search" : search },
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true
};
// This includes an example of how to measure the response time for a call to CI
let start = (new Date()).getTime();
return requestp(options).then((r) => {
let now = (new Date()).getTime();
//console.log("getUserResponse: call to get user registrations with options: " + JSON.stringify(options) + " took(msec): " + (now-start));
return r;
});
}).then((registrationsResponse) => {
return coerceCIRegistrationsToClientFormat(registrationsResponse);
}).then((registrationsResponse) => {
result.credentials = registrationsResponse.fido2;
return result;
}).then((userResponse) => {
if (userDetails.isAdmin()) {
result.admin = true;
}
return result;
});
}
//
// Determines if the user is logged in.
// If so, returns their username and list of currently registered FIDO2 credentials as determined from a CI API.
// If not returns {"authenticated":false}
//
function sendUserResponse(req, rsp) {
let userDetails = getUserDetails(req);
if (userDetails.isAuthenticated()) {
getUserResponse(req)
.then((userResponse) => {
rsp.json(userResponse);
}).catch((e) => {
handleErrorResponse("sendUserResponse", rsp, e, "Unable to get user registrations");
});
} else {
rsp.json({"authenticated": false});
}
}
//
// Serves a simple metadata JSON payload with username, rpId and FIDO endpoint details.
// Typically this is used by the mobile app during registration and the user is authenticated
// via their access token.
//
function sendMetadata(req, rsp) {
let userDetails = getUserDetails(req);
let baseURL = 'https://' + process.env.RPID + ((process.env.LOCAL_SSL_SERVER == "true") ? (':' + process.env.LOCAL_SSL_PORT) : '');
if (userDetails.isAuthenticated()) {
rsp.json({
"username": userDetails.username,
"displayName": userDetails.userDisplayName,
"rpId": process.env.RPID,
"attestation_options": baseURL + '/attestation/options',
"attestation_result": baseURL + '/attestation/result',
"assertion_options": baseURL + '/assertion/options',
"assertion_result": baseURL + '/assertion/result'
});
} else {
rsp.json({"error": "Not authenticated"});
}
}
//
// Used in the app to allow the user to generate a new user access token.
//
function newAccessToken(req, rsp) {
let userDetails = getUserDetails(req);
if (userDetails.isAuthenticated()) {
// force generate a new access token
getUserAccessToken(req, true)
.then((uat) => {
// update session if there is one
if (req.session != null && uat != null) {
req.session.userAccessToken = uat;
}
// return the user response
sendUserResponse(req, rsp);
});
} else {
// return the user response - will handle unauthenticated state
sendUserResponse(req, rsp);
}
}
//
// Stores the user access token in a custom SCIM attribute in the CI user record.
// Handles JIT-P of the SCIM attribute if it's not already part of user schema.
//
function storeUserAccessToken(req, userSCIMRecord, uat) {
let userDetails = getUserDetails(req);
return tm.getAccessToken()
.then((access_token) => {
/*
* attempt to write to custom schema attribute. If not yet created, catch
* that error and provision the custom schema attribute then try again
*/
logger.logWithTS("Existing user SCIM record: " + JSON.stringify(userSCIMRecord));
// this is a deep copy
let newUserSCIMRecord = JSON.parse(JSON.stringify(userSCIMRecord));
if (newUserSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"] == null) {
newUserSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"] = [];
}
let found = false;
for (let i = 0; i < newUserSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"].length && !found; i++) {
let ca = newUserSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"][i];
if (ca.name == "userAccessToken") {
found = true;
ca.values = [ uat ];
}
}
if (!found) {
newUserSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"].push({
"name": "userAccessToken",
"values": [ uat ]
});
}
newUserSCIMRecord["schemas"] = [
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:ibm:2.0:User"
];
let optionalSchemas = [
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
"urn:ietf:params:scim:schemas:extension:ibm:2.0:Notification"
];
for (let i = 0; i < optionalSchemas.length; i++) {
if (newUserSCIMRecord[optionalSchemas[i]] != null) {
newUserSCIMRecord["schemas"].push(optionalSchemas[i]);
}
}
logger.logWithTS("Performing user PUT operation with new record: " + JSON.stringify(newUserSCIMRecord));
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Users/" + userDetails.userSCIMId,
method: "PUT",
headers: {
"Content-type": "application/scim+json",
"Authorization": "Bearer " + access_token
},
body: JSON.stringify(newUserSCIMRecord)
}).then((userRecord) => {
logger.logWithTS("Successfully stored userAccessToken for: " + userDetails.username);
}).catch((e) => {
// if this was a 400 with a particular error detail, attempt creating the schema attribute and then trying again, otherwise give up
if (e != null && e.statusCode == 400 && e.error.indexOf('CSIAI0213E The custom attribute with SCIM name [userAccessToken] was not found') >= 0) {
logger.logWithTS("Trying to JIT-P the custom schema attribute");
// first get the current attributes to see if there is a "spare" custom attribute
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Schema/attributes",
method: "GET",
qs: { "filter" : "customAvailable" },
headers: {
"Accept": "application/scim+json",
"Authorization": "Bearer " + access_token
}
}).then((attributesResponseStr) => {
return JSON.parse(attributesResponseStr);
}).then((attributesResponse) => {
if (attributesResponse != null && attributesResponse.totalResults > 0) {
return attributesResponse.Resources[0].name;
} else {
return null;
}
}).then((customAttrName) => {
//logger.logWithTS("Using custom attribute: " + customAttrName);
if (customAttrName != null) {
let schemaAttributeBody = {
"name": customAttrName,
"displayName": "User Access Token",
"description": "Custom access token to use as an API key for FIDO Photo application",
"scimName": "userAccessToken",
"readOnly": false,
"multiValue": false,
"schemas": [ "urn:ietf:params:scim:schemas:ibm:core:2.0:SchemaAttribute" ],
"type": "string"
};
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Schema/attributes",
method: "POST",
headers: {
"Content-type": "application/scim+json",
"Authorization": "Bearer " + access_token
},
body: JSON.stringify(schemaAttributeBody),
resolveWithFullResponse: true
}).then((httpResponse) => {
if (httpResponse != null && httpResponse.statusCode == 201) {
logger.logWithTS("Successfully created new schema attribute for userAccessToken");
// now retry...
return storeUserAccessToken(req, userSCIMRecord, uat);
} else {
// shouldn't get here
logger.logWithTS("Unable to create new schema attribute for userAccessToken response code: " + httpResponse.statusCode);
logger.logWithTS(JSON.stringify(httpResponse));
}
});
} else {
// shouldn't get here either
logger.logWithTS("Unable to create new schema attribute for userAccessToken because there are no custom attributes available");
}
});
} else {
logger.logWithTS("ciservices.storeUserAccessToken e: " + e + " stringify(e): " + (e != null ? JSON.stringify(e): "null"));
throw e;
}
});
});
}
//
// Used to return the existing application group ID, cached if possible, otherwise
// from the SCIM registry, creating the group if needed
//
function getApplicationGroupID(req) {
// if it's already cached, just return that
if (applicationGroupSCIMId != null) {
return applicationGroupSCIMId;
} else {
// need to look it up / create if necessary
let access_token = null;
return tm.getAccessToken(req)
.then((at) => {
access_token = at;
// lookup group
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Groups",
method: "GET",
qs: {
"filter" : "displayName eq \"" + applicationGroupName + "\"",
"attributes": "id,displayName"
},
headers: {
"Accept": "application/scim+json",
"Authorization": "Bearer " + access_token
},
json: true
});
}).then((scimResponse) => {
// if we got the group, return it's id, otherwise create it, then return the id
if (scimResponse.totalResults == 1) {
applicationGroupSCIMId = scimResponse.Resources[0].id;
return applicationGroupSCIMId;
} else {
// create the group, get id from response and return that
let groupData = {
"displayName": applicationGroupName,
"urn:ietf:params:scim:schemas:extension:ibm:2.0:Group": {
"description": "Application-specific group"
},
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:Group",
"urn:ietf:params:scim:schemas:extension:ibm:2.0:Group"
]
};
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Groups",
method: "POST",
headers: {
"Content-type": "application/scim+json",
"Authorization": "Bearer " + access_token
},
body: JSON.stringify(groupData),
resolveWithFullResponse: true
}).then((httpResponse) => {
if (httpResponse != null && httpResponse.statusCode == 201) {
logger.logWithTS("Successfully created new application group: " + applicationGroupName);
// the ID should be in the body
let applicationGroup = JSON.parse(httpResponse.body);
applicationGroupSCIMId = applicationGroup.id;
return applicationGroupSCIMId;
} else {
throw "Unable to create application group";
}
});
}
});
}
}
//
// Used to check the user is in the application group, and add them if not already there.
//
function validateUserAccount(req) {
let userDetails = getUserDetails(req);
let access_token = null;
return tm.getAccessToken(req)
.then((at) => {
access_token = at;
// this shouldn't happen
if (!userDetails.isAuthenticated()) {
throw "Not authenticated";
}
// get the group id, creating the group if needed
return getApplicationGroupID(req);
}).then((groupId) => {
// pull the full user record
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Users/" + userDetails.userSCIMId,
method: "GET",
headers: {
"Accept": "application/scim+json",
"Authorization": "Bearer " + access_token
},
json: true
});
}).then((userSCIMRecord) => {
// If this user is not active, bail out
if (!userSCIMRecord.active) {
throw "User disabled";
}
// Is the user in the application group? If not, add them
let inApplicationGroup = false;
if (userSCIMRecord["groups"] != null) {
for (let i = 0; i < userSCIMRecord.groups.length && !inApplicationGroup; i++) {
if (userSCIMRecord.groups[i].id == applicationGroupSCIMId) {
inApplicationGroup = true;
}
}
}
if (!inApplicationGroup) {
let patchBody = {
"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
"Operations": [
{ "op": "add", "path": "members", "value": [ { "type": "user", "value": userDetails.userSCIMId } ] }
]
};
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Groups/" + applicationGroupSCIMId,
method: "PATCH",
headers: {
"Content-type": "application/scim+json",
"Authorization": "Bearer " + access_token
},
body: JSON.stringify(patchBody),
resolveWithFullResponse: true
}).then((httpResponse) => {
if (httpResponse != null && httpResponse.statusCode == 204) {
logger.logWithTS("Successfully added user: " + userDetails.userSCIMId + " to application group: " + applicationGroupSCIMId);
} else {
logger.logWithTS("Unable to add user to application group: " + JSON.stringify(httpResponse));
throw "Unable to add user to application group";
}
});
}
});
}
//
// Used to retrieve an existing (or generate a new) "user access token", which is an API key that users are
// assigned to be able to programatically call the FIDO attestation (and assertion) APIs.
//
function getUserAccessToken(req, forceGenerateNew) {
let userDetails = getUserDetails(req);
return tm.getAccessToken(req)
.then((access_token) => {
// if the user session already has a user access token in it, just return that
if (!forceGenerateNew && userDetails.userAccessToken != null) {
return userDetails.userAccessToken;
} else {
// try reading one from the user profile
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/Users/" + userDetails.userSCIMId,
method: "GET",
headers: {
"Accept": "application/scim+json",
"Authorization": "Bearer " + access_token
},
json: true
}).then((userSCIMRecord) => {
if (userSCIMRecord.active) {
// do they have a userAccessToken attribute we want?
let uat = null;
if (!forceGenerateNew) {
if (userSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"] != null) {
for (let i = 0; i < userSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"].length && uat == null; i++) {
let ca = userSCIMRecord["urn:ietf:params:scim:schemas:extension:ibm:2.0:User"]["customAttributes"][i];
if (ca.name == "userAccessToken") {
uat = ca.values[0];
}
}
}
}
if (uat != null) {
// all good, return it
return uat;
} else {
// user didn't have one, or we're forcing generation of a new token
logger.logWithTS("Generating new userAccessToken for user: " + userDetails.username);
uat = tm.generateAccessTokenForUser(userDetails.userSCIMId);
// store it - with error handling in case custom SCIM attribute doesn't yet exist
return storeUserAccessToken(req, userSCIMRecord, uat)
.then(() => {
return uat;
});
}
} else {
logger.logWithTS("User disabled: " + userDetails.userSCIMId);
return null;
}
});
}
}).catch((e) => {
logger.logWithTS("ciservices.getUserAccessToken e: " + e + " stringify(e): " + (e != null ? JSON.stringify(e): "null"));
return null;
});
}
//
// Used to verify the signature information in the exif of an image against registered
// credentials in Cloud Identity. If a matching credential can be found and the signature
// is valid, return information about the credential and the registered owner, otherwise
// return a general failed status.
//
function photoVerifier(req,rsp) {
let result = { "status": "failed" };
try {
let imageHash = req.body.imageHash;
let sigInfoStr = req.body.sigInfo;
let sigInfo = JSON.parse(sigInfoStr);
if (imageHash == null || imageHash.length == 0) {
throw "Invalid image hash";
}
if (sigInfo.credentialId == null) {
throw "Signature Info JSON missing credentialId";
}
if (sigInfo.signature == null) {
throw "Signature Info JSON missing signature";
}
if (sigInfo.authenticatorData == null) {
throw "Signature Info JSON missing authenticatorData";
}
// In Cloud Identity, credentialId is stored in b64u
let credentialId = KJUR.hextob64u(sigInfo.credentialId);
let access_token = null;
// perform lookup on credentialId
tm.getAccessToken(req)
.then( (at) => {
access_token = at;
// until there is a way to search by credId, have to scan all registrations
// TODO - this search does not yet exist in CI, so for now we have to do a scan
// When that is fixed, the search indicated below can be added back in.
//let search = 'attributes/credentialId="' + credentialId + '"';
return requestp({
url: process.env.CI_TENANT_ENDPOINT + "/v2.0/factors/fido2/registrations",
method: "GET",
// qs: { "search": search },
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + access_token
},
json: true
});
}).then((allRegistrations) => {
//logger.logWithTS("ciservices.photoVerifier allRegistrations: " + JSON.stringify(allRegistrations));
let foundReg = null;
// TODO - this can be optomised once the above search criteria works