Skip to content

Commit

Permalink
fix(backend): Make all 4 keys (legacy and new) optional in `authentic…
Browse files Browse the repository at this point in the history
…ateRequest` (#1437)
  • Loading branch information
anagstef committed Jun 29, 2023
1 parent ae0aad0 commit ac4e472
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 64 deletions.
6 changes: 6 additions & 0 deletions .changeset/shaggy-spiders-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-sdk-node': patch
'@clerk/backend': patch
---

Make all 4 keys (legacy and new) optional in authenticateRequest params
2 changes: 0 additions & 2 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { createBackendApiClient } from './api';
import type { CreateAuthenticateRequestOptions } from './tokens';
import { createAuthenticateRequest } from './tokens';

export type { InstanceKeys } from './tokens';

export * from './api/resources';
export * from './tokens';
export * from './tokens/jwt';
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export {
OptionalVerifyTokenOptions,
RequiredVerifyTokenOptions,
} from './request';
export type { InstanceKeys } from './request';
14 changes: 7 additions & 7 deletions packages/backend/src/tokens/interstitialRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const isBrowser = (userAgent: string | undefined) => VALID_USER_AGENTS.test(user
// automatically treated as signed out. This exception is needed for development, because the any // missing uat throws an interstitial in development.
export const nonBrowserRequestInDevRule: InterstitialRule = options => {
const { apiKey, secretKey, userAgent } = options;
const key = secretKey || apiKey;
const key = secretKey || apiKey || '';
if (isDevelopmentFromApiKey(key) && !isBrowser(userAgent)) {
return signedOut(options, AuthErrorReason.HeaderMissingNonBrowser);
}
Expand All @@ -53,7 +53,7 @@ export const crossOriginRequestWithoutHeader: InterstitialRule = options => {

export const isPrimaryInDevAndRedirectsToSatellite: InterstitialRule = options => {
const { apiKey, secretKey, isSatellite, searchParams } = options;
const key = secretKey || apiKey;
const key = secretKey || apiKey || '';
const isDev = isDevelopmentFromApiKey(key);

if (isDev && !isSatellite && shouldRedirectToSatelliteUrl(searchParams)) {
Expand All @@ -64,7 +64,7 @@ export const isPrimaryInDevAndRedirectsToSatellite: InterstitialRule = options =

export const potentialFirstLoadInDevWhenUATMissing: InterstitialRule = options => {
const { apiKey, secretKey, clientUat } = options;
const key = secretKey || apiKey;
const key = secretKey || apiKey || '';
const res = isDevelopmentFromApiKey(key);
if (res && !clientUat) {
return interstitial(options, AuthErrorReason.CookieUATMissing);
Expand All @@ -80,7 +80,7 @@ export const potentialRequestAfterSignInOrOutFromClerkHostedUiInDev: Interstitia
const { apiKey, secretKey, referrer, host, forwardedHost, forwardedPort, forwardedProto } = options;
const crossOriginReferrer =
referrer && checkCrossOrigin({ originURL: new URL(referrer), host, forwardedHost, forwardedPort, forwardedProto });
const key = secretKey || apiKey;
const key = secretKey || apiKey || '';

if (isDevelopmentFromApiKey(key) && crossOriginReferrer) {
return interstitial(options, AuthErrorReason.CrossOriginReferrer);
Expand All @@ -91,7 +91,7 @@ export const potentialRequestAfterSignInOrOutFromClerkHostedUiInDev: Interstitia
export const satelliteInDevReturningFromPrimary: InterstitialRule = options => {
const { apiKey, secretKey, isSatellite, searchParams } = options;

const key = secretKey || apiKey;
const key = secretKey || apiKey || '';

if (isSatellite && isReturningFromPrimary(searchParams) && isDevelopmentFromApiKey(key)) {
return interstitial(options, AuthErrorReason.SatelliteReturnsFromPrimary);
Expand All @@ -101,7 +101,7 @@ export const satelliteInDevReturningFromPrimary: InterstitialRule = options => {

export const potentialFirstRequestOnProductionEnvironment: InterstitialRule = options => {
const { apiKey, secretKey, clientUat, cookieToken } = options;
const key = secretKey || apiKey;
const key = secretKey || apiKey || '';

if (isProductionFromApiKey(key) && !clientUat && !cookieToken) {
return signedOut(options, AuthErrorReason.CookieAndUATMissing);
Expand Down Expand Up @@ -189,7 +189,7 @@ async function verifyRequestState(options: AuthenticateRequestOptions, token: st
export const isSatelliteAndNeedsSyncing: InterstitialRule = options => {
const { clientUat, isSatellite, searchParams, secretKey, apiKey, userAgent } = options;

const key = secretKey || apiKey;
const key = secretKey || apiKey || '';
const isDev = isDevelopmentFromApiKey(key);

const isSignedOut = !clientUat || clientUat === '0';
Expand Down
63 changes: 12 additions & 51 deletions packages/backend/src/tokens/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,57 +40,18 @@ export type OptionalVerifyTokenOptions = Partial<
>
>;

type PublicKeys =
| {
publishableKey: string;
/**
* @deprecated Use `publishableKey` instead.
*/
frontendApi: never;
}
| {
publishableKey: never;
/**
* @deprecated Use `publishableKey` instead.
*/
frontendApi: string;
}
| {
publishableKey: string;
/**
* @deprecated Use `publishableKey` instead.
*/
frontendApi: string;
};

type SecretKeys =
| {
secretKey: string;
/**
* @deprecated Use `secretKey` instead.
*/
apiKey: never;
}
| {
secretKey: never;
/**
* @deprecated Use `secretKey` instead.
*/
apiKey: string;
}
| {
secretKey: string;
/**
* @deprecated Use `secretKey` instead.
*/
apiKey: string;
};

export type InstanceKeys = PublicKeys & SecretKeys;

export type AuthenticateRequestOptions = InstanceKeys &
OptionalVerifyTokenOptions &
export type AuthenticateRequestOptions = OptionalVerifyTokenOptions &
LoadResourcesOptions & {
publishableKey?: string;
secretKey?: string;
/**
* @deprecated Use `publishableKey` instead.
*/
frontendApi?: string;
/**
* @deprecated Use `secretKey` instead.
*/
apiKey?: string;
apiVersion?: string;
apiUrl?: string;
/* Client token cookie value */
Expand Down Expand Up @@ -156,7 +117,7 @@ export async function authenticateRequest(options: AuthenticateRequestOptions):
assertValidSecretKey(options.secretKey || options.apiKey);

if (options.isSatellite) {
assertSignInUrlExists(options.signInUrl, options.secretKey || options.apiKey);
assertSignInUrlExists(options.signInUrl, (options.secretKey || options.apiKey) as string);
assertProxyUrlOrDomain(options.proxyUrl || options.domain);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-node/src/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const authenticateRequest = (opts: AuthenticateRequestParams) => {
throw new Error(satelliteAndMissingProxyUrlAndDomain);
}

if (isSatellite && !isHttpOrHttps(signInUrl) && isDevelopmentFromApiKey(secretKey || apiKey)) {
if (isSatellite && !isHttpOrHttps(signInUrl) && isDevelopmentFromApiKey(secretKey || apiKey || '')) {
throw new Error(satelliteAndMissingSignInUrl);
}

Expand Down
14 changes: 12 additions & 2 deletions packages/sdk-node/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AuthenticateRequestOptions, AuthObject, Clerk, InstanceKeys, SignedInAuthObject } from '@clerk/backend';
import type { AuthenticateRequestOptions, AuthObject, Clerk, SignedInAuthObject } from '@clerk/backend';
import type { MultiDomainAndOrProxy } from '@clerk/types';
import type { NextFunction, Request, Response } from 'express';
import type { IncomingMessage } from 'http';
Expand Down Expand Up @@ -38,8 +38,18 @@ export type ClerkMiddlewareOptions = {

export type ClerkClient = ReturnType<typeof Clerk>;

export type AuthenticateRequestParams = InstanceKeys & {
export type AuthenticateRequestParams = {
clerkClient: ClerkClient;
publishableKey?: string;
secretKey?: string;
/**
* @deprecated Use `publishableKey` instead.
*/
frontendApi?: string;
/**
* @deprecated Use `secretKey` instead.
*/
apiKey?: string;
req: IncomingMessage;
options?: ClerkMiddlewareOptions;
};

0 comments on commit ac4e472

Please sign in to comment.