File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff 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' ,
Original file line number Diff line number Diff line change @@ -21,6 +21,10 @@ const basePath = '/m2m_tokens';
2121export type M2MTokenFormat = 'opaque' | 'jwt' ;
2222
2323type 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 ) {
You can’t perform that action at this time.
0 commit comments