Skip to content

chore(nextjs): Add machine secret key environment variable to BAPI client init #6478

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

Merged
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
18 changes: 18 additions & 0 deletions .changeset/pink-countries-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"@clerk/astro": patch
"@clerk/express": patch
"@clerk/fastify": patch
"@clerk/nextjs": patch
"@clerk/nuxt": patch
"@clerk/react-router": patch
"@clerk/remix": patch
"@clerk/tanstack-react-start": patch
---

Add ability to define a machine secret key to Clerk BAPI client function

```ts
const clerkClient = createClerkClient({ machineSecretKey: 'ak_xxxxx' })

clerkClient.machineTokens.create({...})
```
1 change: 1 addition & 0 deletions packages/astro/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface InternalEnv {
readonly CLERK_API_VERSION?: string;
readonly CLERK_JWT_KEY?: string;
readonly CLERK_SECRET_KEY?: string;
readonly CLERK_MACHINE_SECRET_KEY?: string;
readonly PUBLIC_CLERK_DOMAIN?: string;
readonly PUBLIC_CLERK_IS_SATELLITE?: string;
readonly PUBLIC_CLERK_PROXY_URL?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/integration/create-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function createClerkEnvSchema() {
PUBLIC_CLERK_TELEMETRY_DISABLED: envField.boolean({ context: 'client', access: 'public', optional: true }),
PUBLIC_CLERK_TELEMETRY_DEBUG: envField.boolean({ context: 'client', access: 'public', optional: true }),
CLERK_SECRET_KEY: envField.string({ context: 'server', access: 'secret' }),
CLERK_MACHINE_SECRET_KEY: envField.string({ context: 'server', access: 'secret', optional: true }),
CLERK_JWT_KEY: envField.string({ context: 'server', access: 'secret', optional: true }),
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/server/clerk-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type CreateClerkClientWithOptions = (context: APIContext, options?: ClerkOptions
const createClerkClientWithOptions: CreateClerkClientWithOptions = (context, options) =>
createClerkClient({
secretKey: getSafeEnv(context).sk,
machineSecretKey: getSafeEnv(context).machineSecretKey,
publishableKey: getSafeEnv(context).pk,
apiUrl: getSafeEnv(context).apiUrl,
apiVersion: getSafeEnv(context).apiVersion,
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/server/get-safe-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function getSafeEnv(context: ContextOrLocals) {
proxyUrl: getContextEnvVar('PUBLIC_CLERK_PROXY_URL', context),
pk: getContextEnvVar('PUBLIC_CLERK_PUBLISHABLE_KEY', context),
sk: getContextEnvVar('CLERK_SECRET_KEY', context),
machineSecretKey: getContextEnvVar('CLERK_MACHINE_SECRET_KEY', context),
signInUrl: getContextEnvVar('PUBLIC_CLERK_SIGN_IN_URL', context),
signUpUrl: getContextEnvVar('PUBLIC_CLERK_SIGN_UP_URL', context),
clerkJsUrl: getContextEnvVar('PUBLIC_CLERK_JS_URL', context),
Expand Down
1 change: 1 addition & 0 deletions packages/express/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const loadClientEnv = () => {
export const loadApiEnv = () => {
return {
secretKey: process.env.CLERK_SECRET_KEY || '',
machineSecretKey: process.env.CLERK_MACHINE_SECRET_KEY || '',
apiUrl: process.env.CLERK_API_URL || 'https://api.clerk.com',
apiVersion: process.env.CLERK_API_VERSION || 'v1',
domain: process.env.CLERK_DOMAIN || '',
Expand Down
3 changes: 2 additions & 1 deletion packages/fastify/src/clerkClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createClerkClient } from '@clerk/backend';

import { API_URL, API_VERSION, JWT_KEY, SDK_METADATA, SECRET_KEY } from './constants';
import { API_URL, API_VERSION, JWT_KEY, MACHINE_SECRET_KEY, SDK_METADATA, SECRET_KEY } from './constants';

export const clerkClient = createClerkClient({
secretKey: SECRET_KEY,
machineSecretKey: MACHINE_SECRET_KEY,
apiUrl: API_URL,
apiVersion: API_VERSION,
jwtKey: JWT_KEY,
Expand Down
1 change: 1 addition & 0 deletions packages/fastify/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { apiUrlFromPublishableKey } from '@clerk/shared/apiUrlFromPublishableKey

export const API_VERSION = process.env.CLERK_API_VERSION || 'v1';
export const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';
export const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';
export const PUBLISHABLE_KEY = process.env.CLERK_PUBLISHABLE_KEY || '';
export const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);
export const JWT_KEY = process.env.CLERK_JWT_KEY || '';
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const CLERK_JS_VERSION = process.env.NEXT_PUBLIC_CLERK_JS_VERSION || '';
export const CLERK_JS_URL = process.env.NEXT_PUBLIC_CLERK_JS_URL || '';
export const API_VERSION = process.env.CLERK_API_VERSION || 'v1';
export const SECRET_KEY = process.env.CLERK_SECRET_KEY || '';
export const MACHINE_SECRET_KEY = process.env.CLERK_MACHINE_SECRET_KEY || '';
export const PUBLISHABLE_KEY = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY || '';
export const ENCRYPTION_KEY = process.env.CLERK_ENCRYPTION_KEY || '';
export const API_URL = process.env.CLERK_API_URL || apiUrlFromPublishableKey(PUBLISHABLE_KEY);
Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/src/server/createClerkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
API_VERSION,
DOMAIN,
IS_SATELLITE,
MACHINE_SECRET_KEY,
PROXY_URL,
PUBLISHABLE_KEY,
SDK_METADATA,
Expand All @@ -22,6 +23,7 @@ const clerkClientDefaultOptions = {
proxyUrl: PROXY_URL,
domain: DOMAIN,
isSatellite: IS_SATELLITE,
machineSecretKey: MACHINE_SECRET_KEY,
sdkMetadata: SDK_METADATA,
telemetry: {
disabled: TELEMETRY_DISABLED,
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare module 'nuxt/schema' {
interface RuntimeConfig {
clerk: {
secretKey?: string;
machineSecretKey?: string;
jwtKey?: string;
webhookSigningSecret?: string;
};
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default defineNuxtModule<ModuleOptions>({
// Private keys available only on within server-side
clerk: {
secretKey: undefined,
machineSecretKey: undefined,
jwtKey: undefined,
webhookSigningSecret: undefined,
},
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/runtime/server/clerkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function clerkClient(event: H3Event) {
domain: runtimeConfig.public.clerk.domain,
isSatellite: runtimeConfig.public.clerk.isSatellite,
secretKey: runtimeConfig.clerk.secretKey,
machineSecretKey: runtimeConfig.clerk.machineSecretKey,
jwtKey: runtimeConfig.clerk.jwtKey,
telemetry: {
disabled: isTruthy(runtimeConfig.public.clerk.telemetry?.disabled),
Expand Down
3 changes: 2 additions & 1 deletion packages/react-router/src/ssr/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function authenticateRequest(
const { request } = args;
const { audience, authorizedParties } = opts;

const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey } = opts;
const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey, machineSecretKey } = opts;
const { signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl } = opts;

const requestState = await createClerkClient({
Expand All @@ -24,6 +24,7 @@ export async function authenticateRequest(
isSatellite,
domain,
publishableKey,
machineSecretKey,
userAgent: `${PACKAGE_NAME}@${PACKAGE_VERSION}`,
}).authenticateRequest(patchRequest(request), {
audience,
Expand Down
2 changes: 2 additions & 0 deletions packages/react-router/src/ssr/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const loadOptions = (args: LoaderFunctionArgs, overrides: RootAuthLoaderO
// 4. Then try from globalThis (Cloudflare Workers).
// 5. Then from loader context (Cloudflare Pages).
const secretKey = overrides.secretKey || getEnvVariable('CLERK_SECRET_KEY', context);
const machineSecretKey = overrides.machineSecretKey || getEnvVariable('CLERK_MACHINE_SECRET_KEY', context);
const publishableKey = overrides.publishableKey || getPublicEnvVariables(context).publishableKey;
const jwtKey = overrides.jwtKey || getEnvVariable('CLERK_JWT_KEY', context);
const apiUrl = getEnvVariable('CLERK_API_URL', context) || apiUrlFromPublishableKey(publishableKey);
Expand Down Expand Up @@ -67,6 +68,7 @@ export const loadOptions = (args: LoaderFunctionArgs, overrides: RootAuthLoaderO
// used to append options that are not initialized from env
...overrides,
secretKey,
machineSecretKey,
publishableKey,
jwtKey,
apiUrl,
Expand Down
4 changes: 4 additions & 0 deletions packages/react-router/src/ssr/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export type RootAuthLoaderOptions = {
* Used to override the CLERK_SECRET_KEY env variable if needed.
*/
secretKey?: string;
/**
* Used to override the CLERK_MACHINE_SECRET_KEY env variable if needed.
*/
machineSecretKey?: string;
/**
* @deprecated Use [session token claims](https://clerk.com/docs/backend-requests/making/custom-session-token) instead.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/remix/src/ssr/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function authenticateRequest(
const { request } = args;
const { audience, authorizedParties } = opts;

const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey } = opts;
const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey, machineSecretKey } = opts;
const { signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl } = opts;

const requestState = await createClerkClient({
Expand All @@ -32,6 +32,7 @@ export async function authenticateRequest(
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
machineSecretKey,
});

const locationHeader = requestState.headers.get(constants.Headers.Location);
Expand Down
2 changes: 2 additions & 0 deletions packages/remix/src/ssr/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const loadOptions = (args: LoaderFunctionArgs, overrides: RootAuthLoaderO
// 3. Then try from globalThis (Cloudflare Workers).
// 4. Then from loader context (Cloudflare Pages).
const secretKey = overrides.secretKey || getEnvVariable('CLERK_SECRET_KEY', context) || '';
const machineSecretKey = overrides.machineSecretKey || getEnvVariable('CLERK_MACHINE_SECRET_KEY', context);
const publishableKey = overrides.publishableKey || getEnvVariable('CLERK_PUBLISHABLE_KEY', context) || '';
const jwtKey = overrides.jwtKey || getEnvVariable('CLERK_JWT_KEY', context);
const apiUrl = getEnvVariable('CLERK_API_URL', context) || apiUrlFromPublishableKey(publishableKey);
Expand Down Expand Up @@ -69,6 +70,7 @@ export const loadOptions = (args: LoaderFunctionArgs, overrides: RootAuthLoaderO
// used to append options that are not initialized from env
...overrides,
secretKey,
machineSecretKey,
publishableKey,
jwtKey,
apiUrl,
Expand Down
4 changes: 4 additions & 0 deletions packages/remix/src/ssr/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export type RootAuthLoaderOptions = {
publishableKey?: string;
jwtKey?: string;
secretKey?: string;
/**
* Used to override the CLERK_MACHINE_SECRET_KEY env variable if needed.
*/
machineSecretKey?: string;
/**
* @deprecated Use [session token claims](https://clerk.com/docs/backend-requests/making/custom-session-token) instead.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export async function authenticateRequest(
): Promise<AuthenticatedState | UnauthenticatedState> {
const { audience, authorizedParties } = opts;

const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey, acceptsToken } = opts;
const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey, acceptsToken, machineSecretKey } =
opts;
const { signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl } = opts;

const requestState = await createClerkClient({
apiUrl,
secretKey,
machineSecretKey,
jwtKey,
proxyUrl,
isSatellite,
Expand Down
1 change: 1 addition & 0 deletions packages/tanstack-react-start/src/server/clerkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const clerkClient = (options?: ClerkOptions): ClerkClient => {
const commonEnv = commonEnvs();
return createClerkClient({
secretKey: commonEnv.SECRET_KEY,
machineSecretKey: commonEnv.MACHINE_SECRET_KEY,
publishableKey: commonEnv.PUBLISHABLE_KEY,
apiUrl: commonEnv.API_URL,
apiVersion: commonEnv.API_VERSION,
Expand Down
1 change: 1 addition & 0 deletions packages/tanstack-react-start/src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const commonEnvs = () => {
// Server-only environment variables
API_VERSION: getEnvVariable('CLERK_API_VERSION') || 'v1',
SECRET_KEY: getEnvVariable('CLERK_SECRET_KEY'),
MACHINE_SECRET_KEY: getEnvVariable('CLERK_MACHINE_SECRET_KEY'),
ENCRYPTION_KEY: getEnvVariable('CLERK_ENCRYPTION_KEY'),
CLERK_JWT_KEY: getEnvVariable('CLERK_JWT_KEY'),
API_URL: getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(publicEnvs.publishableKey),
Expand Down
2 changes: 2 additions & 0 deletions packages/tanstack-react-start/src/server/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const loadOptions = (request: Request, overrides: LoaderOptions = {}) =>
const clerkRequest = createClerkRequest(patchRequest(request));
const commonEnv = commonEnvs();
const secretKey = overrides.secretKey || commonEnv.SECRET_KEY;
const machineSecretKey = overrides.machineSecretKey || commonEnv.MACHINE_SECRET_KEY;
const publishableKey = overrides.publishableKey || commonEnv.PUBLISHABLE_KEY;
const jwtKey = overrides.jwtKey || commonEnv.CLERK_JWT_KEY;
const apiUrl = getEnvVariable('CLERK_API_URL') || apiUrlFromPublishableKey(publishableKey);
Expand Down Expand Up @@ -52,6 +53,7 @@ export const loadOptions = (request: Request, overrides: LoaderOptions = {}) =>
// used to append options that are not initialized from env
...overrides,
secretKey,
machineSecretKey,
publishableKey,
jwtKey,
apiUrl,
Expand Down
1 change: 1 addition & 0 deletions packages/tanstack-react-start/src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type LoaderOptions = {
publishableKey?: string;
jwtKey?: string;
secretKey?: string;
machineSecretKey?: string;
signInUrl?: string;
signUpUrl?: string;
} & Pick<VerifyTokenOptions, 'audience' | 'authorizedParties'> &
Expand Down