samlesa 2.12.3 → 2.12.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of samlesa might be problematic. Click here for more details.

Files changed (66) hide show
  1. package/build/index.js +54 -64
  2. package/build/index.js.map +1 -1
  3. package/build/src/api.js +24 -23
  4. package/build/src/api.js.map +1 -1
  5. package/build/src/binding-post.js +358 -368
  6. package/build/src/binding-post.js.map +1 -1
  7. package/build/src/binding-redirect.js +333 -332
  8. package/build/src/binding-redirect.js.map +1 -1
  9. package/build/src/binding-simplesign.js +222 -232
  10. package/build/src/binding-simplesign.js.map +1 -1
  11. package/build/src/entity-idp.js +132 -130
  12. package/build/src/entity-idp.js.map +1 -1
  13. package/build/src/entity-sp.js +96 -96
  14. package/build/src/entity-sp.js.map +1 -1
  15. package/build/src/entity.js +225 -235
  16. package/build/src/entity.js.map +1 -1
  17. package/build/src/extractor.js +369 -369
  18. package/build/src/extractor.js.map +1 -1
  19. package/build/src/flow.js +320 -319
  20. package/build/src/flow.js.map +1 -1
  21. package/build/src/libsaml.js +660 -641
  22. package/build/src/libsaml.js.map +1 -1
  23. package/build/src/metadata-idp.js +127 -127
  24. package/build/src/metadata-idp.js.map +1 -1
  25. package/build/src/metadata-sp.js +231 -231
  26. package/build/src/metadata-sp.js.map +1 -1
  27. package/build/src/metadata.js +166 -176
  28. package/build/src/metadata.js.map +1 -1
  29. package/build/src/types.js +11 -11
  30. package/build/src/urn.js +212 -212
  31. package/build/src/urn.js.map +1 -1
  32. package/build/src/utility.js +292 -248
  33. package/build/src/utility.js.map +1 -1
  34. package/build/src/validator.js +27 -26
  35. package/build/src/validator.js.map +1 -1
  36. package/index.d.ts +10 -10
  37. package/index.js +18 -18
  38. package/package.json +1 -5
  39. package/qodana.yaml +29 -29
  40. package/src/binding-post.ts +1 -1
  41. package/src/binding-redirect.ts +83 -64
  42. package/src/entity-idp.ts +26 -20
  43. package/src/libsaml.ts +79 -48
  44. package/src/utility.ts +147 -76
  45. package/types/index.d.ts +10 -10
  46. package/types/src/api.d.ts +13 -13
  47. package/types/src/binding-post.d.ts +46 -46
  48. package/types/src/binding-redirect.d.ts +52 -52
  49. package/types/src/binding-simplesign.d.ts +39 -39
  50. package/types/src/entity-idp.d.ts +35 -42
  51. package/types/src/entity-sp.d.ts +36 -36
  52. package/types/src/entity.d.ts +101 -99
  53. package/types/src/extractor.d.ts +25 -25
  54. package/types/src/flow.d.ts +6 -6
  55. package/types/src/libsaml.d.ts +200 -210
  56. package/types/src/metadata-idp.d.ts +24 -24
  57. package/types/src/metadata-sp.d.ts +36 -36
  58. package/types/src/metadata.d.ts +59 -57
  59. package/types/src/types.d.ts +129 -127
  60. package/types/src/urn.d.ts +194 -194
  61. package/types/src/utility.d.ts +134 -134
  62. package/types/src/validator.d.ts +3 -3
  63. package/.idea/compiler.xml +0 -6
  64. package/.idea/deployment.xml +0 -14
  65. package/.idea/jsLibraryMappings.xml +0 -6
  66. package/build/.idea/workspace.xml +0 -58
