samlesa 4.3.4 → 4.4.0

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.
package/README.md CHANGED
@@ -154,22 +154,25 @@ const { context: samlResponse } = await idp.createLoginResponse({
154
154
  });
155
155
  ```
156
156
 
157
- ### Artifact Binding Support
158
-
159
- ```typescript
160
- import { ServiceProvider, IdentityProvider } from 'samlesa';
161
-
162
- // Create SOAP login request (Artifact binding)
163
- const soapRequest = await sp.createLoginSoapRequest(idp, 'artifact', {
164
- inResponseTo: '_requestId',
165
- });
166
-
167
- // Parse Artifact Resolve request
168
- const artifactResult = await sp.parseLoginRequestResolve(idp, soapXml);
169
-
170
- // Resolve SAML Response by Artifact ID
171
- const responseResult = await sp.parseLoginResponseResolve(idp, artifactId, request);
172
- ```
157
+ ### Artifact Binding Support
158
+
159
+ ```typescript
160
+ import { ServiceProvider, IdentityProvider } from 'samlesa';
161
+
162
+ // Create front-channel artifact login request
163
+ const loginRequest = sp.createLoginRequest(idp, 'artifact');
164
+
165
+ // Parse ArtifactResolve request on the SP artifact resolution endpoint
166
+ const artifactResolve = await sp.parseArtifactResolveRequest(idp, soapXml);
167
+
168
+ // Create ArtifactResponse with the resolved SAML message
169
+ const artifactResponse = await sp.createArtifactResolveResponse(idp, {
170
+ inResponseTo: artifactResolve.extract.request.id,
171
+ });
172
+
173
+ // Parse artifact-based login response from the ACS endpoint
174
+ const responseResult = await sp.parseLoginResponse(idp, 'artifact', request);
175
+ ```
173
176
 
174
177
  ---
175
178
 
@@ -0,0 +1,55 @@
1
+ import * as crypto from 'node:crypto';
2
+ export const SAML2_ARTIFACT_TYPE_CODE = 0x0004;
3
+ export const SAML2_ARTIFACT_LENGTH = 44;
4
+ export function computeArtifactSourceId(entityId) {
5
+ return crypto.createHash('sha1').update(entityId).digest().subarray(0, 20);
6
+ }
7
+ export function generateArtifactId(entityId, endpointIndex = 0) {
8
+ if (!entityId || typeof entityId !== 'string') {
9
+ throw new Error('ERR_INVALID_ARTIFACT_ENTITY_ID');
10
+ }
11
+ if (!Number.isInteger(endpointIndex) || endpointIndex < 0 || endpointIndex > 0xffff) {
12
+ throw new Error('ERR_INVALID_ARTIFACT_ENDPOINT_INDEX');
13
+ }
14
+ const typeCode = Buffer.alloc(2);
15
+ typeCode.writeUInt16BE(SAML2_ARTIFACT_TYPE_CODE, 0);
16
+ const endpointIndexBuffer = Buffer.alloc(2);
17
+ endpointIndexBuffer.writeUInt16BE(endpointIndex, 0);
18
+ const artifact = Buffer.concat([
19
+ typeCode,
20
+ endpointIndexBuffer,
21
+ computeArtifactSourceId(entityId),
22
+ crypto.randomBytes(20),
23
+ ]);
24
+ return artifact.toString('base64');
25
+ }
26
+ export function parseArtifact(artifact) {
27
+ if (typeof artifact !== 'string' || artifact.trim() === '') {
28
+ throw new Error('ERR_INVALID_ARTIFACT');
29
+ }
30
+ const decoded = Buffer.from(artifact, 'base64');
31
+ if (decoded.length !== SAML2_ARTIFACT_LENGTH) {
32
+ throw new Error('ERR_INVALID_ARTIFACT_LENGTH');
33
+ }
34
+ const typeCode = decoded.readUInt16BE(0);
35
+ if (typeCode !== SAML2_ARTIFACT_TYPE_CODE) {
36
+ throw new Error('ERR_UNSUPPORTED_ARTIFACT_TYPE');
37
+ }
38
+ return {
39
+ artifact,
40
+ typeCode,
41
+ endpointIndex: decoded.readUInt16BE(2),
42
+ sourceId: decoded.subarray(4, 24).toString('hex'),
43
+ messageHandle: decoded.subarray(24, 44).toString('hex'),
44
+ };
45
+ }
46
+ export function validateArtifact(artifact, expectedEntityId) {
47
+ const parsed = parseArtifact(artifact);
48
+ if (expectedEntityId) {
49
+ const expectedSourceId = computeArtifactSourceId(expectedEntityId).toString('hex');
50
+ if (parsed.sourceId !== expectedSourceId) {
51
+ throw new Error('ERR_UNMATCH_ARTIFACT_SOURCE');
52
+ }
53
+ }
54
+ return parsed;
55
+ }