|
| 1 | +import { SamlConfig, SamlResponseData } from '../types'; |
| 2 | +import { SamlValidationError, SamlValidationErrorType } from './types'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Service for SAML SSO operations |
| 6 | + */ |
| 7 | +export default class SamlService { |
| 8 | + /** |
| 9 | + * Generate SAML AuthnRequest |
| 10 | + * |
| 11 | + * @param workspaceId - workspace ID |
| 12 | + * @param acsUrl - Assertion Consumer Service URL |
| 13 | + * @param relayState - relay state to pass through |
| 14 | + * @param samlConfig - SAML configuration |
| 15 | + * @returns AuthnRequest ID and encoded SAML request |
| 16 | + */ |
| 17 | + public async generateAuthnRequest( |
| 18 | + workspaceId: string, |
| 19 | + acsUrl: string, |
| 20 | + relayState: string, |
| 21 | + samlConfig: SamlConfig |
| 22 | + ): Promise<{ requestId: string; encodedRequest: string }> { |
| 23 | + /** |
| 24 | + * @todo Implement using @node-saml/node-saml |
| 25 | + * |
| 26 | + * This method should: |
| 27 | + * 1. Generate unique AuthnRequest ID |
| 28 | + * 2. Create SAML AuthnRequest XML |
| 29 | + * 3. Encode it as base64 |
| 30 | + * 4. Return both requestId and encoded request |
| 31 | + */ |
| 32 | + throw new Error('Not implemented'); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Validate and parse SAML Response |
| 37 | + * |
| 38 | + * @param samlResponse - base64-encoded SAML Response |
| 39 | + * @param workspaceId - workspace ID |
| 40 | + * @param acsUrl - expected Assertion Consumer Service URL |
| 41 | + * @param samlConfig - SAML configuration |
| 42 | + * @returns parsed SAML response data |
| 43 | + */ |
| 44 | + public async validateAndParseResponse( |
| 45 | + samlResponse: string, |
| 46 | + workspaceId: string, |
| 47 | + acsUrl: string, |
| 48 | + samlConfig: SamlConfig |
| 49 | + ): Promise<SamlResponseData> { |
| 50 | + /** |
| 51 | + * @todo Implement using @node-saml/node-saml |
| 52 | + * |
| 53 | + * This method should: |
| 54 | + * 1. Decode base64 SAML Response |
| 55 | + * 2. Validate XML signature using x509Cert |
| 56 | + * 3. Validate Audience (should match SSO_SP_ENTITY_ID) |
| 57 | + * 4. Validate Recipient (should match acsUrl) |
| 58 | + * 5. Validate InResponseTo (should match saved AuthnRequest ID) |
| 59 | + * 6. Validate time conditions (NotBefore, NotOnOrAfter) |
| 60 | + * 7. Extract NameID |
| 61 | + * 8. Extract email using attributeMapping |
| 62 | + * 9. Extract name using attributeMapping (if available) |
| 63 | + * 10. Return parsed data |
| 64 | + */ |
| 65 | + throw new Error('Not implemented'); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Validate Audience value |
| 70 | + * |
| 71 | + * @param audience - audience value from SAML Assertion |
| 72 | + * @returns true if audience matches SSO_SP_ENTITY_ID |
| 73 | + */ |
| 74 | + public validateAudience(audience: string): boolean { |
| 75 | + const spEntityId = process.env.SSO_SP_ENTITY_ID; |
| 76 | + |
| 77 | + if (!spEntityId) { |
| 78 | + throw new Error('SSO_SP_ENTITY_ID environment variable is not set'); |
| 79 | + } |
| 80 | + |
| 81 | + return audience === spEntityId; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Validate Recipient value |
| 86 | + * |
| 87 | + * @param recipient - recipient URL from SAML Assertion |
| 88 | + * @param expectedAcsUrl - expected ACS URL |
| 89 | + * @returns true if recipient matches expected ACS URL |
| 90 | + */ |
| 91 | + public validateRecipient(recipient: string, expectedAcsUrl: string): boolean { |
| 92 | + return recipient === expectedAcsUrl; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Validate time conditions (NotBefore and NotOnOrAfter) |
| 97 | + * |
| 98 | + * @param notBefore - NotBefore timestamp |
| 99 | + * @param notOnOrAfter - NotOnOrAfter timestamp |
| 100 | + * @param clockSkew - allowed clock skew in milliseconds (default: 2 minutes) |
| 101 | + * @returns true if assertion is valid at current time |
| 102 | + */ |
| 103 | + public validateTimeConditions( |
| 104 | + notBefore: Date, |
| 105 | + notOnOrAfter: Date, |
| 106 | + clockSkew: number = 2 * 60 * 1000 |
| 107 | + ): boolean { |
| 108 | + const now = Date.now(); |
| 109 | + const notBeforeTime = notBefore.getTime() - clockSkew; |
| 110 | + const notOnOrAfterTime = notOnOrAfter.getTime() + clockSkew; |
| 111 | + |
| 112 | + return now >= notBeforeTime && now < notOnOrAfterTime; |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments