forked from tngan/samlify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.ts
386 lines (364 loc) Β· 10.2 KB
/
extractor.ts
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
import { DOMParser } from 'xmldom';
import { select, SelectedValue } from 'xpath';
import { uniq, last, zipObject, notEmpty } from './utility';
import camelCase from 'camelcase';
const dom = DOMParser;
interface ExtractorField {
key: string;
localPath: string[] | string[][];
attributes: string[];
index?: string[];
attributePath?: string[];
context?: boolean;
}
export type ExtractorFields = ExtractorField[];
function buildAbsoluteXPath(paths) {
return paths.reduce((currentPath, name) => {
let appendedPath = currentPath;
const isWildcard = name.startsWith('~');
if (isWildcard) {
const pathName = name.replace('~', '');
appendedPath = currentPath + `/*[contains(local-name(), '${pathName}')]`;
}
if (!isWildcard) {
appendedPath = currentPath + `/*[local-name(.)='${name}']`;
}
return appendedPath;
}, '');
}
function buildAttributeXPath(attributes) {
if (attributes.length === 0) {
return '/text()';
}
if (attributes.length === 1) {
return `/@${attributes[0]}`;
}
const filters = attributes.map(attribute => `name()='${attribute}'`).join(' or ');
return `/@*[${filters}]`;
}
export const loginRequestFields: ExtractorFields = [
{
key: 'request',
localPath: ['AuthnRequest'],
attributes: ['ID', 'IssueInstant', 'Destination', 'AssertionConsumerServiceURL']
},
{
key: 'issuer',
localPath: ['AuthnRequest', 'Issuer'],
attributes: []
},
{
key: 'nameIDPolicy',
localPath: ['AuthnRequest', 'NameIDPolicy'],
attributes: ['Format', 'AllowCreate']
},
{
key: 'authnContextClassRef',
localPath: ['AuthnRequest', 'AuthnContextClassRef'],
attributes: []
},
{
key: 'signature',
localPath: ['AuthnRequest', 'Signature'],
attributes: [],
context: true
}
];
// support two-tiers status code
export const loginResponseStatusFields = [
{
key: 'top',
localPath: ['Response', 'Status', 'StatusCode'],
attributes: ['Value'],
},
{
key: 'second',
localPath: ['Response', 'Status', 'StatusCode', 'StatusCode'],
attributes: ['Value'],
}
];
// support two-tiers status code
export const logoutResponseStatusFields = [
{
key: 'top',
localPath: ['LogoutResponse', 'Status', 'StatusCode'],
attributes: ['Value']
},
{
key: 'second',
localPath: ['LogoutResponse', 'Status', 'StatusCode', 'StatusCode'],
attributes: ['Value'],
}
];
export const loginResponseFields: ((asserion: any) => ExtractorFields) = assertion => [
{
key: 'conditions',
localPath: ['Assertion', 'Conditions'],
attributes: ['NotBefore', 'NotOnOrAfter'],
shortcut: assertion
},
{
key: 'response',
localPath: ['Response'],
attributes: ['ID', 'IssueInstant', 'Destination', 'InResponseTo'],
},
{
key: 'audience',
localPath: ['Assertion', 'Conditions', 'AudienceRestriction', 'Audience'],
attributes: [],
shortcut: assertion
},
// {
// key: 'issuer',
// localPath: ['Response', 'Issuer'],
// attributes: []
// },
{
key: 'issuer',
localPath: ['Assertion', 'Issuer'],
attributes: [],
shortcut: assertion
},
{
key: 'nameID',
localPath: ['Assertion', 'Subject', 'NameID'],
attributes: [],
shortcut: assertion
},
{
key: 'sessionIndex',
localPath: ['Assertion', 'AuthnStatement'],
attributes: ['AuthnInstant', 'SessionNotOnOrAfter', 'SessionIndex'],
shortcut: assertion
},
{
key: 'attributes',
localPath: ['Assertion', 'AttributeStatement', 'Attribute'],
index: ['Name'],
attributePath: ['AttributeValue'],
attributes: [],
shortcut: assertion
}
];
export const logoutRequestFields: ExtractorFields = [
{
key: 'request',
localPath: ['LogoutRequest'],
attributes: ['ID', 'IssueInstant', 'Destination']
},
{
key: 'issuer',
localPath: ['LogoutRequest', 'Issuer'],
attributes: []
},
{
key: 'nameID',
localPath: ['LogoutRequest', 'NameID'],
attributes: []
},
{
key: 'signature',
localPath: ['LogoutRequest', 'Signature'],
attributes: [],
context: true
}
];
export const logoutResponseFields: ExtractorFields = [
{
key: 'response',
localPath: ['LogoutResponse'],
attributes: ['ID', 'Destination', 'InResponseTo']
},
{
key: 'issuer',
localPath: ['LogoutResponse', 'Issuer'],
attributes: []
},
{
key: 'signature',
localPath: ['LogoutResponse', 'Signature'],
attributes: [],
context: true
}
];
export function extract(context: string, fields) {
const rootDoc = new dom().parseFromString(context);
return fields.reduce((result: any, field) => {
// get essential fields
const key = field.key;
const localPath = field.localPath;
const attributes = field.attributes;
const isEntire = field.context;
const shortcut = field.shortcut;
// get optional fields
const index = field.index;
const attributePath = field.attributePath;
// set allowing overriding if there is a shortcut injected
let targetDoc = rootDoc;
// if shortcut is used, then replace the doc
// it's a design for overriding the doc used during runtime
if (shortcut) {
targetDoc = new dom().parseFromString(shortcut);
}
// special case: multiple path
/*
{
key: 'issuer',
localPath: [
['Response', 'Issuer'],
['Response', 'Assertion', 'Issuer']
],
attributes: []
}
*/
if (localPath.every(path => Array.isArray(path))) {
const multiXPaths = localPath
.map(path => {
// not support attribute yet, so ignore it
return `${buildAbsoluteXPath(path)}/text()`;
})
.join(' | ');
return {
...result,
[key]: uniq(select(multiXPaths, targetDoc).map((n: Node) => n.nodeValue).filter(notEmpty))
};
}
// eo special case: multiple path
const baseXPath = buildAbsoluteXPath(localPath);
const attributeXPath = buildAttributeXPath(attributes);
// special case: get attributes where some are in child, some are in parent
/*
{
key: 'attributes',
localPath: ['Response', 'Assertion', 'AttributeStatement', 'Attribute'],
index: ['Name'],
attributePath: ['AttributeValue'],
attributes: []
}
*/
if (index && attributePath) {
// find the index in localpath
const indexPath = buildAttributeXPath(index);
const fullLocalXPath = `${baseXPath}${indexPath}`;
const parentNodes = select(baseXPath, targetDoc);
// [uid, mail, edupersonaffiliation], ready for aggregate
const parentAttributes = select(fullLocalXPath, targetDoc).map((n: Attr) => n.value);
// [attribute, attributevalue]
const childXPath = buildAbsoluteXPath([last(localPath)].concat(attributePath));
const childAttributeXPath = buildAttributeXPath(attributes);
const fullChildXPath = `${childXPath}${childAttributeXPath}`;
// [ 'test', '[email protected]', [ 'users', 'examplerole1' ] ]
const childAttributes = parentNodes.map(node => {
const nodeDoc = new dom().parseFromString(node.toString());
if (attributes.length === 0) {
const childValues = select(fullChildXPath, nodeDoc).map((n: Node) => n.nodeValue);
if (childValues.length === 1) {
return childValues[0];
}
return childValues;
}
if (attributes.length > 0) {
const childValues = select(fullChildXPath, nodeDoc).map((n: Attr) => n.value);
if (childValues.length === 1) {
return childValues[0];
}
return childValues;
}
return null;
});
// aggregation
const obj = zipObject(parentAttributes, childAttributes);
return {
...result,
[key]: obj
};
}
// case: fetch entire content, only allow one existence
/*
{
key: 'signature',
localPath: ['AuthnRequest', 'Signature'],
attributes: [],
context: true
}
*/
if (isEntire) {
const node = select(baseXPath, targetDoc);
let value: string | string[] | null = null;
if (node.length === 1) {
value = node[0].toString();
}
if (node.length > 1) {
value = node.map(n => n.toString());
}
return {
...result,
[key]: value
};
}
// case: multiple attribute
/*
{
key: 'nameIDPolicy',
localPath: ['AuthnRequest', 'NameIDPolicy'],
attributes: ['Format', 'AllowCreate']
}
*/
if (attributes.length > 1) {
const baseNode = select(baseXPath, targetDoc).map(n => n.toString());
const childXPath = `${buildAbsoluteXPath([last(localPath)])}${attributeXPath}`;
const attributeValues = baseNode.map((node: string) => {
const nodeDoc = new dom().parseFromString(node);
const values = select(childXPath, nodeDoc).reduce((r: any, n: Attr) => {
r[camelCase(n.name)] = n.value;
return r;
}, {});
return values;
});
return {
...result,
[key]: attributeValues.length === 1 ? attributeValues[0] : attributeValues
};
}
// case: single attribute
/*
{
key: 'statusCode',
localPath: ['Response', 'Status', 'StatusCode'],
attributes: ['Value'],
}
*/
if (attributes.length === 1) {
const fullPath = `${baseXPath}${attributeXPath}`;
const attributeValues = select(fullPath, targetDoc).map((n: Attr) => n.value);
return {
...result,
[key]: attributeValues[0]
};
}
// case: zero attribute
/*
{
key: 'issuer',
localPath: ['AuthnRequest', 'Issuer'],
attributes: []
}
*/
if (attributes.length === 0) {
let attributeValue: SelectedValue[] | Array<(string | null)> | null = null;
const node = select(baseXPath, targetDoc);
if (node.length === 1) {
const fullPath = `string(${baseXPath}${attributeXPath})`;
attributeValue = select(fullPath, targetDoc);
}
if (node.length > 1) {
attributeValue = node.map((n: Node) => n.firstChild!.nodeValue);
}
return {
...result,
[key]: attributeValue
};
}
return result;
}, {});
}