|
| 1 | +import { SAML, SamlConfig as NodeSamlConfig, Profile } from '@node-saml/node-saml'; |
1 | 2 | import { SamlConfig, SamlResponseData } from '../types'; |
2 | 3 | import { SamlValidationError, SamlValidationErrorType } from './types'; |
3 | | -import { validateAudience, validateRecipient, validateTimeConditions } from './utils'; |
| 4 | +import { extractAttribute } from './utils'; |
4 | 5 |
|
5 | 6 | /** |
6 | 7 | * Service for SAML SSO operations |
@@ -40,31 +41,172 @@ export default class SamlService { |
40 | 41 | * @param workspaceId - workspace ID |
41 | 42 | * @param acsUrl - expected Assertion Consumer Service URL |
42 | 43 | * @param samlConfig - SAML configuration |
| 44 | + * @param expectedRequestId - optional expected InResponseTo value (if provided, validates that response matches) |
43 | 45 | * @returns parsed SAML response data |
| 46 | + * @throws SamlValidationError if validation fails |
44 | 47 | */ |
45 | 48 | public async validateAndParseResponse( |
46 | 49 | samlResponse: string, |
47 | 50 | workspaceId: string, |
48 | 51 | acsUrl: string, |
49 | | - samlConfig: SamlConfig |
| 52 | + samlConfig: SamlConfig, |
| 53 | + expectedRequestId?: string |
50 | 54 | ): Promise<SamlResponseData> { |
| 55 | + const saml = this.createSamlInstance(acsUrl, samlConfig); |
| 56 | + |
| 57 | + let profile: Profile; |
| 58 | + |
| 59 | + try { |
| 60 | + /** |
| 61 | + * node-saml validates: |
| 62 | + * - XML signature using x509Cert |
| 63 | + * - Audience (via idpIssuer option) |
| 64 | + * - Time conditions (NotBefore, NotOnOrAfter with clock skew) |
| 65 | + */ |
| 66 | + const result = await saml.validatePostResponseAsync({ |
| 67 | + SAMLResponse: samlResponse, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!result.profile) { |
| 71 | + throw new SamlValidationError( |
| 72 | + SamlValidationErrorType.INVALID_SIGNATURE, |
| 73 | + 'SAML response validation failed: no profile returned' |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + profile = result.profile; |
| 78 | + } catch (error) { |
| 79 | + const message = error instanceof Error ? error.message : 'Unknown SAML validation error'; |
| 80 | + |
| 81 | + /** |
| 82 | + * Determine specific error type based on error message |
| 83 | + */ |
| 84 | + if (message.includes('signature')) { |
| 85 | + throw new SamlValidationError( |
| 86 | + SamlValidationErrorType.INVALID_SIGNATURE, |
| 87 | + `SAML signature validation failed: ${message}` |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + if (message.includes('expired') || message.includes('NotOnOrAfter') || message.includes('NotBefore')) { |
| 92 | + throw new SamlValidationError( |
| 93 | + SamlValidationErrorType.EXPIRED_ASSERTION, |
| 94 | + `SAML assertion time validation failed: ${message}` |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + if (message.includes('audience') || message.includes('Audience')) { |
| 99 | + throw new SamlValidationError( |
| 100 | + SamlValidationErrorType.INVALID_AUDIENCE, |
| 101 | + `SAML audience validation failed: ${message}` |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Fallback for unknown error types |
| 107 | + * Note: Error classification relies on message text which may change between library versions |
| 108 | + */ |
| 109 | + throw new SamlValidationError( |
| 110 | + SamlValidationErrorType.VALIDATION_FAILED, |
| 111 | + `SAML validation failed: ${message}` |
| 112 | + ); |
| 113 | + } |
| 114 | + |
51 | 115 | /** |
52 | | - * @todo Implement using @node-saml/node-saml |
53 | | - * |
54 | | - * This method should: |
55 | | - * 1. Decode base64 SAML Response |
56 | | - * 2. Validate XML signature using x509Cert |
57 | | - * 3. Validate Audience (should match SSO_SP_ENTITY_ID) |
58 | | - * 4. Validate Recipient (should match acsUrl) |
59 | | - * 5. Validate InResponseTo (should match saved AuthnRequest ID) |
60 | | - * 6. Validate time conditions (NotBefore, NotOnOrAfter) |
61 | | - * 7. Extract NameID |
62 | | - * 8. Extract email using attributeMapping |
63 | | - * 9. Extract name using attributeMapping (if available) |
64 | | - * 10. Return parsed data |
| 116 | + * Extract NameID (Profile type defines nameID as required string) |
65 | 117 | */ |
66 | | - throw new Error('Not implemented'); |
| 118 | + const nameId = profile.nameID; |
| 119 | + |
| 120 | + if (!nameId) { |
| 121 | + throw new SamlValidationError( |
| 122 | + SamlValidationErrorType.INVALID_NAME_ID, |
| 123 | + 'SAML response does not contain NameID' |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Extract InResponseTo and validate if expectedRequestId provided |
| 129 | + * Profile uses index signature [attributeName: string]: unknown for additional properties |
| 130 | + */ |
| 131 | + const inResponseTo = profile.inResponseTo as string | undefined; |
| 132 | + |
| 133 | + if (expectedRequestId && inResponseTo !== expectedRequestId) { |
| 134 | + throw new SamlValidationError( |
| 135 | + SamlValidationErrorType.INVALID_IN_RESPONSE_TO, |
| 136 | + `InResponseTo mismatch: expected ${expectedRequestId}, got ${inResponseTo}`, |
| 137 | + { expected: expectedRequestId, received: inResponseTo } |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Extract attributes from profile |
| 143 | + * node-saml puts SAML attributes directly on the profile object via index signature |
| 144 | + */ |
| 145 | + const attributes = profile as unknown as Record<string, string | string[]>; |
| 146 | + |
| 147 | + /** |
| 148 | + * Extract email using attributeMapping |
| 149 | + */ |
| 150 | + const email = extractAttribute(attributes, samlConfig.attributeMapping.email); |
| 151 | + |
| 152 | + if (!email) { |
| 153 | + throw new SamlValidationError( |
| 154 | + SamlValidationErrorType.MISSING_EMAIL, |
| 155 | + `Email attribute not found in SAML response. Expected attribute: ${samlConfig.attributeMapping.email}`, |
| 156 | + { attributeMapping: samlConfig.attributeMapping } |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Extract name using attributeMapping (optional) |
| 162 | + */ |
| 163 | + let name: string | undefined; |
| 164 | + |
| 165 | + if (samlConfig.attributeMapping.name) { |
| 166 | + name = extractAttribute(attributes, samlConfig.attributeMapping.name); |
| 167 | + } |
| 168 | + |
| 169 | + return { |
| 170 | + nameId, |
| 171 | + email, |
| 172 | + name, |
| 173 | + inResponseTo, |
| 174 | + }; |
67 | 175 | } |
68 | 176 |
|
| 177 | + /** |
| 178 | + * Create node-saml SAML instance with given configuration |
| 179 | + * |
| 180 | + * @param acsUrl - Assertion Consumer Service URL |
| 181 | + * @param samlConfig - SAML configuration from workspace |
| 182 | + * @returns configured SAML instance |
| 183 | + */ |
| 184 | + private createSamlInstance(acsUrl: string, samlConfig: SamlConfig): SAML { |
| 185 | + const spEntityId = process.env.SSO_SP_ENTITY_ID; |
| 186 | + |
| 187 | + if (!spEntityId) { |
| 188 | + throw new Error('SSO_SP_ENTITY_ID environment variable is not set'); |
| 189 | + } |
| 190 | + |
| 191 | + const options: NodeSamlConfig = { |
| 192 | + callbackUrl: acsUrl, |
| 193 | + entryPoint: samlConfig.ssoUrl, |
| 194 | + issuer: spEntityId, |
| 195 | + idpIssuer: samlConfig.idpEntityId, |
| 196 | + idpCert: samlConfig.x509Cert, |
| 197 | + wantAssertionsSigned: true, |
| 198 | + wantAuthnResponseSigned: false, |
| 199 | + /** |
| 200 | + * Allow 2 minutes clock skew for time validation |
| 201 | + */ |
| 202 | + acceptedClockSkewMs: 2 * 60 * 1000, |
| 203 | + }; |
| 204 | + |
| 205 | + if (samlConfig.nameIdFormat) { |
| 206 | + options.identifierFormat = samlConfig.nameIdFormat; |
| 207 | + } |
| 208 | + |
| 209 | + return new SAML(options); |
| 210 | + } |
69 | 211 | } |
70 | 212 |
|
0 commit comments