-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
857 lines (765 loc) · 26.8 KB
/
index.js
File metadata and controls
857 lines (765 loc) · 26.8 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const app = express();
var cors = require('cors');
const req = require('express/lib/request');
const { Server } = require("socket.io");
const { createServer } = require('node:http');
const https = require('node:https');
const serv = createServer(app);
const io = new Server(serv, {
cors: {
origin: "https://mrdoc.hiddemo.com",
}
});
;
io.on("connection", (socket) => {
console.log("connect");
});
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
})
app.post('/conng', (req, res) => {
let data = req.body;
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let grant_type = req.body.grant_type;
let username = req.body.username;
let password = req.body.password;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
if (hostname !== null && tenant !== null) {
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/token', {
grant_type: grant_type,
username: username,
password: password,
client_id: client_id,
client_secret: client_secret,
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}
).then(function (response) {
res.send(response?.data);
console.log(response?.data);
}).catch(function (error) {
console.log(error);
//(!!error.response.data ? res.send(error.response.data): console.log('error!'))
res?.send(error?.response?.data)
});
}
/*
*/
//res.send('Data: '+ JSON.stringify(data));
});
app.post("/users", (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm, "");
// Users/.search endpoint with POST and containing a JSON payload to list USERs
axios.post("https://" + hostname + "/scim/" + tenant + "/v2/Users/.search",
JSON.stringify(
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:SearchRequest"
],
"filter": "groups eq \"" + "UG_ROOT" + "\"", // fetching from the root group => fetch all users
"sortBy": "id",
"sortOrder": "descending",
"startIndex": 0, // this can be used to create paged UIs
"count": 100 // (the top limit is a 100, it is impossible to retrieve more within a single request)
}
),
{
headers: {
"Content-Type": "application/scim+json",
"Authorization": `Bearer ${access_token}`
}
}
).then(function (response) {
// IMPORTANT: the data is in "resources"
userList = response.data.resources;
// JUST FOR JAVASCRIPT:
// the way the authenticators are returned makes it difficult with angular JS notation, we just transfer the data in
// a more convenient field of the User object here.
for (var index in userList) {
// putting authenticators in a simple sub object
userList[index].authenticators = userList[index]["urn:hid:scim:api:idp:2.0:UserAuthenticator"].authenticators;
// putting attributes in a simple sub object (mapped by attribute name)
// note that the first name, last name, email are also retrieved as attributes
// from [0 => EMAIL = test@mail.fr, 1 => TITLE = Mr]
// to {EMAIL => test@mail.fr, TITLE => Mr }
var attributesIndexed = userList[index]["urn:hid:scim:api:idp:2.0:UserAttribute"].attributes;
var attributesMappedByName = {};
for (indexAttributes in attributesIndexed) {
attributesMappedByName[attributesIndexed[indexAttributes].name] = attributesIndexed[indexAttributes];
}
userList[index].attributes = attributesMappedByName;
}
console.log(userList);
res.send(userList);
}, function (error) {
console.log(error);
})
});
app.post("/register", (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm, "");
let userEmail = req.body.email;
let userFamily = req.body.familyName;
let userFirstname = req.body.firstName;
let userPhone = req.body.Phone;
console.log(access_token);
// Users endpoint with POST and containing a JSON payload to create a USER
axios.post("https://" + hostname + "/scim/" + tenant + "/v2/Users",
JSON.stringify({
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:hid:scim:api:idp:2.0:Attribute",
"urn:hid:scim:api:idp:2.0:UserDevice"
],
"externalId": userEmail, // could be an auto generated ID
"name": {
"familyName": userFamily,
"givenName": userFirstname
},
"emails": [{
"value": userEmail,
"type": "work"
}],
"groups": [{
"value": "UG_ROOT" // all users will be created in one root group on AaaS, the permissions are managed through roles
}],
"urn:hid:scim:api:idp:2.0:UserAttribute": {
"attributes": [
{
"name": "ATR_MOBILE",
"type": "string",
"value": userPhone
}
]
},
"meta": {
"resourceType": "User"
}
}),
{
headers: {
"Content-Type": "application/scim+json",
"Authorization": `Bearer ${access_token}`// bearer token from OPENID
}
}
).then(function (response) {
// Retrieving the internal ID of the user, which can be used later to request the detail of the User with SCIM.
let createdUserId = response.data.id;
let messageUser = 'The User was created with the id: ' + createdUserId;
res.send(messageUser)
// Refresh the list of users !
})
.catch(function (error) {
res.send(error?.response?.data);
});
});
app.post("/createAuthenticator", (req, res) => {
//initializing
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm, "");
let userId = req.body.Email;
let passwordAuth = req.body.Password;
let policy = req.body.policy;
//console.log(passwordAuthenticator);
createAuthenticatorData = null;
createAuthenticatorDataError = null;
resetPasswordData = null;
resetPasswordDataError = null;
enableAuthenticatorData = null;
enableAuthenticatorDataError = null;
disableAuthenticatorData = null;
disableAuthenticatorDataError = null;
listOfAuthenticatorsDataError = null;
listOfAuthenticators = null;
//find a User by externalId for whom an Authenticator will be created
//The result will be in JSON and will contain arrays of users
axios.post("https://" + hostname + "/scim/" + tenant + "/v2/Users/.search",
JSON.stringify({
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:SearchRequest"
],
"filter": "externalId eq \"" + userId + "\""// we pass the external ID of the user
}),
{
headers: {
"Content-Type": "application/scim+json",
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
)
.then(function (response) {
//if user was found, then Authenticator will be created, otherwise we will get error
if (response.data.resources.length > 0) {
//createAuthenticatorRequest(response.data.resources[0].id, nameAuthenticator);
idUser = response.data.resources[0].id;
axios.post("https://" + hostname + "/scim/" + tenant + "/v2/Authenticator",
JSON.stringify({
"schemas": [
"urn:hid:scim:api:idp:2.0:Authenticator"
],
"policy": {
"value": policy
},
"status": {
"status": "ENABLED",
"expiryDate": "2040-05-15T18:15:21+00:00",
"startDate": "2015-05-15T18:15:21+00:00"
},
"owner": {
"value": idUser // the internal ID of the user (!= external ID), this ID is retrieved via the Users search endpoint
},
"urn:hid:scim:api:idp:2.0:Password": {
"username": userId,
"password": passwordAuth// no need to URL encode here, because of the content type of the request
}
}),
{
headers: {
"Content-Type": "application/scim+json",
"Authorization": `Bearer ${access_token.replace(/(\r?\n|\r)/gm, "")}`
}
}
).then(function (response) {
createAuthenticatorData = "Successfully created password authenticator for " + userId + " user.";
res.send(createAuthenticatorData);
console.log(createAuthenticatorData);
console.log(response);
}, function (error) {
res?.send(error?.response?.data)
console.log(error?.response?.data);
});
} else {
createAuthenticatorDataError = "There are no User with id: " + userId;//+ nameAuthenticator;
res.send(createAuthenticatorDataError);
}
}, function (error) {
createAuthenticatorDataError = error;
res.send(createAuthenticatorDataError);
});
});
// create OTP authenticator
app.post("/createotpAuthenticator", (req, res) => {
//initializing
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm, "");
let userId = req.body.Email;
//console.log(passwordAuthenticator);
createAuthenticatorData = null;
createAuthenticatorDataError = null;
resetPasswordData = null;
resetPasswordDataError = null;
enableAuthenticatorData = null;
enableAuthenticatorDataError = null;
disableAuthenticatorData = null;
disableAuthenticatorDataError = null;
listOfAuthenticatorsDataError = null;
listOfAuthenticators = null;
//find a User by externalId for whom an Authenticator will be created
//The result will be in JSON and will contain arrays of users
axios.post("https://" + hostname + "/scim/" + tenant + "/v2/Users/.search",
JSON.stringify({
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:SearchRequest"
],
"filter": "externalId eq \"" + userId + "\""// we pass the external ID of the user
}),
{
headers: {
"Content-Type": "application/scim+json",
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
)
.then(function (response) {
//if user was found, then Authenticator will be created, otherwise we will get error
if (response.data.resources.length > 0) {
//createAuthenticatorRequest(response.data.resources[0].id, nameAuthenticator);
//res.send(response.data.resources[0].id);
idUser = response.data.resources[0].id;
axios.post("https://" + hostname + "/scim/" + tenant + "/v2/Authenticator",
JSON.stringify({
"schemas": [
"urn:hid:scim:api:idp:2.0:Authenticator"
],
"policy": {
"value": "AT_OTP"
},
"status": {
"status": "ENABLED",
"expiryDate": "2040-05-15T18:15:21+00:00",
"startDate": "2015-05-15T18:15:21+00:00"
},
"owner": {
"value": idUser // the internal ID of the user (!= external ID), this ID is retrieved via the Users search endpoint
}
}),
{
headers: {
"Content-Type": "application/scim+json",
"Authorization": `Bearer ${access_token.replace(/(\r?\n|\r)/gm, "")}`
}
}
).then(function (response) {
createAuthenticatorData = "Successfully created OTP authenticator for " + userId + " user.";
res.send(response.data);
console.log(createAuthenticatorData);
console.log(response);
}, function (error) {
res.send(error.response.data);
console.log(error);
});
} else {
createAuthenticatorDataError = "There are no User with id: " + userId;//+ nameAuthenticator;
res.send(createAuthenticatorDataError);
}
}, function (error) {
createAuthenticatorDataError = error.response.data;
res.send(createAuthenticatorDataError);
});
});
// Test Password Authenticator.
app.post('/passauth', (req, res) => {
let userName = req.body.username;
let password = req.body.password;
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
//let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm,"");
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/token',
"grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//"Authorization" : `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
client_token = response.data.access_token;
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/token', {
grant_type: 'password',
username: userName,
password: password,
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
"Authorization": `Bearer ${client_token}` // bearer token from OPENID
}
}
).then(function (response) {
res.send(response?.data);
console.log(response?.data);
}).catch(function (error) {
console.log(error);
//(!!error.response.data ? res.send(error.response.data): console.log('error!'))
res?.send(error?.response?.data)
});
}, function (error) {
res?.send(error?.response?.data)
});
});
// Test HID Approve TOTP Authentication.
app.post('/approvetotp', (req, res) => {
let userName = req.body.username;
let password = req.body.password;
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
//let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm,"");
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/token',
"grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//"Authorization" : `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
client_token = response.data.access_token;
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/token', {
grant_type: 'password',
username: userName,
mode: 'SYNCHRONOUS',
authType: 'AT_EMPOTP',
password: password,
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
"Authorization": `Bearer ${client_token}` // bearer token from OPENID
}
}
).then(function (response) {
res.send(response?.data);
console.log(response?.data);
}).catch(function (error) {
console.log(error);
//(!!error.response.data ? res.send(error.response.data): console.log('error!'))
res?.send(error?.response?.data)
});
}, function (error) {
res?.send(error?.response?.data)
});
});
// Device List
app.post('/devicelist', (req, res) => {
let userid = req.body.userid;
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
let access_token = req.body.access_token;
//client_token = response.data.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
axios.post('https://' + hostname + '/scim/' + tenant + '/v2/Users/.search', JSON.stringify({
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:SearchRequest"
],
"filter": "externalId eq \"" + userid + "\""// we pass the external ID of the user
}), {
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
userinternalID = response.data.resources['0'].id;
axios.post('https://' + hostname + '/scim/' + tenant + '/v2/Device/.search', JSON.stringify({
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:SearchRequest"
],
"filter": "owner.value eq \"" + userinternalID + "\"",
"sortBy": "id",
"sortOrder": "descending",
"startIndex": 0,
"count": 100
}), {
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
res.send(response?.data);
})
}).catch(function (error) {
console.log(error);
//(!!error.response.data ? res.send(error.response.data): console.log('error!'))
res?.send(error?.response?.data)
});
});
// Send Push Notification
app.post('/bcauthorize', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
let bcPayload = req.body.bcPayload;
//let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm,"");
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/token',
"grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//"Authorization" : `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
client_token = response.data.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/bcauthorize', bcPayload, {
headers: {
'Content-Type': 'application/json',
"Authorization": `Bearer ${client_token}` // bearer token from OPENID
}
}
).then(function (response) {
res.send(response?.data);
console.log(response?.data);
}).catch(function (error) {
console.log(error);
//(!!error.response.data ? res.send(error.response.data): console.log('error!'))
res?.send(error?.response?.data)
});
}, function (error) {
res?.send(error?.response?.data)
});
});
// Recieve callback CIBA
app.post('/callback_url', (req, res) => {
const idtoken = req.body.id_token;
function parseJwt(token) {
return JSON.parse(Buffer.from(token?.split('.')[1], 'base64')?.toString());
}
const parsed_token = parseJwt(idtoken);
console.log(parsed_token);
io.emit('clientstatus', parsed_token.clientapprovalstatus);
});
app.post('/userinfo', (req, res) => {
const access_token = req.body.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
const tenant = req.body.tenant;
const hostname = req.body.hostname;
axios.get('https://' + hostname + '/idp/' + tenant + '/authn/userinfo',
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
function parseJwt(token) {
return JSON.parse(Buffer.from(token?.split('.')[1], 'base64')?.toString());
}
const userinfo = parseJwt(response.data);
res.send(userinfo);
}, function (error) {
res?.send(error?.response?.data)
});
});
// Clone a device to allow rooted device type.
app.post('/clonedevice', (req, res) => {
let userName = req.body.username;
let password = req.body.password;
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
let access_token = req.body.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
let cPayload = req.body.cPayload;
axios.post('https://' + hostname + '/configuration/' + tenant + '/v2/Device/Type/?api-version=8', cPayload,
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
console.log(response.data)
res.send(response.data);
}, function (error) {
DataError = error;
console.log(error.data);
res?.send(error?.response?.data)
});
});
// Create a device
app.post('/createdevice', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
let access_token = req.body.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
let body = req.body.bPayload;
console.log(access_token);
axios.post('https://' + hostname + '/scim/' + tenant + '/v2/Device/?api-version=7', body,
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
console.log(response);
res.send(response.data);
}, function (error) {
console.log(error);
res?.send(error?.response?.data)
});
});
// Get a known device
app.post('/getdevice', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
let deviceID = req.body.deviceID;
console.log(access_token);
axios.get('https://' + hostname + '/scim/' + tenant + '/v2/Device/' + deviceID + '?api-version=7',
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
console.log(response);
res.send(response.data);
}, function (error) {
console.log(error);
res?.send(error?.response?.data)
});
});
// Assign a device to a user
app.post('/assigndevice', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
let deviceID = req.body.deviceID;
let jPayload = req.body.cPayload;
axios.put('https://' + hostname + '/scim/' + tenant + '/v2/Device/' + deviceID + '?api-version=7', jPayload,
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
console.log(response);
res.send(response.data);
}, function (error) {
console.log(error);
res?.send(error?.response?.data)
});
});
// Provision device
app.post('/provisiondevice', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(?:\\[rn]|[\r\n]+)+/g, "");
let body = req.body.cPayload;
axios.post('https://' + hostname + '/scim/' + tenant + '/v2/Device/Provision/?api-version=7', body,
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
console.log(response);
res.send(response.data);
}, function (error) {
console.log(error);
res?.send(error?.response?.data)
});
});
// Search for Device type and External ID.
app.post('/sdevice', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm, "");
let sPayload = req.body.sdevicePayload;
axios.post('https://' + hostname + '/scim/' + tenant + '/v2/Device/.search', sPayload,
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
console.log(response.data)
res.send(response.data)
}, function (error) {
DataError = error.message;
console.log(error);
res?.send(error?.response?.data)
});
});
// Assign Device
app.post('/asdevice', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm, "");
let sPayload = req.body.adevicePayload;
let deviceID = req.body.deviceiID;
axios.put('https://' + hostname + '/scim/' + tenant + '/v2/Device/' + deviceID, sPayload,
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID
}
}
).then(function (response) {
console.log(response.data)
res.send(response.data)
}, function (error) {
res?.send(error?.response?.data)
});
});
// Update CIBA Listener
app.post('/updateCB', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let access_token = req.body.access_token.replace(/(\r?\n|\r)/gm, "");
let sbPayload = req.body.cbPayload;
let client_id = req.body.client_id;
axios.post('https://' + hostname + '/scim/' + tenant + '/v2/Users/.search?api-version=9', {
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:SearchRequest"
],
"filter": "username eq " + client_id,
"sortBy": "id",
"sortOrder": "descending",
"startIndex": 0,
"count": 100
},
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID /configuration/{tenant}/v2/User/AttributeType/{uid}
}
}
).then(function (response) {
let cInternalid = response.data.resources[0].id;
console.log(response);
console.log(cInternalid);
axios.put('https://' + hostname + '/scim/' + tenant + '/v2/Users/' + cInternalid + '?api-version=9', sbPayload,
{
headers: {
'Content-Type': 'application/scim+json',
"Authorization": `Bearer ${access_token}` // bearer token from OPENID /configuration/{tenant}/v2/User/AttributeType/{uid}
}
}
).then(function (response) {
console.log(response.data)
res.send(response.data)
}, function (error) {
res?.send(error?.response?.data)
});
});
});
// OTP Authentication
app.post('/otpauth', (req, res) => {
let hostname = req.body.hostname;
let tenant = req.body.tenant;
let username = req.body.username;
let otppassword = req.body.password1;
let client_id = req.body.client_id;
let client_secret = req.body.client_secret;
if (hostname !== '' && tenant !== '') {
axios.post('https://' + hostname + '/idp/' + tenant + '/authn/token', {
mode: 'SYNCHRONOUS',
authType: 'AT_OTP',
grant_type: 'password',
username: username,
password: otppassword,
client_id: client_id,
client_secret: client_secret,
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}
).then(function (response) {
res.send(response?.data);
console.log(response?.data);
}).catch(function (error) {
console.log(error);
res?.send(error?.response?.data)
});
}
});
serv.listen(4000, () => {
console.log('listening on port 4000')
});