Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekradko committed Oct 9, 2024
1 parent 012717a commit 9cab1f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
15 changes: 5 additions & 10 deletions packages/backend/src/tokens/__tests__/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ describe('tokens.loadClerkJWKFromRemote(options)', () => {

it('loads JWKS from Backend API when secretKey is provided', async () => {
server.use(
http.get('https://api.clerk.com/v1/jwks', ({ request }) => {
console.log('Outgoing:', request.method, request.url);
http.get('https://api.clerk.com/v1/jwks', () => {
return HttpResponse.json(mockJwks);
}),
);
Expand All @@ -65,8 +64,7 @@ describe('tokens.loadClerkJWKFromRemote(options)', () => {

it('loads JWKS from Backend API using the provided apiUrl', async () => {
server.use(
http.get('https://api.clerk.test/v1/jwks', ({ request }) => {
console.log('Outgoing:', request.method, request.url);
http.get('https://api.clerk.test/v1/jwks', () => {
return HttpResponse.json(mockJwks);
}),
);
Expand All @@ -83,8 +81,7 @@ describe('tokens.loadClerkJWKFromRemote(options)', () => {

it('caches JWK by kid', async () => {
server.use(
http.get('https://api.clerk.com/v1/jwks', ({ request }) => {
console.log('Outgoing:', request.method, request.url);
http.get('https://api.clerk.com/v1/jwks', () => {
return HttpResponse.json(mockJwks);
}),
);
Expand All @@ -110,8 +107,7 @@ describe('tokens.loadClerkJWKFromRemote(options)', () => {
// fakeFetch.onCall(4).returns(jsonError('Connection to the origin web server failed', 542));

server.use(
http.get('https://api.clerk.com/v1/jwks', ({ request }) => {
console.log('Outgoing1:', request.method, request.url);
http.get('https://api.clerk.com/v1/jwks', () => {
return HttpResponse.json({}, { status: 503 });
}),
);
Expand Down Expand Up @@ -158,8 +154,7 @@ describe('tokens.loadClerkJWKFromRemote(options)', () => {

it('throws an error when no JWK matches the provided kid', async () => {
server.use(
http.get('https://api.clerk.com/v1/jwks', ({ request }) => {
console.log('Outgoing:', request.method, request.url);
http.get('https://api.clerk.com/v1/jwks', () => {
return HttpResponse.json(mockJwks);
}),
);
Expand Down
47 changes: 20 additions & 27 deletions packages/backend/src/tokens/__tests__/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { http, HttpResponse } from 'msw';
import sinon from 'sinon';
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';

import { TokenVerificationErrorReason } from '../../errors';
import {
mockExpiredJwt,
mockInvalidSignatureJwt,
mockJwks,
mockJwt,
mockJwtPayload,
mockMalformedJwt,
} from '../../fixtures';
import runtime from '../../runtime';
import { jsonOk } from '../../util/testUtils';
import { mockExpiredJwt, mockInvalidSignatureJwt, mockJwt, mockJwtPayload, mockMalformedJwt } from '../../fixtures';
import { server } from '../../mock-server';
import { AuthErrorReason, AuthStatus } from '../authStatus';
import {
authenticateRequest,
Expand Down Expand Up @@ -386,31 +379,31 @@ describe('tokens.getOrganizationSyncTarget(url,options)', _ => {
});

describe('tokens.authenticateRequest(options)', () => {
let fakeClock;
let fakeFetch;

beforeEach(() => {
fakeClock = sinon.useFakeTimers(new Date(mockJwtPayload.iat * 1000).getTime());
fakeFetch = sinon.stub(runtime, 'fetch');
fakeFetch.onCall(0).returns(jsonOk(mockJwks));
// the refresh token flow calls verify twice, so we need to support two calls
fakeFetch.onCall(1).returns(jsonOk(mockJwks));
vi.useFakeTimers();
vi.setSystemTime(new Date(mockJwtPayload.iat * 1000).getTime());
});

afterEach(() => {
fakeClock.restore();
fakeFetch.restore();
sinon.restore();
});
afterEach(() => {});

//
// HTTP Authorization exists
//

test('returns signed out state if jwk fails to load from remote', async () => {
fakeFetch.onCall(0).returns(jsonOk({}));
test.only('returns signed out state if jwk fails to load from remote', async () => {
server.use(
http.get('https://api.clerk.test/v1/jwks', () => {
return new HttpResponse('{}', { status: 200 });
}),
);

try {
const requestState = await authenticateRequest(mockRequestWithHeaderAuth(), mockOptions());

const requestState = await authenticateRequest(mockRequestWithHeaderAuth(), mockOptions());
console.log('requestState', requestState);
} catch (e) {
console.log('e', e);
}

const errMessage =
'The JWKS endpoint did not contain any signing keys. Contact [email protected]. Contact [email protected] (reason=jwk-remote-failed-to-load, token-carrier=header)';
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/vitest.setup.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ afterEach(() => server.resetHandlers());
afterAll(() => server.close());

server.events.on('request:start', ({ request }) => {
console.log('MSW intercepted:', request.method, request.url);
// FOR DEBUGGING
// console.log('MSW intercepted:', request.method, request.url);
});

globalThis.PACKAGE_NAME = '@clerk/backend';
Expand Down

0 comments on commit 9cab1f0

Please sign in to comment.