Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Propagate audience option to verifyToken #978

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/backend/src/tokens/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ export async function verifyJwt(

// Verify audience claim (aud)
if (typeof aud === 'string') {
if (aud !== audience) {
if (audience && aud !== audience) {
throw new TokenVerificationError({
action: TokenVerificationErrorAction.EnsureClerkJWT,
reason: TokenVerificationErrorReason.TokenVerificationFailed,
message: `Invalid JWT audience claim (aud) ${JSON.stringify(aud)}. Expected "${audience}".`,
});
}
} else if (Array.isArray(aud) && aud.length > 0 && aud.every(a => typeof a === 'string')) {
if (!aud.includes(audience)) {
if (audience && !aud.includes(audience)) {
throw new TokenVerificationError({
action: TokenVerificationErrorAction.EnsureClerkJWT,
reason: TokenVerificationErrorReason.TokenVerificationFailed,
Expand Down
8 changes: 7 additions & 1 deletion packages/backend/src/tokens/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export type RequiredVerifyTokenOptions = Required<
export type OptionalVerifyTokenOptions = Partial<
Pick<
VerifyTokenOptions,
'authorizedParties' | 'clockSkewInSeconds' | 'jwksCacheTtlInMs' | 'skipJwksCache' | 'jwtKey' | 'proxyUrl'
| 'audience'
| 'authorizedParties'
| 'clockSkewInSeconds'
| 'jwksCacheTtlInMs'
| 'skipJwksCache'
| 'jwtKey'
| 'proxyUrl'
>
>;

Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/tokens/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export async function verifyToken(token: string, options: VerifyTokenOptions): P
secretKey,
apiUrl,
apiVersion,
audience,
authorizedParties,
clockSkewInSeconds,
issuer,
Expand Down Expand Up @@ -53,6 +54,7 @@ export async function verifyToken(token: string, options: VerifyTokenOptions): P
}

return await verifyJwt(token, {
audience,
authorizedParties,
clockSkewInSeconds,
key,
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 @@ -12,7 +12,7 @@ import { parseCookies } from './utils';
*/
export function authenticateRequest(args: LoaderFunctionArgs, opts: RootAuthLoaderOptions = {}): Promise<RequestState> {
const { request, context } = args;
const { loadSession, loadUser, loadOrganization, authorizedParties } = opts;
const { loadSession, loadUser, loadOrganization, audience, authorizedParties } = opts;

// Fetch environment variables across Remix runtimes.
// 1. First try from process.env if exists (Node).
Expand Down Expand Up @@ -77,6 +77,7 @@ export function authenticateRequest(args: LoaderFunctionArgs, opts: RootAuthLoad
forwardedHost: headers.get('x-forwarded-host') as string,
referrer: headers.get('referer') || '',
userAgent: headers.get('user-agent') as string,
audience,
authorizedParties,
proxyUrl,
isSatellite,
Expand Down
6 changes: 3 additions & 3 deletions packages/remix/src/ssr/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AuthObject, Organization, Session, User } from '@clerk/backend';
import type { AuthObject, OptionalVerifyTokenOptions, Organization, Session, User } from '@clerk/backend';
import type { MultiDomainAndOrProxy } from '@clerk/types';
import type { DataFunctionArgs, LoaderFunction } from '@remix-run/server-runtime';

Expand All @@ -19,8 +19,8 @@ export type RootAuthLoaderOptions = {
loadUser?: boolean;
loadSession?: boolean;
loadOrganization?: boolean;
authorizedParties?: [];
} & MultiDomainAndOrProxy;
} & Pick<OptionalVerifyTokenOptions, 'audience' | 'authorizedParties'> &
MultiDomainAndOrProxy;

export type RootAuthLoaderCallback<Options extends RootAuthLoaderOptions> = (
args: LoaderFunctionArgsWithAuth<Options>,
Expand Down