Skip to content

Commit

Permalink
feat(backend): Enforce request param in authenticateRequest - repla…
Browse files Browse the repository at this point in the history
…ce header related options
  • Loading branch information
dimkl committed Nov 16, 2023
1 parent 1119b0f commit 1a7879d
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 214 deletions.
11 changes: 1 addition & 10 deletions packages/backend/src/api/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@ import {
} from './endpoints';
import { buildRequest } from './request';

export type CreateBackendApiOptions = {
/* Secret Key */
secretKey?: string;
/* Backend API URL */
apiUrl?: string;
/* Backend API version */
apiVersion?: string;
/* Library/SDK name */
userAgent?: string;
};
export type CreateBackendApiOptions = Parameters<typeof buildRequest>[0];

export type ApiClient = ReturnType<typeof createBackendApiClient>;

Expand Down
14 changes: 12 additions & 2 deletions packages/backend/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { API_URL, API_VERSION, constants, USER_AGENT } from '../constants';
import runtime from '../runtime';
import { assertValidSecretKey } from '../util/assertValidSecretKey';
import { joinPaths } from '../util/path';
import type { CreateBackendApiOptions } from './factory';
import { deserialize } from './resources/Deserializer';

export type ClerkBackendApiRequestOptions = {
Expand Down Expand Up @@ -64,7 +63,18 @@ const withLegacyReturn =
}
};

export function buildRequest(options: CreateBackendApiOptions) {
type BuildRequestOptions = {
/* Secret Key */
secretKey?: string;
/* Backend API URL */
apiUrl?: string;
/* Backend API version */
apiVersion?: string;
/* Library/SDK name */
userAgent?: string;
};

export function buildRequest(options: BuildRequestOptions) {
const request = async <T>(requestOptions: ClerkBackendApiRequestOptions): Promise<ClerkBackendApiResponse<T>> => {
const { secretKey, apiUrl = API_URL, apiVersion = API_VERSION, userAgent = USER_AGENT } = options;
const { path, method, queryParams, headerParams, bodyParams, formData } = requestOptions;
Expand Down
7 changes: 2 additions & 5 deletions packages/backend/src/tokens/authObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import type {
ServerGetTokenOptions,
} from '@clerk/types';

import type { Organization, Session, User } from '../api';
import type { CreateBackendApiOptions, Organization, Session, User } from '../api';
import { createBackendApiClient } from '../api';

type AuthObjectDebugData = Record<string, any>;
type CreateAuthObjectDebug = (data?: AuthObjectDebugData) => AuthObjectDebug;
type AuthObjectDebug = () => AuthObjectDebugData;

export type SignedInAuthObjectOptions = {
secretKey?: string;
apiUrl?: string;
apiVersion?: string;
export type SignedInAuthObjectOptions = CreateBackendApiOptions & {
token: string;
session?: Session;
user?: User;
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/tokens/authStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ type RequestStateParams = {
domain?: string;
isSatellite?: boolean;
proxyUrl?: string;
searchParams?: URLSearchParams;
signInUrl?: string;
signUpUrl?: string;
afterSignInUrl?: string;
Expand Down
8 changes: 1 addition & 7 deletions packages/backend/src/tokens/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type CreateAuthenticateRequestOptions = {
| 'proxyUrl'
| 'domain'
| 'isSatellite'
| 'userAgent'
>
>;
apiClient: ApiClient;
Expand All @@ -36,7 +35,6 @@ export function createAuthenticateRequest(params: CreateAuthenticateRequestOptio
isSatellite: buildtimeIsSatellite = false,
domain: buildtimeDomain = '',
audience: buildtimeAudience = '',
userAgent: buildtimeUserAgent,
} = params.options;

const authenticateRequest = ({
Expand All @@ -47,8 +45,6 @@ export function createAuthenticateRequest(params: CreateAuthenticateRequestOptio
jwtKey: runtimeJwtKey,
isSatellite: runtimeIsSatellite,
domain: runtimeDomain,
searchParams: runtimeSearchParams,
userAgent: runtimeUserAgent,
...rest
}: Omit<AuthenticateRequestOptions, 'apiUrl' | 'apiVersion'>) => {
return authenticateRequestOriginal({
Expand All @@ -61,9 +57,7 @@ export function createAuthenticateRequest(params: CreateAuthenticateRequestOptio
publishableKey: runtimePublishableKey || buildtimePublishableKey,
isSatellite: runtimeIsSatellite || buildtimeIsSatellite,
domain: runtimeDomain || buildtimeDomain,
jwtKey: runtimeJwtKey || buildtimeJwtKey,
searchParams: runtimeSearchParams,
userAgent: runtimeUserAgent?.toString() || buildtimeUserAgent,
jwtKey: runtimeJwtKey || buildtimeJwtKey
});
};

Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/tokens/interstitialRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { AuthStatusOptionsType, RequestState } from './authStatus';
import { AuthErrorReason, interstitial, signedIn, signedOut } from './authStatus';
import { verifyToken } from './verify';

type InterstitialRuleOptions = AuthStatusOptionsType & {
export type InterstitialRuleOptions = AuthStatusOptionsType & {
/* Request origin header value */
origin?: string;
/* Request host header value */
Expand All @@ -23,6 +23,8 @@ type InterstitialRuleOptions = AuthStatusOptionsType & {
clientUat?: string;
/* Client token header value */
headerToken?: string;
/* Request search params value */
searchParams?: URLSearchParams;
};

type InterstitialRuleResult = RequestState | undefined;
Expand Down
Loading

0 comments on commit 1a7879d

Please sign in to comment.