@@ -1,369 +1,359 @@
1
- "use strict";
2
- /**
3
- * @file binding-post.ts
4
- * @author tngan
5
- * @desc Binding-level API, declare the functions using POST binding
6
- */
7
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
- if (k2 === undefined) k2 = k;
9
- var desc = Object.getOwnPropertyDescriptor(m, k);
10
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
- desc = { enumerable: true, get: function() { return m[k]; } };
12
- }
13
- Object.defineProperty(o, k2, desc);
14
- }) : (function(o, m, k, k2) {
15
- if (k2 === undefined) k2 = k;
16
- o[k2] = m[k];
17
- }));
18
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
- Object.defineProperty(o, "default", { enumerable: true, value: v });
20
- }) : function(o, v) {
21
- o["default"] = v;
22
- });
23
- var __importStar = (this && this.__importStar) || (function () {
24
- var ownKeys = function(o) {
25
- ownKeys = Object.getOwnPropertyNames || function (o) {
26
- var ar = [];
27
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
28
- return ar;
29
- };
30
- return ownKeys(o);
31
- };
32
- return function (mod) {
33
- if (mod && mod.__esModule) return mod;
34
- var result = {};
35
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
36
- __setModuleDefault(result, mod);
37
- return result;
38
- };
39
- })();
40
- var __importDefault = (this && this.__importDefault) || function (mod) {
41
- return (mod && mod.__esModule) ? mod : { "default": mod };
42
- };
43
- Object.defineProperty(exports, "__esModule", { value: true });
44
- const urn_js_1 = require("./urn.js");
45
- const libsaml_js_1 = __importDefault(require("./libsaml.js"));
46
- const utility_js_1 = __importStar(require("./utility.js"));
47
- const binding = urn_js_1.wording.binding;
48
- /**
49
- * @desc Generate a base64 encoded login request
50
- * @param {string} referenceTagXPath reference uri
51
- * @param {object} entity object includes both idp and sp
52
- * @param {function} customTagReplacement used when developers have their own login response template
53
- */
54
- function base64LoginRequest(referenceTagXPath, entity, customTagReplacement) {
55
- const metadata = { idp: entity.idp.entityMeta, sp: entity.sp.entityMeta };
56
- const spSetting = entity.sp.entitySetting;
57
- let id = '';
58
- if (metadata && metadata.idp && metadata.sp) {
59
- const base = metadata.idp.getSingleSignOnService(binding.post);
60
- let rawSamlRequest;
61
- if (spSetting.loginRequestTemplate && customTagReplacement) {
62
- const info = customTagReplacement(spSetting.loginRequestTemplate.context);
63
- id = (0, utility_js_1.get)(info, 'id', null);
64
- rawSamlRequest = (0, utility_js_1.get)(info, 'context', null);
65
- }
66
- else {
67
- const nameIDFormat = spSetting.nameIDFormat;
68
- const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
69
- id = spSetting.generateID();
70
- rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginRequestTemplate.context, {
71
- ID: id,
72
- Destination: base,
73
- Issuer: metadata.sp.getEntityID(),
74
- IssueInstant: new Date().toISOString(),
75
- AssertionConsumerServiceURL: metadata.sp.getAssertionConsumerService(binding.post),
76
- EntityID: metadata.sp.getEntityID(),
77
- AllowCreate: spSetting.allowCreate,
78
- NameIDFormat: selectedNameIDFormat
79
- });
80
- }
81
- if (metadata.idp.isWantAuthnRequestsSigned()) {
82
- const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm, transformationAlgorithms } = spSetting;
83
- return {
84
- id,
85
- context: libsaml_js_1.default.constructSAMLSignature({
86
- referenceTagXPath,
87
- privateKey,
88
- privateKeyPass,
89
- signatureAlgorithm,
90
- transformationAlgorithms,
91
- rawSamlMessage: rawSamlRequest,
92
- signingCert: metadata.sp.getX509Certificate('signing'),
93
- signatureConfig: spSetting.signatureConfig || {
94
- prefix: 'ds',
95
- location: { reference: "/*[local-name(.)='AuthnRequest']/*[local-name(.)='Issuer']", action: 'after' },
96
- }
97
- }),
98
- };
99
- }
100
- // No need to embeded XML signature
101
- return {
102
- id,
103
- context: utility_js_1.default.base64Encode(rawSamlRequest),
104
- };
105
- }
106
- throw new Error('ERR_GENERATE_POST_LOGIN_REQUEST_MISSING_METADATA');
107
- }
108
- /**
109
- * @desc Generate a base64 encoded login response
110
- * @param {object} requestInfo corresponding request, used to obtain the id
111
- * @param {object} entity object includes both idp and sp
112
- * @param {object} user current logged user (e.g. req.user)
113
- * @param {function} customTagReplacement used when developers have their own login response template
114
- * @param {boolean} encryptThenSign whether or not to encrypt then sign first (if signing). Defaults to sign-then-encrypt
115
- */
116
- async function base64LoginResponse(requestInfo = {}, entity, user = {}, customTagReplacement, encryptThenSign = false) {
117
- const idpSetting = entity.idp.entitySetting;
118
- const spSetting = entity.sp.entitySetting;
119
- const id = idpSetting.generateID();
120
- const metadata = {
121
- idp: entity.idp.entityMeta,
122
- sp: entity.sp.entityMeta,
123
- };
124
- const nameIDFormat = idpSetting.nameIDFormat;
125
- const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
126
- if (metadata && metadata.idp && metadata.sp) {
127
- const base = metadata.sp.getAssertionConsumerService(binding.post);
128
- let rawSamlResponse;
129
- const nowTime = new Date();
130
- const spEntityID = metadata.sp.getEntityID();
131
- const fiveMinutesLaterTime = new Date(nowTime.getTime());
132
- fiveMinutesLaterTime.setMinutes(fiveMinutesLaterTime.getMinutes() + 5);
133
- const fiveMinutesLater = fiveMinutesLaterTime.toISOString();
134
- const now = nowTime.toISOString();
135
- const acl = metadata.sp.getAssertionConsumerService(binding.post);
136
- const tvalue = {
137
- ID: id,
138
- AssertionID: idpSetting.generateID(),
139
- Destination: base,
140
- Audience: spEntityID,
141
- EntityID: spEntityID,
142
- SubjectRecipient: acl,
143
- Issuer: metadata.idp.getEntityID(),
144
- IssueInstant: now,
145
- AssertionConsumerServiceURL: acl,
146
- StatusCode: urn_js_1.StatusCode.Success,
147
- // can be customized
148
- ConditionsNotBefore: now,
149
- ConditionsNotOnOrAfter: fiveMinutesLater,
150
- SubjectConfirmationDataNotOnOrAfter: fiveMinutesLater,
151
- NameIDFormat: selectedNameIDFormat,
152
- NameID: user.email || '',
153
- InResponseTo: (0, utility_js_1.get)(requestInfo, 'extract.request.id', ''),
154
- AuthnStatement: '',
155
- AttributeStatement: '',
156
- };
157
- if (idpSetting.loginResponseTemplate && customTagReplacement) {
158
- const template = customTagReplacement(idpSetting.loginResponseTemplate.context);
159
- rawSamlResponse = (0, utility_js_1.get)(template, 'context', null);
160
- }
161
- else {
162
- if (requestInfo !== null) {
163
- tvalue.InResponseTo = requestInfo.extract.request.id;
164
- }
165
- rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginResponseTemplate.context, tvalue);
166
- }
167
- const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm } = idpSetting;
168
- const config = {
169
- privateKey,
170
- privateKeyPass,
171
- signatureAlgorithm,
172
- signingCert: metadata.idp.getX509Certificate('signing'),
173
- isBase64Output: false,
174
- };
175
- // step: sign assertion ? -> encrypted ? -> sign message ?
176
- if (metadata.sp.isWantAssertionsSigned()) {
177
- // console.debug('sp wants assertion signed');
178
- rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
179
- ...config,
180
- rawSamlMessage: rawSamlResponse,
181
- transformationAlgorithms: spSetting.transformationAlgorithms,
182
- referenceTagXPath: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']",
183
- signatureConfig: {
184
- prefix: 'ds',
185
- location: { reference: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']/*[local-name(.)='Issuer']", action: 'after' },
186
- },
187
- });
188
- }
189
- // console.debug('after assertion signed', rawSamlResponse);
190
- // SAML response must be signed sign message first, then encrypt
191
- if (!encryptThenSign && (spSetting.wantMessageSigned || !metadata.sp.isWantAssertionsSigned())) {
192
- // console.debug('sign then encrypt and sign entire message');
193
- rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
194
- ...config,
195
- rawSamlMessage: rawSamlResponse,
196
- isMessageSigned: true,
197
- transformationAlgorithms: spSetting.transformationAlgorithms,
198
- signatureConfig: spSetting.signatureConfig || {
199
- prefix: 'ds',
200
- location: { reference: "/*[local-name(.)='Response']/*[local-name(.)='Issuer']", action: 'after' },
201
- },
202
- });
203
- console.log(rawSamlResponse);
204
- console.log("这他妈是什么------------------");
205
- }
206
- // console.debug('after message signed', rawSamlResponse);
207
- if (idpSetting.isAssertionEncrypted) {
208
- // console.debug('idp is configured to do encryption');
209
- const context = await libsaml_js_1.default.encryptAssertion(entity.idp, entity.sp, rawSamlResponse);
210
- if (encryptThenSign) {
211
- //need to decode it
212
- rawSamlResponse = utility_js_1.default.base64Decode(context);
213
- }
214
- else {
215
- return Promise.resolve({ id, context });
216
- }
217
- }
218
- //sign after encrypting
219
- if (encryptThenSign && (spSetting.wantMessageSigned || !metadata.sp.isWantAssertionsSigned())) {
220
- rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
221
- ...config,
222
- rawSamlMessage: rawSamlResponse,
223
- isMessageSigned: true,
224
- transformationAlgorithms: spSetting.transformationAlgorithms,
225
- signatureConfig: spSetting.signatureConfig || {
226
- prefix: 'ds',
227
- location: { reference: "/*[local-name(.)='Response']/*[local-name(.)='Issuer']", action: 'after' },
228
- },
229
- });
230
- }
231
- return Promise.resolve({
232
- id,
233
- context: utility_js_1.default.base64Encode(rawSamlResponse),
234
- });
235
- }
236
- throw new Error('ERR_GENERATE_POST_LOGIN_RESPONSE_MISSING_METADATA');
237
- }
238
- /**
239
- * @desc Generate a base64 encoded logout request
240
- * @param {object} user current logged user (e.g. req.user)
241
- * @param {string} referenceTagXPath reference uri
242
- * @param {object} entity object includes both idp and sp
243
- * @param {function} customTagReplacement used when developers have their own login response template
244
- * @return {string} base64 encoded request
245
- */
246
- function base64LogoutRequest(user, referenceTagXPath, entity, customTagReplacement) {
247
- const metadata = { init: entity.init.entityMeta, target: entity.target.entityMeta };
248
- const initSetting = entity.init.entitySetting;
249
- const nameIDFormat = initSetting.nameIDFormat;
250
- const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
251
- let id = '';
252
- if (metadata && metadata.init && metadata.target) {
253
- let rawSamlRequest;
254
- if (initSetting.logoutRequestTemplate && customTagReplacement) {
255
- const template = customTagReplacement(initSetting.logoutRequestTemplate.context);
256
- id = (0, utility_js_1.get)(template, 'id', null);
257
- rawSamlRequest = (0, utility_js_1.get)(template, 'context', null);
258
- }
259
- else {
260
- id = initSetting.generateID();
261
- const tvalue = {
262
- ID: id,
263
- Destination: metadata.target.getSingleLogoutService(binding.post),
264
- Issuer: metadata.init.getEntityID(),
265
- IssueInstant: new Date().toISOString(),
266
- EntityID: metadata.init.getEntityID(),
267
- NameIDFormat: selectedNameIDFormat,
268
- NameID: user.logoutNameID,
269
- };
270
- rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutRequestTemplate.context, tvalue);
271
- }
272
- if (entity.target.entitySetting.wantLogoutRequestSigned) {
273
- // Need to embeded XML signature
274
- const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm, transformationAlgorithms } = initSetting;
275
- return {
276
- id,
277
- context: libsaml_js_1.default.constructSAMLSignature({
278
- referenceTagXPath,
279
- privateKey,
280
- privateKeyPass,
281
- signatureAlgorithm,
282
- transformationAlgorithms,
283
- rawSamlMessage: rawSamlRequest,
284
- signingCert: metadata.init.getX509Certificate('signing'),
285
- signatureConfig: initSetting.signatureConfig || {
286
- prefix: 'ds',
287
- location: { reference: "/*[local-name(.)='LogoutRequest']/*[local-name(.)='Issuer']", action: 'after' },
288
- }
289
- }),
290
- };
291
- }
292
- return {
293
- id,
294
- context: utility_js_1.default.base64Encode(rawSamlRequest),
295
- };
296
- }
297
- throw new Error('ERR_GENERATE_POST_LOGOUT_REQUEST_MISSING_METADATA');
298
- }
299
- /**
300
- * @desc Generate a base64 encoded logout response
301
- * @param {object} requestInfo corresponding request, used to obtain the id
302
- * @param {string} referenceTagXPath reference uri
303
- * @param {object} entity object includes both idp and sp
304
- * @param {function} customTagReplacement used when developers have their own login response template
305
- */
306
- function base64LogoutResponse(requestInfo, entity, customTagReplacement) {
307
- const metadata = {
308
- init: entity.init.entityMeta,
309
- target: entity.target.entityMeta,
310
- };
311
- let id = '';
312
- const initSetting = entity.init.entitySetting;
313
- if (metadata && metadata.init && metadata.target) {
314
- let rawSamlResponse;
315
- if (initSetting.logoutResponseTemplate) {
316
- const template = customTagReplacement(initSetting.logoutResponseTemplate.context);
317
- id = template.id;
318
- rawSamlResponse = template.context;
319
- }
320
- else {
321
- id = initSetting.generateID();
322
- const tvalue = {
323
- ID: id,
324
- Destination: metadata.target.getSingleLogoutService(binding.post),
325
- EntityID: metadata.init.getEntityID(),
326
- Issuer: metadata.init.getEntityID(),
327
- IssueInstant: new Date().toISOString(),
328
- StatusCode: urn_js_1.StatusCode.Success,
329
- InResponseTo: (0, utility_js_1.get)(requestInfo, 'extract.request.id', null)
330
- };
331
- rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutResponseTemplate.context, tvalue);
332
- }
333
- if (entity.target.entitySetting.wantLogoutResponseSigned) {
334
- const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm, transformationAlgorithms } = initSetting;
335
- return {
336
- id,
337
- context: libsaml_js_1.default.constructSAMLSignature({
338
- isMessageSigned: true,
339
- transformationAlgorithms: transformationAlgorithms,
340
- privateKey,
341
- privateKeyPass,
342
- signatureAlgorithm,
343
- rawSamlMessage: rawSamlResponse,
344
- signingCert: metadata.init.getX509Certificate('signing'),
345
- signatureConfig: {
346
- prefix: 'ds',
347
- location: {
348
- reference: "/*[local-name(.)='LogoutResponse']/*[local-name(.)='Issuer']",
349
- action: 'after'
350
- }
351
- }
352
- }),
353
- };
354
- }
355
- return {
356
- id,
357
- context: utility_js_1.default.base64Encode(rawSamlResponse),
358
- };
359
- }
360
- throw new Error('ERR_GENERATE_POST_LOGOUT_RESPONSE_MISSING_METADATA');
361
- }
362
- const postBinding = {
363
- base64LoginRequest,
364
- base64LoginResponse,
365
- base64LogoutRequest,
366
- base64LogoutResponse,
367
- };
368
- exports.default = postBinding;
1
+ "use strict";
2
+ /**
3
+ * @file binding-post.ts
4
+ * @author tngan
5
+ * @desc Binding-level API, declare the functions using POST binding
6
+ */
7
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
+ if (k2 === undefined) k2 = k;
9
+ var desc = Object.getOwnPropertyDescriptor(m, k);
10
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
+ desc = { enumerable: true, get: function() { return m[k]; } };
12
+ }
13
+ Object.defineProperty(o, k2, desc);
14
+ }) : (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ }));
18
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
20
+ }) : function(o, v) {
21
+ o["default"] = v;
22
+ });
23
+ var __importStar = (this && this.__importStar) || function (mod) {
24
+ if (mod && mod.__esModule) return mod;
25
+ var result = {};
26
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
27
+ __setModuleDefault(result, mod);
28
+ return result;
29
+ };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ const urn_js_1 = require("./urn.js");
35
+ const libsaml_js_1 = __importDefault(require("./libsaml.js"));
36
+ const utility_js_1 = __importStar(require("./utility.js"));
37
+ const binding = urn_js_1.wording.binding;
38
+ /**
39
+ * @desc Generate a base64 encoded login request
40
+ * @param {string} referenceTagXPath reference uri
41
+ * @param {object} entity object includes both idp and sp
42
+ * @param {function} customTagReplacement used when developers have their own login response template
43
+ */
44
+ function base64LoginRequest(referenceTagXPath, entity, customTagReplacement) {
45
+ const metadata = { idp: entity.idp.entityMeta, sp: entity.sp.entityMeta };
46
+ const spSetting = entity.sp.entitySetting;
47
+ let id = '';
48
+ if (metadata && metadata.idp && metadata.sp) {
49
+ const base = metadata.idp.getSingleSignOnService(binding.post);
50
+ let rawSamlRequest;
51
+ if (spSetting.loginRequestTemplate && customTagReplacement) {
52
+ const info = customTagReplacement(spSetting.loginRequestTemplate.context);
53
+ id = (0, utility_js_1.get)(info, 'id', null);
54
+ rawSamlRequest = (0, utility_js_1.get)(info, 'context', null);
55
+ }
56
+ else {
57
+ const nameIDFormat = spSetting.nameIDFormat;
58
+ const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
59
+ id = spSetting.generateID();
60
+ rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginRequestTemplate.context, {
61
+ ID: id,
62
+ Destination: base,
63
+ Issuer: metadata.sp.getEntityID(),
64
+ IssueInstant: new Date().toISOString(),
65
+ AssertionConsumerServiceURL: metadata.sp.getAssertionConsumerService(binding.post),
66
+ EntityID: metadata.sp.getEntityID(),
67
+ AllowCreate: spSetting.allowCreate,
68
+ NameIDFormat: selectedNameIDFormat
69
+ });
70
+ }
71
+ if (metadata.idp.isWantAuthnRequestsSigned()) {
72
+ const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm, transformationAlgorithms } = spSetting;
73
+ return {
74
+ id,
75
+ context: libsaml_js_1.default.constructSAMLSignature({
76
+ referenceTagXPath,
77
+ privateKey,
78
+ privateKeyPass,
79
+ signatureAlgorithm,
80
+ transformationAlgorithms,
81
+ rawSamlMessage: rawSamlRequest,
82
+ signingCert: metadata.sp.getX509Certificate('signing'),
83
+ signatureConfig: spSetting.signatureConfig || {
84
+ prefix: 'ds',
85
+ location: { reference: "/*[local-name(.)='AuthnRequest']/*[local-name(.)='Issuer']", action: 'after' },
86
+ }
87
+ }),
88
+ };
89
+ }
90
+ // No need to embeded XML signature
91
+ return {
92
+ id,
93
+ context: utility_js_1.default.base64Encode(rawSamlRequest),
94
+ };
95
+ }
96
+ throw new Error('ERR_GENERATE_POST_LOGIN_REQUEST_MISSING_METADATA');
97
+ }
98
+ /**
99
+ * @desc Generate a base64 encoded login response
100
+ * @param {object} requestInfo corresponding request, used to obtain the id
101
+ * @param {object} entity object includes both idp and sp
102
+ * @param {object} user current logged user (e.g. req.user)
103
+ * @param {function} customTagReplacement used when developers have their own login response template
104
+ * @param {boolean} encryptThenSign whether or not to encrypt then sign first (if signing). Defaults to sign-then-encrypt
105
+ */
106
+ async function base64LoginResponse(requestInfo = {}, entity, user = {}, customTagReplacement, encryptThenSign = false) {
107
+ const idpSetting = entity.idp.entitySetting;
108
+ const spSetting = entity.sp.entitySetting;
109
+ const id = idpSetting.generateID();
110
+ const metadata = {
111
+ idp: entity.idp.entityMeta,
112
+ sp: entity.sp.entityMeta,
113
+ };
114
+ const nameIDFormat = idpSetting.nameIDFormat;
115
+ const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
116
+ if (metadata && metadata.idp && metadata.sp) {
117
+ const base = metadata.sp.getAssertionConsumerService(binding.post);
118
+ let rawSamlResponse;
119
+ const nowTime = new Date();
120
+ const spEntityID = metadata.sp.getEntityID();
121
+ const fiveMinutesLaterTime = new Date(nowTime.getTime());
122
+ fiveMinutesLaterTime.setMinutes(fiveMinutesLaterTime.getMinutes() + 5);
123
+ const fiveMinutesLater = fiveMinutesLaterTime.toISOString();
124
+ const now = nowTime.toISOString();
125
+ const acl = metadata.sp.getAssertionConsumerService(binding.post);
126
+ const tvalue = {
127
+ ID: id,
128
+ AssertionID: idpSetting.generateID(),
129
+ Destination: base,
130
+ Audience: spEntityID,
131
+ EntityID: spEntityID,
132
+ SubjectRecipient: acl,
133
+ Issuer: metadata.idp.getEntityID(),
134
+ IssueInstant: now,
135
+ AssertionConsumerServiceURL: acl,
136
+ StatusCode: urn_js_1.StatusCode.Success,
137
+ // can be customized
138
+ ConditionsNotBefore: now,
139
+ ConditionsNotOnOrAfter: fiveMinutesLater,
140
+ SubjectConfirmationDataNotOnOrAfter: fiveMinutesLater,
141
+ NameIDFormat: selectedNameIDFormat,
142
+ NameID: user.email || '',
143
+ InResponseTo: (0, utility_js_1.get)(requestInfo, 'extract.request.id', ''),
144
+ AuthnStatement: '',
145
+ AttributeStatement: '',
146
+ };
147
+ if (idpSetting.loginResponseTemplate && customTagReplacement) {
148
+ const template = customTagReplacement(idpSetting.loginResponseTemplate.context);
149
+ rawSamlResponse = (0, utility_js_1.get)(template, 'context', null);
150
+ }
151
+ else {
152
+ if (requestInfo !== null) {
153
+ tvalue.InResponseTo = requestInfo.extract.request.id;
154
+ }
155
+ rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginResponseTemplate.context, tvalue);
156
+ }
157
+ const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm } = idpSetting;
158
+ const config = {
159
+ privateKey,
160
+ privateKeyPass,
161
+ signatureAlgorithm,
162
+ signingCert: metadata.idp.getX509Certificate('signing'),
163
+ isBase64Output: false,
164
+ };
165
+ // step: sign assertion ? -> encrypted ? -> sign message ?
166
+ if (metadata.sp.isWantAssertionsSigned()) {
167
+ // console.debug('sp wants assertion signed');
168
+ rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
169
+ ...config,
170
+ rawSamlMessage: rawSamlResponse,
171
+ transformationAlgorithms: spSetting.transformationAlgorithms,
172
+ referenceTagXPath: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']",
173
+ signatureConfig: {
174
+ prefix: 'ds',
175
+ location: { reference: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']/*[local-name(.)='Issuer']", action: 'after' },
176
+ },
177
+ });
178
+ }
179
+ // console.debug('after assertion signed', rawSamlResponse);
180
+ // SAML response must be signed sign message first, then encrypt
181
+ if (!encryptThenSign && (spSetting.wantMessageSigned || !metadata.sp.isWantAssertionsSigned())) {
182
+ // console.debug('sign then encrypt and sign entire message');
183
+ rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
184
+ ...config,
185
+ rawSamlMessage: rawSamlResponse,
186
+ isMessageSigned: true,
187
+ transformationAlgorithms: spSetting.transformationAlgorithms,
188
+ signatureConfig: spSetting.signatureConfig || {
189
+ prefix: 'ds',
190
+ location: { reference: "/*[local-name(.)='Response']/*[local-name(.)='Issuer']", action: 'after' },
191
+ },
192
+ });
193
+ console.log(rawSamlResponse);
194
+ console.log('这他妈是什么------------------');
195
+ }
196
+ // console.debug('after message signed', rawSamlResponse);
197
+ if (idpSetting.isAssertionEncrypted) {
198
+ // console.debug('idp is configured to do encryption');
199
+ const context = await libsaml_js_1.default.encryptAssertion(entity.idp, entity.sp, rawSamlResponse);
200
+ if (encryptThenSign) {
201
+ //need to decode it
202
+ rawSamlResponse = utility_js_1.default.base64Decode(context);
203
+ }
204
+ else {
205
+ return Promise.resolve({ id, context });
206
+ }
207
+ }
208
+ //sign after encrypting
209
+ if (encryptThenSign && (spSetting.wantMessageSigned || !metadata.sp.isWantAssertionsSigned())) {
210
+ rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
211
+ ...config,
212
+ rawSamlMessage: rawSamlResponse,
213
+ isMessageSigned: true,
214
+ transformationAlgorithms: spSetting.transformationAlgorithms,
215
+ signatureConfig: spSetting.signatureConfig || {
216
+ prefix: 'ds',
217
+ location: { reference: "/*[local-name(.)='Response']/*[local-name(.)='Issuer']", action: 'after' },
218
+ },
219
+ });
220
+ }
221
+ return Promise.resolve({
222
+ id,
223
+ context: utility_js_1.default.base64Encode(rawSamlResponse),
224
+ });
225
+ }
226
+ throw new Error('ERR_GENERATE_POST_LOGIN_RESPONSE_MISSING_METADATA');
227
+ }
228
+ /**
229
+ * @desc Generate a base64 encoded logout request
230
+ * @param {object} user current logged user (e.g. req.user)
231
+ * @param {string} referenceTagXPath reference uri
232
+ * @param {object} entity object includes both idp and sp
233
+ * @param {function} customTagReplacement used when developers have their own login response template
234
+ * @return {string} base64 encoded request
235
+ */
236
+ function base64LogoutRequest(user, referenceTagXPath, entity, customTagReplacement) {
237
+ const metadata = { init: entity.init.entityMeta, target: entity.target.entityMeta };
238
+ const initSetting = entity.init.entitySetting;
239
+ const nameIDFormat = initSetting.nameIDFormat;
240
+ const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
241
+ let id = '';
242
+ if (metadata && metadata.init && metadata.target) {
243
+ let rawSamlRequest;
244
+ if (initSetting.logoutRequestTemplate && customTagReplacement) {
245
+ const template = customTagReplacement(initSetting.logoutRequestTemplate.context);
246
+ id = (0, utility_js_1.get)(template, 'id', null);
247
+ rawSamlRequest = (0, utility_js_1.get)(template, 'context', null);
248
+ }
249
+ else {
250
+ id = initSetting.generateID();
251
+ const tvalue = {
252
+ ID: id,
253
+ Destination: metadata.target.getSingleLogoutService(binding.post),
254
+ Issuer: metadata.init.getEntityID(),
255
+ IssueInstant: new Date().toISOString(),
256
+ EntityID: metadata.init.getEntityID(),
257
+ NameIDFormat: selectedNameIDFormat,
258
+ NameID: user.logoutNameID,
259
+ };
260
+ rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutRequestTemplate.context, tvalue);
261
+ }
262
+ if (entity.target.entitySetting.wantLogoutRequestSigned) {
263
+ // Need to embeded XML signature
264
+ const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm, transformationAlgorithms } = initSetting;
265
+ return {
266
+ id,
267
+ context: libsaml_js_1.default.constructSAMLSignature({
268
+ referenceTagXPath,
269
+ privateKey,
270
+ privateKeyPass,
271
+ signatureAlgorithm,
272
+ transformationAlgorithms,
273
+ rawSamlMessage: rawSamlRequest,
274
+ signingCert: metadata.init.getX509Certificate('signing'),
275
+ signatureConfig: initSetting.signatureConfig || {
276
+ prefix: 'ds',
277
+ location: { reference: "/*[local-name(.)='LogoutRequest']/*[local-name(.)='Issuer']", action: 'after' },
278
+ }
279
+ }),
280
+ };
281
+ }
282
+ return {
283
+ id,
284
+ context: utility_js_1.default.base64Encode(rawSamlRequest),
285
+ };
286
+ }
287
+ throw new Error('ERR_GENERATE_POST_LOGOUT_REQUEST_MISSING_METADATA');
288
+ }
289
+ /**
290
+ * @desc Generate a base64 encoded logout response
291
+ * @param {object} requestInfo corresponding request, used to obtain the id
292
+ * @param {string} referenceTagXPath reference uri
293
+ * @param {object} entity object includes both idp and sp
294
+ * @param {function} customTagReplacement used when developers have their own login response template
295
+ */
296
+ function base64LogoutResponse(requestInfo, entity, customTagReplacement) {
297
+ const metadata = {
298
+ init: entity.init.entityMeta,
299
+ target: entity.target.entityMeta,
300
+ };
301
+ let id = '';
302
+ const initSetting = entity.init.entitySetting;
303
+ if (metadata && metadata.init && metadata.target) {
304
+ let rawSamlResponse;
305
+ if (initSetting.logoutResponseTemplate) {
306
+ const template = customTagReplacement(initSetting.logoutResponseTemplate.context);
307
+ id = template.id;
308
+ rawSamlResponse = template.context;
309
+ }
310
+ else {
311
+ id = initSetting.generateID();
312
+ const tvalue = {
313
+ ID: id,
314
+ Destination: metadata.target.getSingleLogoutService(binding.post),
315
+ EntityID: metadata.init.getEntityID(),
316
+ Issuer: metadata.init.getEntityID(),
317
+ IssueInstant: new Date().toISOString(),
318
+ StatusCode: urn_js_1.StatusCode.Success,
319
+ InResponseTo: (0, utility_js_1.get)(requestInfo, 'extract.request.id', null)
320
+ };
321
+ rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutResponseTemplate.context, tvalue);
322
+ }
323
+ if (entity.target.entitySetting.wantLogoutResponseSigned) {
324
+ const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm, transformationAlgorithms } = initSetting;
325
+ return {
326
+ id,
327
+ context: libsaml_js_1.default.constructSAMLSignature({
328
+ isMessageSigned: true,
329
+ transformationAlgorithms: transformationAlgorithms,
330
+ privateKey,
331
+ privateKeyPass,
332
+ signatureAlgorithm,
333
+ rawSamlMessage: rawSamlResponse,
334
+ signingCert: metadata.init.getX509Certificate('signing'),
335
+ signatureConfig: {
336
+ prefix: 'ds',
337
+ location: {
338
+ reference: "/*[local-name(.)='LogoutResponse']/*[local-name(.)='Issuer']",
339
+ action: 'after'
340
+ }
341
+ }
342
+ }),
343
+ };
344
+ }
345
+ return {
346
+ id,
347
+ context: utility_js_1.default.base64Encode(rawSamlResponse),
348
+ };
349
+ }
350
+ throw new Error('ERR_GENERATE_POST_LOGOUT_RESPONSE_MISSING_METADATA');
351
+ }
352
+ const postBinding = {
353
+ base64LoginRequest,
354
+ base64LoginResponse,
355
+ base64LogoutRequest,
356
+ base64LogoutResponse,
357
+ };
358
+ exports.default = postBinding;
369
359
  //# sourceMappingURL=binding-post.js.map