Skip to content

Commit 19f18f8

Browse files
authored
chore(backend): Add machine secret key rotation BAPI method (#6760)
1 parent 50a8622 commit 19f18f8

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

.changeset/angry-impalas-draw.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+
Add machine secret key rotation BAPI method
6+
7+
Usage:
8+
9+
```ts
10+
clerkClient.machines.rotateSecretKey({
11+
machineId: 'mch_xxx',
12+
previousTokenTtl: 3600,
13+
})
14+
```

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ describe('MachineAPI', () => {
171171
expect(response.secret).toBe('ak_test_...');
172172
});
173173

174+
it('rotates a machine secret key', async () => {
175+
server.use(
176+
http.post(
177+
`https://api.clerk.test/v1/machines/${machineId}/secret_key/rotate`,
178+
validateHeaders(() => {
179+
return HttpResponse.json({
180+
secret: 'ak_updated_...',
181+
});
182+
}),
183+
),
184+
);
185+
186+
const response = await apiClient.machines.rotateSecretKey({
187+
machineId,
188+
previousTokenTtl: 3600,
189+
});
190+
191+
expect(response.secret).toBe('ak_updated_...');
192+
});
193+
174194
it('creates a machine scope', async () => {
175195
server.use(
176196
http.post(

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ type GetMachineListParams = {
4343
query?: string;
4444
};
4545

46+
type RotateMachineSecretKeyParams = {
47+
/**
48+
* The ID of the machine to rotate the secret key for.
49+
*/
50+
machineId: string;
51+
/**
52+
* The time in seconds that the previous secret key will remain valid after rotation.
53+
*/
54+
previousTokenTtl: number;
55+
};
56+
4657
export class MachineApi extends AbstractAPI {
4758
async get(machineId: string) {
4859
this.requireId(machineId);
@@ -94,6 +105,18 @@ export class MachineApi extends AbstractAPI {
94105
});
95106
}
96107

108+
async rotateSecretKey(params: RotateMachineSecretKeyParams) {
109+
const { machineId, previousTokenTtl } = params;
110+
this.requireId(machineId);
111+
return this.request<MachineSecretKey>({
112+
method: 'POST',
113+
path: joinPaths(basePath, machineId, 'secret_key', 'rotate'),
114+
bodyParams: {
115+
previousTokenTtl,
116+
},
117+
});
118+
}
119+
97120
/**
98121
* Creates a new machine scope, allowing the specified machine to access another machine.
99122
*

0 commit comments

Comments
 (0)