Skip to content

Commit 6f9def6

Browse files
committed
More changes
1 parent 2752ffe commit 6f9def6

10 files changed

+71
-73
lines changed

jest.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export default {
1515
testPathIgnorePatterns: ['/node_modules/', '/frontend/', '/dist/'],
1616
resetModules: false,
1717
collectCoverage: true,
18-
coverageDirectory: './.out',
18+
coverageDirectory: './build/coverage',
1919
collectCoverageFrom: ['src/**/*.{ts,tsx,js,jsx}', '!src/**/*.d.ts'],
20-
coverageReporters: ['lcov', 'text'],
20+
coverageReporters: ['clover'],
2121
coveragePathIgnorePatterns: ['/dist/', '/node_modules/'],
2222
testTimeout: 60000,
2323
extensionsToTreatAsEsm: ['.ts', '.tsx', '.mts'],

src/auth/authenticator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export abstract class Authenticator {
1616
* @param hostName The base URL for all authentication endpoints.
1717
*/
1818
protected constructor(hostName: string) {
19-
this.hostName = new URL(hostName);
19+
this.hostName = new URL(new URL(hostName).origin);
2020
}
2121

2222
/**

src/auth/client-credentials-authenticator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export class ClientCredentialsAuthenticator extends OAuthAuthenticator {
6565
client,
6666
this.clientAuth,
6767
this.parameters,
68+
{
69+
[oauth.allowInsecureRequests]: process.env.JEST_WORKER_ID !== undefined,
70+
},
6871
);
6972

7073
return oauth.processClientCredentialsResponse(authServer, client, response);

src/auth/oauth-authenticator.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ export abstract class OAuthAuthenticator extends Authenticator {
7474
}
7575
}
7676

77-
/**
78-
* Retrieves the client metadata.
79-
* @returns The client metadata.
80-
*/
81-
public getClient(): oauth.Client {
82-
return this.client;
83-
}
84-
8577
protected abstract performTokenRequest(
8678
authServer: oauth.AuthorizationServer,
8779
client: oauth.Client,

src/auth/openid.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ export class OpenId {
3232
if (!hostname.startsWith('http')) {
3333
hostname = `https://${hostname}`;
3434
}
35-
const issuer = new URL(hostname);
35+
const issuer = new URL(
36+
'/.well-known/openid-configuration',
37+
new URL(hostname).origin,
38+
);
39+
// noinspection JSDeprecatedSymbols
3640
const authServer = await oauth
37-
.discoveryRequest(issuer)
41+
.discoveryRequest(issuer, {
42+
[oauth.allowInsecureRequests]: process.env.JEST_WORKER_ID !== undefined,
43+
})
3844
.then((response) => oauth.processDiscoveryResponse(issuer, response));
3945
return new OpenId(authServer);
4046
}

src/auth/webtoken-authenticator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export class WebTokenAuthenticator extends OAuthAuthenticator {
134134
this.clientAuth,
135135
this.grantType,
136136
parameters,
137+
{
138+
[oauth.allowInsecureRequests]: process.env.JEST_WORKER_ID !== undefined,
139+
},
137140
);
138141

139142
return oauth.processGenericTokenEndpointResponse(

test/auth/client-credentials-authenticator.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ClientCredentialsAuthenticator } from '../../src/auth/client-credentials-authenticator.js';
2-
import { withOauthContainer } from './oauth-authenticator.test.js';
2+
import { withOauthContainer } from './oauth-authenticator-test.js';
33

44
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
55

@@ -10,8 +10,9 @@ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
1010
* refreshes its token and returns the proper Authorization header.
1111
*/
1212
describe('ClientCredentialsAuthenticatorTest', () => {
13-
withOauthContainer((oauthHost: string) => {
13+
withOauthContainer((getOauthHost) => {
1414
test('testRefreshToken', async () => {
15+
const oauthHost = getOauthHost();
1516
await sleep(20);
1617

1718
const authenticator = await ClientCredentialsAuthenticator.builder(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// file: test/auth/oauth-authenticator.test.ts
2+
import { GenericContainer, StartedTestContainer, Wait } from 'testcontainers';
3+
4+
export function withOauthContainer(
5+
defineTests: (getOauthHost: () => string) => void,
6+
): void {
7+
let container: StartedTestContainer;
8+
let oauthHost = '';
9+
10+
beforeAll(async () => {
11+
container = await new GenericContainer(
12+
'ghcr.io/navikt/mock-oauth2-server:2.1.10',
13+
)
14+
.withExposedPorts(8080)
15+
.withWaitStrategy(Wait.forHttp('/', 8080).forStatusCode(405))
16+
.start();
17+
18+
oauthHost = `http://${container.getHost()}:${container.getMappedPort(8080)}`;
19+
}, 30_000);
20+
21+
afterAll(async () => {
22+
await container.stop();
23+
});
24+
25+
describe('with mock OAuth2 server', () => {
26+
defineTests(() => oauthHost); // 👈 defer access to when test runs
27+
});
28+
}

test/auth/oauth-authenticator.test.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/auth/web-token-authenticator.test.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import { generateKeyPair } from 'node:crypto';
22
import { WebTokenAuthenticator } from '../../src/auth/webtoken-authenticator.js';
3-
import { withOauthContainer } from './oauth-authenticator.test.js';
3+
import { withOauthContainer } from './oauth-authenticator-test.js';
44

55
describe('WebTokenAuthenticatorTest', () => {
6-
withOauthContainer((oauthHost: string) => {
7-
const getPrivateKey = (): Promise<string> => {
8-
return new Promise((resolve, reject) => {
9-
generateKeyPair(
10-
'rsa',
11-
{
12-
modulusLength: 2048,
13-
publicKeyEncoding: { type: 'spki', format: 'pem' },
14-
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
15-
},
16-
(err, publicKey, privKey) => {
17-
if (err) {
18-
return reject(err);
19-
}
20-
resolve(privKey);
21-
},
22-
);
23-
});
24-
};
6+
const getPrivateKey = (): Promise<string> => {
7+
return new Promise((resolve, reject) => {
8+
generateKeyPair(
9+
'rsa',
10+
{
11+
modulusLength: 2048,
12+
publicKeyEncoding: { type: 'spki', format: 'pem' },
13+
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
14+
},
15+
(err, publicKey, privKey) => {
16+
if (err) {
17+
return reject(err);
18+
}
19+
resolve(privKey);
20+
},
21+
);
22+
});
23+
};
2524

25+
withOauthContainer((getOauthHost) => {
2626
test('testRefreshToken', async () => {
27+
const oauthHost = getOauthHost();
2728
const authenticator = await WebTokenAuthenticator.builder(
2829
oauthHost,
2930
'1',
@@ -35,7 +36,6 @@ describe('WebTokenAuthenticatorTest', () => {
3536
expect(await authenticator.getAuthToken()).not.toBeFalsy();
3637
const token = await authenticator.refreshToken();
3738
expect(token.access_token).not.toBeFalsy();
38-
const expiresIn = token.expires_in ?? 0;
3939
expect(token.expires_in && token.expires_in > 0).toBe(true);
4040
expect(token.access_token).toBe(await authenticator.getAuthToken());
4141
expect(authenticator.getHost().toString()).toBe(oauthHost + '/');
@@ -45,6 +45,7 @@ describe('WebTokenAuthenticatorTest', () => {
4545
}, 30000);
4646

4747
test('testRefreshTokenWithRS256', async () => {
48+
const oauthHost = getOauthHost();
4849
const authenticator = await WebTokenAuthenticator.builder(
4950
oauthHost,
5051
'1',
@@ -65,6 +66,7 @@ describe('WebTokenAuthenticatorTest', () => {
6566
}, 30000);
6667

6768
test('testRefreshTokenWithExtendedLifetime', async () => {
69+
const oauthHost = getOauthHost();
6870
const authenticator = await WebTokenAuthenticator.builder(
6971
oauthHost,
7072
'1',

0 commit comments

Comments
 (0)