Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-singers-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Fix SAML Connection `attributeMapping` keys not being converted from camelCase to snake_case.
165 changes: 138 additions & 27 deletions packages/backend/src/api/__tests__/SamlConnectionApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ describe('SamlConnectionAPI', () => {
secretKey: 'deadbeef',
});

const mockSamlConnectionResponse = {
object: 'saml_connection',
id: 'samlc_123',
name: 'Test Connection',
provider: 'saml_custom',
domain: 'test.example.com',
organization_id: 'org_123',
created_at: 1672531200000,
updated_at: 1672531200000,
active: true,
sync_user_attributes: false,
allow_subdomains: false,
allow_idp_initiated: false,
idp_entity_id: 'entity_123',
idp_sso_url: 'https://idp.example.com/sso',
idp_certificate: 'cert_data',
idp_metadata_url: null,
idp_metadata: null,
attribute_mapping: {
user_id: 'userId',
email_address: 'email',
first_name: 'firstName',
last_name: 'lastName',
},
};

describe('getSamlConnectionList', () => {
it('successfully fetches SAML connections with all parameters', async () => {
const mockSamlConnectionsResponse = {
data: [
{
object: 'saml_connection',
id: 'samlc_123',
name: 'Test Connection',
provider: 'saml_custom',
domain: 'test.example.com',
organization_id: 'org_123',
created_at: 1672531200000,
updated_at: 1672531200000,
active: true,
sync_user_attributes: false,
allow_subdomains: false,
allow_idp_initiated: false,
idp_entity_id: 'entity_123',
idp_sso_url: 'https://idp.example.com/sso',
idp_certificate: 'cert_data',
idp_metadata_url: null,
idp_metadata: null,
attribute_mapping: {
user_id: 'userId',
email_address: 'email',
first_name: 'firstName',
last_name: 'lastName',
},
},
],
data: [mockSamlConnectionResponse],
total_count: 1,
};

Expand Down Expand Up @@ -73,4 +73,115 @@ describe('SamlConnectionAPI', () => {
expect(response.totalCount).toBe(1);
});
});

describe('createSamlConnection', () => {
it('successfully creates a SAML connection', async () => {
server.use(
http.post(
'https://api.clerk.test/v1/saml_connections',
validateHeaders(async ({ request }) => {
const body = await request.json();

expect(body).toEqual({
name: 'Test Connection',
provider: 'saml_custom',
domain: 'test.example.com',
attribute_mapping: {
user_id: 'userId',
email_address: 'email',
first_name: 'firstName',
last_name: 'lastName',
},
});

return HttpResponse.json(mockSamlConnectionResponse);
}),
),
);

const response = await apiClient.samlConnections.createSamlConnection({
name: 'Test Connection',
provider: 'saml_custom',
domain: 'test.example.com',
attributeMapping: {
userId: 'userId',
emailAddress: 'email',
firstName: 'firstName',
lastName: 'lastName',
},
});

expect(response.id).toBe('samlc_123');
expect(response.name).toBe('Test Connection');
expect(response.organizationId).toBe('org_123');
});
});

describe('updateSamlConnection', () => {
it('successfully updates a SAML connection', async () => {
server.use(
http.patch(
'https://api.clerk.test/v1/saml_connections/samlc_123',
validateHeaders(async ({ request }) => {
const body = await request.json();

expect(body).toEqual({
name: 'Test Connection',
provider: 'saml_custom',
domain: 'test.example.com',
organization_id: 'org_123',
idp_entity_id: 'entity_123',
idp_sso_url: 'https://idp.example.com/sso',
idp_certificate: 'cert_data',
attribute_mapping: {
user_id: 'userId2',
email_address: 'email2',
first_name: 'firstName2',
last_name: 'lastName2',
},
});

return HttpResponse.json({
...mockSamlConnectionResponse,
idp_entity_id: 'entity_123',
idp_sso_url: 'https://idp.example.com/sso',
idp_certificate: 'cert_data',
attribute_mapping: {
user_id: 'userId2',
email_address: 'email2',
first_name: 'firstName2',
last_name: 'lastName2',
},
});
}),
),
);

const response = await apiClient.samlConnections.updateSamlConnection('samlc_123', {
name: 'Test Connection',
provider: 'saml_custom',
domain: 'test.example.com',
organizationId: 'org_123',
idpEntityId: 'entity_123',
idpSsoUrl: 'https://idp.example.com/sso',
idpCertificate: 'cert_data',
attributeMapping: {
userId: 'userId2',
emailAddress: 'email2',
firstName: 'firstName2',
lastName: 'lastName2',
},
});

expect(response.id).toBe('samlc_123');
expect(response.name).toBe('Test Connection');
expect(response.organizationId).toBe('org_123');
expect(response.attributeMapping).toEqual({
userId: 'userId2',
emailAddress: 'email2',
firstName: 'firstName2',
lastName: 'lastName2',
});
});
});
});
6 changes: 6 additions & 0 deletions packages/backend/src/api/endpoints/SamlConnectionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export class SamlConnectionAPI extends AbstractAPI {
method: 'POST',
path: basePath,
bodyParams: params,
options: {
deepSnakecaseBodyParamKeys: true,
},
});
}

