Skip to content

Commit 225fdbb

Browse files
authored
fix(core): Report missing SAML attributes early with an actionable error message (#9316)
1 parent ff31749 commit 225fdbb

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/cli/src/sso/saml/saml.service.ee.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class SamlService {
359359
if (!attributes) {
360360
throw new AuthError('SAML Authentication failed. Invalid SAML response.');
361361
}
362-
if (!attributes.email && missingAttributes.length > 0) {
362+
if (missingAttributes.length > 0) {
363363
throw new AuthError(
364364
`SAML Authentication failed. Invalid SAML response (missing attributes: ${missingAttributes.join(
365365
', ',
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { mock } from 'jest-mock-extended';
2+
import type express from 'express';
3+
import { SamlService } from '@/sso/saml/saml.service.ee';
4+
import { mockInstance } from '../../../shared/mocking';
5+
import { UrlService } from '@/services/url.service';
6+
import { Logger } from '@/Logger';
7+
import type { IdentityProviderInstance, ServiceProviderInstance } from 'samlify';
8+
import * as samlHelpers from '@/sso/saml/samlHelpers';
9+
10+
describe('SamlService', () => {
11+
const logger = mockInstance(Logger);
12+
const urlService = mockInstance(UrlService);
13+
const samlService = new SamlService(logger, urlService);
14+
15+
describe('getAttributesFromLoginResponse', () => {
16+
test('throws when any attribute is missing', async () => {
17+
//
18+
// ARRANGE
19+
//
20+
jest
21+
.spyOn(samlService, 'getIdentityProviderInstance')
22+
.mockReturnValue(mock<IdentityProviderInstance>());
23+
24+
const serviceProviderInstance = mock<ServiceProviderInstance>();
25+
serviceProviderInstance.parseLoginResponse.mockResolvedValue({
26+
samlContent: '',
27+
extract: {},
28+
});
29+
jest
30+
.spyOn(samlService, 'getServiceProviderInstance')
31+
.mockReturnValue(serviceProviderInstance);
32+
33+
jest.spyOn(samlHelpers, 'getMappedSamlAttributesFromFlowResult').mockReturnValue({
34+
attributes: {} as never,
35+
missingAttributes: [
36+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress',
37+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/firstname',
38+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/lastname',
39+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn',
40+
],
41+
});
42+
43+
//
44+
// ACT & ASSERT
45+
//
46+
await expect(
47+
samlService.getAttributesFromLoginResponse(mock<express.Request>(), 'post'),
48+
).rejects.toThrowError(
49+
'SAML Authentication failed. Invalid SAML response (missing attributes: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/firstname, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/lastname, http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn).',
50+
);
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)