-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomClaimProviderLDAP.js
111 lines (91 loc) · 3.95 KB
/
CustomClaimProviderLDAP.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
// Import necessary modules
import ldap from "ldapjs";
import util from "util";
// Export a default component
export default defineComponent({
async run({ steps, $ }) {
//From the Previous Step get the Uid
const AZUREAD_LoginName = steps.trigger.event.body.data.authenticationContext.user.userPrincipalName;
const uid = AZUREAD_LoginName.split('@')[0];
// Create the body object with initial data
const body = {
data: {
'@odata.type': 'microsoft.graph.onTokenIssuanceStartResponseData',
actions: [
{
'@odata.type': 'microsoft.graph.provideClaimsForToken',
claims: {
correlationId: steps.trigger.event.body.data.authenticationContext.correlationId,
apiVersion: '1.0.0',
dateOfBirth: '31/01/2020',
telephoneNumber: '123456789',
customRoles: ['NewYear2024', 'Writer', 'Reader'],
upn: steps.trigger.event.body.data.authenticationContext.user.userPrincipalName,
},
},
],
},
};
try {
// Create an LDAP client
const client = ldap.createClient({
url: 'ldap://ldap.forumsys.com:389',
});
// Set the bind DN and password
const bindDN = 'cn=read-only-admin,dc=example,dc=com';
const bindPassword = 'password';
// Promisify the bind and search functions
const bindAsync = util.promisify(client.bind.bind(client));
const searchAsync = util.promisify(client.search.bind(client));
// Bind to the LDAP server
await bindAsync(bindDN, bindPassword);
// Declare a variable to store the telephone number
let telephoneNumber;
// Set the search options
const opts = {
filter: `(uid=${uid})`,
scope: 'sub'
};
// Perform the LDAP search
const res = await searchAsync('dc=example,dc=com', opts);
// Handle search events
res.on('searchRequest', (searchRequest) => {
console.log('searchRequest: ', searchRequest.messageId);
});
res.on('searchEntry', (entry) => {
console.log('entry: ' + JSON.stringify(entry.pojo));
const result = entry.pojo;
const telephoneNumberAttribute = result.attributes.find((attribute) => attribute.type === 'telephoneNumber');
telephoneNumber = telephoneNumberAttribute ? telephoneNumberAttribute.values[0] : null;
});
res.on('searchReference', (referral) => {
console.log('referral: ' + referral.uris.join());
});
res.on('error', (err) => {
console.error('error: ' + err.message);
});
res.on('end', (result) => {
console.log('status: ' + result.status);
});
// Wait for the LDAP operation to complete
await new Promise((resolve) => {
res.on('end', () => {
resolve();
});
});
// Update the telephoneNumber claim in the body object if it was found
if (telephoneNumber) {
body.data.actions[0].claims.telephoneNumber = telephoneNumber;
}
}
catch (error) {
console.error('LDAP operation failed:', error);
}
// Respond with the updated body object
await $.respond({
status: 200,
headers: { 'Content-Type': 'application/json' },
body,
});
},
});