Expand All @@ -100,6 +103,9 @@ export class SamlConnectionAPI extends AbstractAPI {
method: 'PATCH',
path: joinPaths(basePath, samlConnectionId),
bodyParams: params,
options: {
deepSnakecaseBodyParamKeys: true,
},
Comment on lines +106 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I might use this new options param to override adding of secret key to the Authorization header for #6229

});
}
public async deleteSamlConnection(samlConnectionId: string) {
Expand Down
41 changes: 31 additions & 10 deletions packages/backend/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,41 @@ import { assertValidSecretKey } from '../util/optionsAssertions';
import { joinPaths } from '../util/path';
import { deserialize } from './resources/Deserializer';

export type ClerkBackendApiRequestOptions = {
method: 'GET' | 'POST' | 'PATCH' | 'DELETE' | 'PUT';
queryParams?: Record<string, unknown>;
headerParams?: Record<string, string>;
bodyParams?: Record<string, unknown> | Array<Record<string, unknown>>;
formData?: FormData;
} & (
type ClerkBackendApiRequestOptionsUrlOrPath =
| {
url: string;
path?: string;
}
| {
url?: string;
path: string;
};

type ClerkBackendApiRequestOptionsBodyParams =
| {
bodyParams: Record<string, unknown> | Array<Record<string, unknown>>;
options?: {
/**
* If true, snakecases the keys of the bodyParams object recursively.
* @default false
*/
deepSnakecaseBodyParamKeys?: boolean;
};
}
);
| {
bodyParams?: never;
options?: {
deepSnakecaseBodyParamKeys?: never;
};
};

export type ClerkBackendApiRequestOptions = {
method: 'GET' | 'POST' | 'PATCH' | 'DELETE' | 'PUT';
queryParams?: Record<string, unknown>;
headerParams?: Record<string, string>;
formData?: FormData;
} & ClerkBackendApiRequestOptionsUrlOrPath &
ClerkBackendApiRequestOptionsBodyParams;

export type ClerkBackendApiResponse<T> =
| {
Expand Down Expand Up @@ -78,7 +97,8 @@ export function buildRequest(options: BuildRequestOptions) {
userAgent = USER_AGENT,
skipApiVersionInUrl = false,
} = options;
const { path, method, queryParams, headerParams, bodyParams, formData } = requestOptions;
const { path, method, queryParams, headerParams, bodyParams, formData, options: opts } = requestOptions;
const { deepSnakecaseBodyParamKeys = false } = opts || {};

if (requireSecretKey) {
assertValidSecretKey(secretKey);
Expand Down Expand Up @@ -130,7 +150,8 @@ export function buildRequest(options: BuildRequestOptions) {
return null;
}

const formatKeys = (object: Parameters<typeof snakecaseKeys>[0]) => snakecaseKeys(object, { deep: false });
const formatKeys = (object: Parameters<typeof snakecaseKeys>[0]) =>
snakecaseKeys(object, { deep: deepSnakecaseBodyParamKeys });

return {
body: JSON.stringify(Array.isArray(bodyParams) ? bodyParams.map(formatKeys) : formatKeys(bodyParams)),
Expand Down
Loading