Skip to content

Commit 0af2e6f

Browse files
authored
chore(backend): Allow usage of machine secret key when listing M2M tokens (#7968)
1 parent fa75344 commit 0af2e6f

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

.changeset/smooth-points-search.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@clerk/backend": patch
3+
---
4+
5+
Allow usage of machine secret key when listing M2M tokens:
6+
7+
```ts
8+
const clerkClient = createClerkClient();
9+
10+
const m2mToken = await clerkClient.m2m.list({
11+
machineSecretKey: 'ak_xxxxx',
12+
subject: machineId,
13+
});
14+
```

packages/backend/src/api/__tests__/M2MTokenApi.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,35 @@ describe('M2MToken', () => {
626626
expect(response.totalCount).toBe(2);
627627
});
628628

629+
it('lists m2m tokens using machine secret key option', async () => {
630+
const apiClient = createBackendApiClient({
631+
apiUrl: 'https://api.clerk.test',
632+
});
633+
634+
server.use(
635+
http.get(
636+
'https://api.clerk.test/m2m_tokens',
637+
validateHeaders(({ request }) => {
638+
expect(request.headers.get('Authorization')).toBe('Bearer ak_xxxxx');
639+
const url = new URL(request.url);
640+
expect(url.searchParams.get('subject')).toBe(machineId);
641+
expect(url.searchParams.has('machineSecretKey')).toBe(false);
642+
return HttpResponse.json(mockM2MTokenList);
643+
}),
644+
),
645+
);
646+
647+
const response = await apiClient.m2m.list({
648+
machineSecretKey: 'ak_xxxxx',
649+
subject: machineId,
650+
});
651+
652+
expect(response.data).toHaveLength(2);
653+
expect(response.data[0].id).toBe('mt_1xxxxxxxxxxxxx');
654+
expect(response.data[1].id).toBe('mt_2xxxxxxxxxxxxx');
655+
expect(response.totalCount).toBe(2);
656+
});
657+
629658
it('requires a machine secret or instance secret to list m2m tokens', async () => {
630659
const apiClient = createBackendApiClient({
631660
apiUrl: 'https://api.clerk.test',

packages/backend/src/api/endpoints/M2MTokenApi.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const basePath = '/m2m_tokens';
2121
export type M2MTokenFormat = 'opaque' | 'jwt';
2222

2323
type GetM2MTokenListParams = ClerkPaginationRequest<{
24+
/**
25+
* Custom machine secret key for authentication.
26+
*/
27+
machineSecretKey?: string;
2428
/**
2529
* The machine ID to query machine-to-machine tokens by
2630
*/
@@ -108,11 +112,18 @@ export class M2MTokenApi extends AbstractAPI {
108112
}
109113

110114
async list(queryParams: GetM2MTokenListParams) {
111-
return this.request<PaginatedResourceResponse<M2MToken[]>>({
112-
method: 'GET',
113-
path: basePath,
114-
queryParams,
115-
});
115+
const { machineSecretKey, ...params } = queryParams;
116+
117+
const requestOptions = this.#createRequestOptions(
118+
{
119+
method: 'GET',
120+
path: basePath,
121+
queryParams: params,
122+
},
123+
machineSecretKey,
124+
);
125+
126+
return this.request<PaginatedResourceResponse<M2MToken[]>>(requestOptions);
116127
}
117128

118129
async createToken(params?: CreateM2MTokenParams) {

0 commit comments

Comments
 (0)