-
Notifications
You must be signed in to change notification settings - Fork 419
chore(backend): Rename M2M secret -> token
#6536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/backend": minor | ||
| --- | ||
|
|
||
| Deprecates `clerkClient.m2mTokens.verifySecret({ secret: 'mt_xxx' })` in favor or `clerkClient.m2mTokens.verifyToken({ token: 'mt_xxx' })` |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { deprecated } from '@clerk/shared/deprecated'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { joinPaths } from '../../util/path'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { ClerkBackendApiRequestOptions } from '../request'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { M2MToken } from '../resources/M2MToken'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -31,7 +33,7 @@ type RevokeM2MTokenParams = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| revocationReason?: string | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type VerifyM2MTokenParams = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type VerifyM2MTokenParamsDeprecated = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Custom machine secret key for authentication. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -42,6 +44,17 @@ type VerifyM2MTokenParams = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| secret: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type VerifyM2MTokenParams = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Custom machine secret key for authentication. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| machineSecretKey?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Machine-to-machine token to verify. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class M2MTokenApi extends AbstractAPI { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #createRequestOptions(options: ClerkBackendApiRequestOptions, machineSecretKey?: string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (machineSecretKey) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -94,9 +107,16 @@ export class M2MTokenApi extends AbstractAPI { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.request<M2MToken>(requestOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async verifySecret(params: VerifyM2MTokenParams) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Verify a machine-to-machine token. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @deprecated Use {@link verifyToken} instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async verifySecret(params: VerifyM2MTokenParamsDeprecated) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { secret, machineSecretKey } = params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deprecated('verifySecret', 'Use `verifyToken({ token: mt_xxx })` instead'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestOptions = this.#createRequestOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+110
to
120
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add explicit return type and explicit access modifier to public API Per guidelines, public APIs should declare explicit return types and use explicit access modifiers. Also, consider including a removal timeline in the deprecation message to set expectations. - async verifySecret(params: VerifyM2MTokenParamsDeprecated) {
+ public async verifySecret(params: VerifyM2MTokenParamsDeprecated): Promise<M2MToken> {
const { secret, machineSecretKey } = params;
- deprecated('verifySecret', 'Use `verifyToken({ token: mt_xxx })` instead');
+ deprecated('verifySecret', 'Use `verifyToken({ token: mt_xxx })` instead');📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -108,4 +128,19 @@ export class M2MTokenApi extends AbstractAPI { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.request<M2MToken>(requestOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async verifyToken(params: VerifyM2MTokenParams) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { token, machineSecretKey } = params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestOptions = this.#createRequestOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path: joinPaths(basePath, 'verify'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bodyParams: { token }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| machineSecretKey, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.request<M2MToken>(requestOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+132
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Document verifyToken and add explicit return type with access modifier Add JSDoc for the new public method and specify the return type. This improves DX and aligns with the project’s TypeScript/JSDoc guidelines. - async verifyToken(params: VerifyM2MTokenParams) {
+ /**
+ * Verify a machine-to-machine token.
+ *
+ * Verifies the provided M2M token against the Clerk API.
+ * By default, the client uses the configured Machine Secret Key.
+ * You can override it per-call by passing `machineSecretKey`.
+ *
+ * @param params - The verification parameters.
+ * @returns The verified M2MToken resource.
+ */
+ public async verifyToken(params: VerifyM2MTokenParams): Promise<M2MToken> {
const { token, machineSecretKey } = params;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ export class M2MToken { | |
| readonly expiration: number | null, | ||
| readonly createdAt: number, | ||
| readonly updatedAt: number, | ||
| readonly token?: string, | ||
| readonly secret?: string, | ||
| ) {} | ||
|
Comment on lines
+15
to
17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid a silent breaking change in the constructor parameter order Inserting token before secret changes the positional index of secret. Any direct instantiation like new M2MToken(..., updatedAt, secret) will now set token instead of secret, leaving secret undefined. This can cause subtle bugs without type errors. To preserve backward compatibility, keep secret in its original position and add token after it. Update fromJSON accordingly. - readonly updatedAt: number,
- readonly token?: string,
- readonly secret?: string,
+ readonly updatedAt: number,
+ readonly secret?: string,
+ readonly token?: string,
@@
- data.updated_at,
- data.token,
- data.secret,
+ data.updated_at,
+ data.secret,
+ data.token,Also applies to: 31-33 🤖 Prompt for AI Agents |
||
|
|
||
|
|
@@ -27,6 +28,7 @@ export class M2MToken { | |
| data.expiration, | ||
| data.created_at, | ||
| data.updated_at, | ||
| data.token, | ||
| data.secret, | ||
| ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -200,13 +200,13 @@ function handleClerkAPIError( | |||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| async function verifyMachineToken( | ||||||||||||||||||||||||||||||||||||||||
| secret: string, | ||||||||||||||||||||||||||||||||||||||||
| async function verifyM2MToken( | ||||||||||||||||||||||||||||||||||||||||
| token: string, | ||||||||||||||||||||||||||||||||||||||||
| options: VerifyTokenOptions & { machineSecretKey?: string }, | ||||||||||||||||||||||||||||||||||||||||
| ): Promise<MachineTokenReturnType<M2MToken, MachineTokenVerificationError>> { | ||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||
| const client = createBackendApiClient(options); | ||||||||||||||||||||||||||||||||||||||||
| const verifiedToken = await client.m2mTokens.verifySecret({ secret }); | ||||||||||||||||||||||||||||||||||||||||
| const verifiedToken = await client.m2mTokens.verifyToken({ token }); | ||||||||||||||||||||||||||||||||||||||||
| return { data: verifiedToken, tokenType: TokenType.M2MToken, errors: undefined }; | ||||||||||||||||||||||||||||||||||||||||
| } catch (err: any) { | ||||||||||||||||||||||||||||||||||||||||
| return handleClerkAPIError(TokenType.M2MToken, err, 'Machine token not found'); | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -247,7 +247,7 @@ async function verifyAPIKey( | |||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| export async function verifyMachineAuthToken(token: string, options: VerifyTokenOptions) { | ||||||||||||||||||||||||||||||||||||||||
| if (token.startsWith(M2M_TOKEN_PREFIX)) { | ||||||||||||||||||||||||||||||||||||||||
| return verifyMachineToken(token, options); | ||||||||||||||||||||||||||||||||||||||||
| return verifyM2MToken(token, options); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
248
to
251
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add explicit return type to the public API Public APIs should declare explicit return types. Make verifyMachineAuthToken’s return type a union over supported token data. Apply this diff: -export async function verifyMachineAuthToken(token: string, options: VerifyTokenOptions) {
+export async function verifyMachineAuthToken(
+ token: string,
+ options: VerifyTokenOptions,
+): Promise<MachineTokenReturnType<M2MToken | IdPOAuthAccessToken | APIKey, MachineTokenVerificationError>> {
if (token.startsWith(M2M_TOKEN_PREFIX)) {
return verifyM2MToken(token, options);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
| if (token.startsWith(OAUTH_TOKEN_PREFIX)) { | ||||||||||||||||||||||||||||||||||||||||
| return verifyOAuthToken(token, options); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Export the new VerifyM2MTokenParams type
Same rationale as above: export the public-facing type.
🤖 Prompt for AI Agents