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,333 +1,334 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- /**
40
- * @file binding-redirect.ts
41
- * @author tngan
42
- * @desc Binding-level API, declare the functions using Redirect binding
43
- */
44
- const utility_js_1 = __importStar(require("./utility.js"));
45
- const libsaml_js_1 = __importDefault(require("./libsaml.js"));
46
- const url = __importStar(require("url"));
47
- const urn_js_1 = require("./urn.js");
48
- const binding = urn_js_1.wording.binding;
49
- const urlParams = urn_js_1.wording.urlParams;
50
- /**
51
- * @private
52
- * @desc Helper of generating URL param/value pair
53
- * @param {string} param key
54
- * @param {string} value value of key
55
- * @param {boolean} first determine whether the param is the starting one in order to add query header '?'
56
- * @return {string}
57
- */
58
- function pvPair(param, value, first) {
59
- return (first === true ? '?' : '&') + param + '=' + value;
60
- }
61
- /**
62
- * @private
63
- * @desc Refractored part of URL generation for login/logout request
64
- * @param {string} type
65
- * @param {boolean} isSigned
66
- * @param {string} rawSamlRequest
67
- * @param {object} entitySetting
68
- * @return {string}
69
- */
70
- function buildRedirectURL(opts) {
71
- const { baseUrl, type, isSigned, context, entitySetting, } = opts;
72
- let { relayState = '' } = opts;
73
- const noParams = (url.parse(baseUrl).query || []).length === 0;
74
- const queryParam = libsaml_js_1.default.getQueryParamByType(type);
75
- // In general, this xmlstring is required to do deflate -> base64 -> urlencode
76
- const samlRequest = encodeURIComponent(utility_js_1.default.base64Encode(utility_js_1.default.deflateString(context)));
77
- if (relayState !== '') {
78
- relayState = pvPair(urlParams.relayState, encodeURIComponent(relayState));
79
- }
80
- if (isSigned) {
81
- const sigAlg = pvPair(urlParams.sigAlg, encodeURIComponent(entitySetting.requestSignatureAlgorithm));
82
- const octetString = samlRequest + relayState + sigAlg;
83
- return baseUrl
84
- + pvPair(queryParam, octetString, noParams)
85
- + pvPair(urlParams.signature, encodeURIComponent(libsaml_js_1.default.constructMessageSignature(queryParam + '=' + octetString, entitySetting.privateKey, entitySetting.privateKeyPass, undefined, entitySetting.requestSignatureAlgorithm).toString()));
86
- }
87
- return baseUrl + pvPair(queryParam, samlRequest + relayState, noParams);
88
- }
89
- /**
90
- * @desc Redirect URL for login request
91
- * @param {object} entity object includes both idp and sp
92
- * @param {function} customTagReplacement used when developers have their own login response template
93
- * @return {string} redirect URL
94
- */
95
- function loginRequestRedirectURL(entity, customTagReplacement) {
96
- const metadata = { idp: entity.idp.entityMeta, sp: entity.sp.entityMeta };
97
- const spSetting = entity.sp.entitySetting;
98
- let id = '';
99
- if (metadata && metadata.idp && metadata.sp) {
100
- const base = metadata.idp.getSingleSignOnService(binding.redirect);
101
- let rawSamlRequest;
102
- if (spSetting.loginRequestTemplate && customTagReplacement) {
103
- const info = customTagReplacement(spSetting.loginRequestTemplate);
104
- id = (0, utility_js_1.get)(info, 'id', null);
105
- rawSamlRequest = (0, utility_js_1.get)(info, 'context', null);
106
- }
107
- else {
108
- const nameIDFormat = spSetting.nameIDFormat;
109
- const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
110
- id = spSetting.generateID();
111
- rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginRequestTemplate.context, {
112
- ID: id,
113
- Destination: base,
114
- Issuer: metadata.sp.getEntityID(),
115
- IssueInstant: new Date().toISOString(),
116
- NameIDFormat: selectedNameIDFormat,
117
- AssertionConsumerServiceURL: metadata.sp.getAssertionConsumerService(binding.post),
118
- EntityID: metadata.sp.getEntityID(),
119
- AllowCreate: spSetting.allowCreate,
120
- });
121
- }
122
- return {
123
- id,
124
- context: buildRedirectURL({
125
- context: rawSamlRequest,
126
- type: urlParams.samlRequest,
127
- isSigned: metadata.sp.isAuthnRequestSigned(),
128
- entitySetting: spSetting,
129
- baseUrl: base,
130
- relayState: spSetting.relayState,
131
- }),
132
- };
133
- }
134
- throw new Error('ERR_GENERATE_REDIRECT_LOGIN_REQUEST_MISSING_METADATA');
135
- }
136
- /**
137
- * @desc Redirect URL for login response
138
- * @param {object} requestInfo corresponding request, used to obtain the id
139
- * @param {object} entity object includes both idp and sp
140
- * @param {object} user current logged user (e.g. req.user)
141
- * @param {String} relayState the relaystate sent by sp corresponding request
142
- * @param {function} customTagReplacement used when developers have their own login response template
143
- */
144
- function loginResponseRedirectURL(requestInfo, entity, user = {}, relayState, customTagReplacement) {
145
- const idpSetting = entity.idp.entitySetting;
146
- const spSetting = entity.sp.entitySetting;
147
- const metadata = {
148
- idp: entity.idp.entityMeta,
149
- sp: entity.sp.entityMeta,
150
- };
151
- let id = idpSetting.generateID();
152
- if (metadata && metadata.idp && metadata.sp) {
153
- const base = metadata.sp.getAssertionConsumerService(binding.redirect);
154
- let rawSamlResponse;
155
- //
156
- const nameIDFormat = idpSetting.nameIDFormat;
157
- const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
158
- const nowTime = new Date();
159
- // Five minutes later : nowtime + 5 * 60 * 1000 (in milliseconds)
160
- const fiveMinutesLaterTime = new Date(nowTime.getTime() + 300_000);
161
- const tvalue = {
162
- ID: id,
163
- AssertionID: idpSetting.generateID(),
164
- Destination: base,
165
- SubjectRecipient: base,
166
- Issuer: metadata.idp.getEntityID(),
167
- Audience: metadata.sp.getEntityID(),
168
- EntityID: metadata.sp.getEntityID(),
169
- IssueInstant: nowTime.toISOString(),
170
- AssertionConsumerServiceURL: base,
171
- StatusCode: urn_js_1.namespace.statusCode.success,
172
- // can be customized
173
- ConditionsNotBefore: nowTime.toISOString(),
174
- ConditionsNotOnOrAfter: fiveMinutesLaterTime.toISOString(),
175
- SubjectConfirmationDataNotOnOrAfter: fiveMinutesLaterTime.toISOString(),
176
- NameIDFormat: selectedNameIDFormat,
177
- NameID: user.email || '',
178
- InResponseTo: (0, utility_js_1.get)(requestInfo, 'extract.request.id', ''),
179
- AuthnStatement: '',
180
- AttributeStatement: '',
181
- };
182
- if (idpSetting.loginResponseTemplate && customTagReplacement) {
183
- const template = customTagReplacement(idpSetting.loginResponseTemplate.context);
184
- id = (0, utility_js_1.get)(template, 'id', null);
185
- rawSamlResponse = (0, utility_js_1.get)(template, 'context', null);
186
- }
187
- else {
188
- if (requestInfo !== null) {
189
- tvalue.InResponseTo = requestInfo.extract.request.id;
190
- }
191
- rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginResponseTemplate.context, tvalue);
192
- }
193
- const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm } = idpSetting;
194
- const config = {
195
- privateKey,
196
- privateKeyPass,
197
- signatureAlgorithm,
198
- signingCert: metadata.idp.getX509Certificate('signing'),
199
- isBase64Output: false,
200
- };
201
- // step: sign assertion ? -> encrypted ? -> sign message ?
202
- if (metadata.sp.isWantAssertionsSigned()) {
203
- rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
204
- ...config,
205
- rawSamlMessage: rawSamlResponse,
206
- transformationAlgorithms: spSetting.transformationAlgorithms,
207
- referenceTagXPath: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']",
208
- signatureConfig: {
209
- prefix: 'ds',
210
- location: { reference: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']/*[local-name(.)='Issuer']", action: 'after' },
211
- },
212
- });
213
- }
214
- // Like in post binding, SAML response is always signed
215
- return {
216
- id,
217
- context: buildRedirectURL({
218
- baseUrl: base,
219
- type: urlParams.samlResponse,
220
- isSigned: true,
221
- context: rawSamlResponse,
222
- entitySetting: idpSetting,
223
- relayState,
224
- }),
225
- };
226
- }
227
- throw new Error('ERR_GENERATE_REDIRECT_LOGIN_RESPONSE_MISSING_METADATA');
228
- }
229
- /**
230
- * @desc Redirect URL for logout request
231
- * @param {object} user current logged user (e.g. req.user)
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} redirect URL
235
- */
236
- function logoutRequestRedirectURL(user, entity, relayState, customTagReplacement) {
237
- const metadata = { init: entity.init.entityMeta, target: entity.target.entityMeta };
238
- const initSetting = entity.init.entitySetting;
239
- let id = initSetting.generateID();
240
- const nameIDFormat = initSetting.nameIDFormat;
241
- const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
242
- if (metadata && metadata.init && metadata.target) {
243
- const base = metadata.target.getSingleLogoutService(binding.redirect);
244
- let rawSamlRequest = '';
245
- const requiredTags = {
246
- ID: id,
247
- Destination: base,
248
- EntityID: metadata.init.getEntityID(),
249
- Issuer: metadata.init.getEntityID(),
250
- IssueInstant: new Date().toISOString(),
251
- NameIDFormat: selectedNameIDFormat,
252
- NameID: user.logoutNameID,
253
- SessionIndex: user.sessionIndex,
254
- };
255
- if (initSetting.logoutRequestTemplate && customTagReplacement) {
256
- const info = customTagReplacement(initSetting.logoutRequestTemplate, requiredTags);
257
- id = (0, utility_js_1.get)(info, 'id', null);
258
- rawSamlRequest = (0, utility_js_1.get)(info, 'context', null);
259
- }
260
- else {
261
- rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutRequestTemplate.context, requiredTags);
262
- }
263
- return {
264
- id,
265
- context: buildRedirectURL({
266
- context: rawSamlRequest,
267
- relayState,
268
- type: urlParams.logoutRequest,
269
- isSigned: entity.target.entitySetting.wantLogoutRequestSigned,
270
- entitySetting: initSetting,
271
- baseUrl: base,
272
- }),
273
- };
274
- }
275
- throw new Error('ERR_GENERATE_REDIRECT_LOGOUT_REQUEST_MISSING_METADATA');
276
- }
277
- /**
278
- * @desc Redirect URL for logout response
279
- * @param {object} requescorresponding request, used to obtain the id
280
- * @param {object} entity object includes both idp and sp
281
- * @param {function} customTagReplacement used when developers have their own login response template
282
- */
283
- function logoutResponseRedirectURL(requestInfo, entity, relayState, customTagReplacement) {
284
- const metadata = {
285
- init: entity.init.entityMeta,
286
- target: entity.target.entityMeta,
287
- };
288
- const initSetting = entity.init.entitySetting;
289
- let id = initSetting.generateID();
290
- if (metadata && metadata.init && metadata.target) {
291
- const base = metadata.target.getSingleLogoutService(binding.redirect);
292
- let rawSamlResponse;
293
- if (initSetting.logoutResponseTemplate && customTagReplacement) {
294
- const template = customTagReplacement(initSetting.logoutResponseTemplate);
295
- id = (0, utility_js_1.get)(template, 'id', null);
296
- rawSamlResponse = (0, utility_js_1.get)(template, 'context', null);
297
- }
298
- else {
299
- const tvalue = {
300
- ID: id,
301
- Destination: base,
302
- Issuer: metadata.init.getEntityID(),
303
- EntityID: metadata.init.getEntityID(),
304
- IssueInstant: new Date().toISOString(),
305
- StatusCode: urn_js_1.namespace.statusCode.success,
306
- };
307
- if (requestInfo && requestInfo.extract && requestInfo.extract.request) {
308
- tvalue.InResponseTo = requestInfo.extract.request.id;
309
- }
310
- rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutResponseTemplate.context, tvalue);
311
- }
312
- return {
313
- id,
314
- context: buildRedirectURL({
315
- baseUrl: base,
316
- type: urlParams.logoutResponse,
317
- isSigned: entity.target.entitySetting.wantLogoutResponseSigned,
318
- context: rawSamlResponse,
319
- entitySetting: initSetting,
320
- relayState,
321
- }),
322
- };
323
- }
324
- throw new Error('ERR_GENERATE_REDIRECT_LOGOUT_RESPONSE_MISSING_METADATA');
325
- }
326
- const redirectBinding = {
327
- loginRequestRedirectURL,
328
- loginResponseRedirectURL,
329
- logoutRequestRedirectURL,
330
- logoutResponseRedirectURL,
331
- };
332
- exports.default = redirectBinding;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ /**
30
+ * @file binding-redirect.ts
31
+ * @author tngan
32
+ * @desc Binding-level API, declare the functions using Redirect binding
33
+ */
34
+ const utility_js_1 = __importStar(require("./utility.js"));
35
+ const libsaml_js_1 = __importDefault(require("./libsaml.js"));
36
+ const urn_js_1 = require("./urn.js");
37
+ const binding = urn_js_1.wording.binding;
38
+ const urlParams = urn_js_1.wording.urlParams;
39
+ /**
40
+ * @private
41
+ * @desc Helper of generating URL param/value pair
42
+ * @param {string} param key
43
+ * @param {string} value value of key
44
+ * @param {boolean} first determine whether the param is the starting one in order to add query header '?'
45
+ * @return {string}
46
+ */
47
+ function pvPair(param, value, first) {
48
+ return (first === true ? '?' : '&') + param + '=' + value;
49
+ }
50
+ /**
51
+ * @private
52
+ * @desc Refractored part of URL generation for login/logout request
53
+ * @param {string} type
54
+ * @param {boolean} isSigned
55
+ * @param {string} rawSamlRequest
56
+ * @param {object} entitySetting
57
+ * @return {string}
58
+ */
59
+ function buildRedirectURL(opts) {
60
+ const { baseUrl, type, isSigned, context, entitySetting, } = opts;
61
+ let { relayState = '' } = opts;
62
+ let noParams = true;
63
+ try {
64
+ noParams = new URL(baseUrl)?.searchParams?.size === 0;
65
+ }
66
+ catch {
67
+ noParams = true;
68
+ }
69
+ const queryParam = libsaml_js_1.default.getQueryParamByType(type);
70
+ // In general, this xmlstring is required to do deflate -> base64 -> urlencode
71
+ const samlRequest = encodeURIComponent(utility_js_1.default.base64Encode(utility_js_1.default.deflateString(context)));
72
+ if (relayState !== '') {
73
+ relayState = pvPair(urlParams.relayState, encodeURIComponent(relayState));
74
+ }
75
+ if (isSigned) {
76
+ const sigAlg = pvPair(urlParams.sigAlg, encodeURIComponent(entitySetting.requestSignatureAlgorithm));
77
+ const octetString = samlRequest + relayState + sigAlg;
78
+ return baseUrl
79
+ + pvPair(queryParam, octetString, noParams)
80
+ + pvPair(urlParams.signature, encodeURIComponent(libsaml_js_1.default.constructMessageSignature(queryParam + '=' + octetString, entitySetting.privateKey, entitySetting.privateKeyPass, undefined, entitySetting.requestSignatureAlgorithm).toString()));
81
+ }
82
+ return baseUrl + pvPair(queryParam, samlRequest + relayState, noParams);
83
+ }
84
+ /**
85
+ * @desc Redirect URL for login request
86
+ * @param {object} entity object includes both idp and sp
87
+ * @param {function} customTagReplacement used when developers have their own login response template
88
+ * @return {string} redirect URL
89
+ */
90
+ function loginRequestRedirectURL(entity, customTagReplacement) {
91
+ const metadata = { idp: entity.idp.entityMeta, sp: entity.sp.entityMeta };
92
+ const spSetting = entity.sp.entitySetting;
93
+ let id = '';
94
+ if (metadata && metadata.idp && metadata.sp) {
95
+ const base = metadata.idp.getSingleSignOnService(binding.redirect);
96
+ let rawSamlRequest;
97
+ if (spSetting.loginRequestTemplate && customTagReplacement) {
98
+ const info = customTagReplacement(spSetting.loginRequestTemplate);
99
+ id = (0, utility_js_1.get)(info, 'id', null);
100
+ rawSamlRequest = (0, utility_js_1.get)(info, 'context', null);
101
+ }
102
+ else {
103
+ const nameIDFormat = spSetting.nameIDFormat;
104
+ const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
105
+ id = spSetting.generateID();
106
+ rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginRequestTemplate.context, {
107
+ ID: id,
108
+ Destination: base,
109
+ Issuer: metadata.sp.getEntityID(),
110
+ IssueInstant: new Date().toISOString(),
111
+ NameIDFormat: selectedNameIDFormat,
112
+ AssertionConsumerServiceURL: metadata.sp.getAssertionConsumerService(binding.post),
113
+ EntityID: metadata.sp.getEntityID(),
114
+ AllowCreate: spSetting.allowCreate,
115
+ });
116
+ }
117
+ return {
118
+ id,
119
+ context: buildRedirectURL({
120
+ context: rawSamlRequest,
121
+ type: urlParams.samlRequest,
122
+ isSigned: metadata.sp.isAuthnRequestSigned(),
123
+ entitySetting: spSetting,
124
+ baseUrl: base,
125
+ relayState: spSetting.relayState,
126
+ }),
127
+ };
128
+ }
129
+ throw new Error('ERR_GENERATE_REDIRECT_LOGIN_REQUEST_MISSING_METADATA');
130
+ }
131
+ /**
132
+ * @desc Redirect URL for login response
133
+ * @param {object} requestInfo corresponding request, used to obtain the id
134
+ * @param {object} entity object includes both idp and sp
135
+ * @param {object} user current logged user (e.g. req.user)
136
+ * @param {String} relayState the relaystate sent by sp corresponding request
137
+ * @param {function} customTagReplacement used when developers have their own login response template
138
+ */
139
+ function loginResponseRedirectURL(requestInfo, entity, user = {}, relayState, customTagReplacement) {
140
+ const idpSetting = entity.idp.entitySetting;
141
+ const spSetting = entity.sp.entitySetting;
142
+ const metadata = {
143
+ idp: entity.idp.entityMeta,
144
+ sp: entity.sp.entityMeta,
145
+ };
146
+ let id = idpSetting.generateID();
147
+ if (metadata && metadata.idp && metadata.sp) {
148
+ const base = metadata.sp.getAssertionConsumerService(binding.redirect) ?? 'https://signin.volcengine.com/saml/sso';
149
+ if (!base) {
150
+ throw new Error('dont have a base url');
151
+ }
152
+ let rawSamlResponse;
153
+ //
154
+ const nameIDFormat = idpSetting.nameIDFormat;
155
+ const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
156
+ const nowTime = new Date();
157
+ // Five minutes later : nowtime + 5 * 60 * 1000 (in milliseconds)
158
+ const fiveMinutesLaterTime = new Date(nowTime.getTime() + 300_000);
159
+ const tvalue = {
160
+ ID: id,
161
+ AssertionID: idpSetting.generateID(),
162
+ Destination: base,
163
+ SubjectRecipient: base,
164
+ Issuer: metadata.idp.getEntityID(),
165
+ Audience: metadata.sp.getEntityID(),
166
+ EntityID: metadata.sp.getEntityID(),
167
+ IssueInstant: nowTime.toISOString(),
168
+ AssertionConsumerServiceURL: base,
169
+ StatusCode: urn_js_1.namespace.statusCode.success,
170
+ // can be customized
171
+ ConditionsNotBefore: nowTime.toISOString(),
172
+ ConditionsNotOnOrAfter: fiveMinutesLaterTime.toISOString(),
173
+ SubjectConfirmationDataNotOnOrAfter: fiveMinutesLaterTime.toISOString(),
174
+ NameIDFormat: selectedNameIDFormat,
175
+ NameID: user.email || '',
176
+ InResponseTo: (0, utility_js_1.get)(requestInfo, 'extract.request.id', ''),
177
+ AuthnStatement: '',
178
+ AttributeStatement: '',
179
+ };
180
+ if (idpSetting.loginResponseTemplate && customTagReplacement) {
181
+ const template = customTagReplacement(idpSetting.loginResponseTemplate.context);
182
+ id = (0, utility_js_1.get)(template, 'id', null);
183
+ rawSamlResponse = (0, utility_js_1.get)(template, 'context', null);
184
+ }
185
+ else {
186
+ if (requestInfo !== null) {
187
+ tvalue.InResponseTo = requestInfo.extract.request.id;
188
+ }
189
+ rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLoginResponseTemplate.context, tvalue);
190
+ }
191
+ const { privateKey, privateKeyPass, requestSignatureAlgorithm: signatureAlgorithm } = idpSetting;
192
+ const config = {
193
+ privateKey,
194
+ privateKeyPass,
195
+ signatureAlgorithm,
196
+ signingCert: metadata.idp.getX509Certificate('signing'),
197
+ isBase64Output: false,
198
+ };
199
+ // step: sign assertion ? -> encrypted ? -> sign message ?
200
+ if (metadata.sp.isWantAssertionsSigned()) {
201
+ rawSamlResponse = libsaml_js_1.default.constructSAMLSignature({
202
+ ...config,
203
+ rawSamlMessage: rawSamlResponse,
204
+ transformationAlgorithms: spSetting.transformationAlgorithms,
205
+ referenceTagXPath: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']",
206
+ signatureConfig: {
207
+ prefix: 'ds',
208
+ location: {
209
+ reference: "/*[local-name(.)='Response']/*[local-name(.)='Assertion']/*[local-name(.)='Issuer']",
210
+ action: 'after'
211
+ },
212
+ },
213
+ });
214
+ }
215
+ // Like in post binding, SAML response is always signed
216
+ return {
217
+ id,
218
+ context: buildRedirectURL({
219
+ baseUrl: base,
220
+ type: urlParams.samlResponse,
221
+ isSigned: true,
222
+ context: rawSamlResponse,
223
+ entitySetting: idpSetting,
224
+ relayState,
225
+ }),
226
+ };
227
+ }
228
+ throw new Error('ERR_GENERATE_REDIRECT_LOGIN_RESPONSE_MISSING_METADATA');
229
+ }
230
+ /**
231
+ * @desc Redirect URL for logout request
232
+ * @param {object} user current logged user (e.g. req.user)
233
+ * @param {object} entity object includes both idp and sp
234
+ * @param {function} customTagReplacement used when developers have their own login response template
235
+ * @return {string} redirect URL
236
+ */
237
+ function logoutRequestRedirectURL(user, entity, relayState, customTagReplacement) {
238
+ const metadata = { init: entity.init.entityMeta, target: entity.target.entityMeta };
239
+ const initSetting = entity.init.entitySetting;
240
+ let id = initSetting.generateID();
241
+ const nameIDFormat = initSetting.nameIDFormat;
242
+ const selectedNameIDFormat = Array.isArray(nameIDFormat) ? nameIDFormat[0] : nameIDFormat;
243
+ if (metadata && metadata.init && metadata.target) {
244
+ const base = metadata.target.getSingleLogoutService(binding.redirect);
245
+ let rawSamlRequest = '';
246
+ const requiredTags = {
247
+ ID: id,
248
+ Destination: base,
249
+ EntityID: metadata.init.getEntityID(),
250
+ Issuer: metadata.init.getEntityID(),
251
+ IssueInstant: new Date().toISOString(),
252
+ NameIDFormat: selectedNameIDFormat,
253
+ NameID: user.logoutNameID,
254
+ SessionIndex: user.sessionIndex,
255
+ };
256
+ if (initSetting.logoutRequestTemplate && customTagReplacement) {
257
+ const info = customTagReplacement(initSetting.logoutRequestTemplate, requiredTags);
258
+ id = (0, utility_js_1.get)(info, 'id', null);
259
+ rawSamlRequest = (0, utility_js_1.get)(info, 'context', null);
260
+ }
261
+ else {
262
+ rawSamlRequest = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutRequestTemplate.context, requiredTags);
263
+ }
264
+ return {
265
+ id,
266
+ context: buildRedirectURL({
267
+ context: rawSamlRequest,
268
+ relayState,
269
+ type: urlParams.logoutRequest,
270
+ isSigned: entity.target.entitySetting.wantLogoutRequestSigned,
271
+ entitySetting: initSetting,
272
+ baseUrl: base,
273
+ }),
274
+ };
275
+ }
276
+ throw new Error('ERR_GENERATE_REDIRECT_LOGOUT_REQUEST_MISSING_METADATA');
277
+ }
278
+ /**
279
+ * @desc Redirect URL for logout response
280
+ * @param {object} requescorresponding request, used to obtain the id
281
+ * @param {object} entity object includes both idp and sp
282
+ * @param {function} customTagReplacement used when developers have their own login response template
283
+ */
284
+ function logoutResponseRedirectURL(requestInfo, entity, relayState, customTagReplacement) {
285
+ const metadata = {
286
+ init: entity.init.entityMeta,
287
+ target: entity.target.entityMeta,
288
+ };
289
+ const initSetting = entity.init.entitySetting;
290
+ let id = initSetting.generateID();
291
+ if (metadata && metadata.init && metadata.target) {
292
+ const base = metadata.target.getSingleLogoutService(binding.redirect);
293
+ let rawSamlResponse;
294
+ if (initSetting.logoutResponseTemplate && customTagReplacement) {
295
+ const template = customTagReplacement(initSetting.logoutResponseTemplate);
296
+ id = (0, utility_js_1.get)(template, 'id', null);
297
+ rawSamlResponse = (0, utility_js_1.get)(template, 'context', null);
298
+ }
299
+ else {
300
+ const tvalue = {
301
+ ID: id,
302
+ Destination: base,
303
+ Issuer: metadata.init.getEntityID(),
304
+ EntityID: metadata.init.getEntityID(),
305
+ IssueInstant: new Date().toISOString(),
306
+ StatusCode: urn_js_1.namespace.statusCode.success,
307
+ };
308
+ if (requestInfo && requestInfo.extract && requestInfo.extract.request) {
309
+ tvalue.InResponseTo = requestInfo.extract.request.id;
310
+ }
311
+ rawSamlResponse = libsaml_js_1.default.replaceTagsByValue(libsaml_js_1.default.defaultLogoutResponseTemplate.context, tvalue);
312
+ }
313
+ return {
314
+ id,
315
+ context: buildRedirectURL({
316
+ baseUrl: base,
317
+ type: urlParams.logoutResponse,
318
+ isSigned: entity.target.entitySetting.wantLogoutResponseSigned,
319
+ context: rawSamlResponse,
320
+ entitySetting: initSetting,
321
+ relayState,
322
+ }),
323
+ };
324
+ }
325
+ throw new Error('ERR_GENERATE_REDIRECT_LOGOUT_RESPONSE_MISSING_METADATA');
326
+ }
327
+ const redirectBinding = {
328
+ loginRequestRedirectURL,
329
+ loginResponseRedirectURL,
330
+ logoutRequestRedirectURL,
331
+ logoutResponseRedirectURL,
332
+ };
333
+ exports.default = redirectBinding;
333
334
  //# sourceMappingURL=binding-redirect.js.map