Skip to content

Commit

Permalink
Adjust node mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Jan 7, 2025
1 parent 9cd02b0 commit 65c2a2b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ClientRegistrarResponse: IClient = {
clientId: "abcde",
clientSecret: "12345",
clientType: "dynamic",
// This date is set 3000 years from now, so it expiring unexpectedly should not be a problem for us.
// 5000-01-07
expiresAt: 95618140501,
};

Expand Down
38 changes: 22 additions & 16 deletions packages/node/src/login/oidc/ClientRegistrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
IIssuerConfig,
IClient,
IClientRegistrarOptions,
IOpenIdDynamicClient,
} from "@inrupt/solid-client-authn-core";
import {
ConfigurationError,
Expand Down Expand Up @@ -142,28 +143,33 @@ export default class ClientRegistrar implements IClientRegistrar {
grant_types: ["authorization_code", "refresh_token"],
});

const persistedClientMetadata: Record<string, string> = {
let persistedClientMetadata: IOpenIdDynamicClient = {
clientId: registeredClient.metadata.client_id,
idTokenSignedResponseAlg:
registeredClient.metadata.id_token_signed_response_alg ?? signingAlg,
clientType: "dynamic",
};
if (registeredClient.metadata.client_secret !== undefined) {
persistedClientMetadata.clientSecret =
registeredClient.metadata.client_secret;
}

await this.storageUtility.setForUser(

Check failure on line 152 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Delete `⏎······`
options.sessionId,
persistedClientMetadata,
options.sessionId, {
clientId: persistedClientMetadata.clientId,

Check failure on line 154 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Delete `··`
idTokenSignedResponseAlg: persistedClientMetadata.idTokenSignedResponseAlg!,

Check failure on line 155 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Replace `··idTokenSignedResponseAlg:` with `idTokenSignedResponseAlg:⏎·······`
clientType: "dynamic",

Check failure on line 156 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Delete `··`
}

Check failure on line 157 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Replace `··}⏎····` with `}`
);
return {
clientId: persistedClientMetadata.clientId,
clientSecret: persistedClientMetadata.clientSecret,
idTokenSignedResponseAlg:
persistedClientMetadata.idTokenSignedResponseAlg,
clientName: registeredClient.metadata.client_name as string | undefined,
clientType: "dynamic",
};
if (registeredClient.metadata.client_secret !== undefined) {
persistedClientMetadata = {
...persistedClientMetadata,
clientSecret: registeredClient.metadata.client_secret,
// If a client secret is present, it has an expiration date.
expiresAt: registeredClient.metadata.client_secret_expires_at as number

Check failure on line 164 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Insert `,`
}

Check failure on line 165 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Insert `;`
await this.storageUtility.setForUser(

Check failure on line 166 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Delete `⏎········`
options.sessionId, {
clientSecret: persistedClientMetadata.clientSecret,

Check failure on line 168 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Replace `··········` with `········`
expiresAt: String(persistedClientMetadata.expiresAt),

Check failure on line 169 in packages/node/src/login/oidc/ClientRegistrar.ts

View workflow job for this annotation

GitHub Actions / lint / lint

Delete `··`
}
);
}
return persistedClientMetadata;
}
}
22 changes: 10 additions & 12 deletions packages/node/src/login/oidc/__mocks__/ClientRegistrar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ import type {
import { jest } from "@jest/globals";
import type { ClientMetadata } from "openid-client";

export const ClientRegistrarResponse: IClient = {
clientId: "abcde",
clientSecret: "12345",
clientType: "dynamic",
export const mockDefaultClient = (): IClient => {
return {
clientId: "a client id",
clientSecret: "a client secret",
clientType: "dynamic",
// 5000-01-07
expiresAt: 95618140501,
};
};

export const ClientRegistrarResponse: IClient = mockDefaultClient();

export const PublicClientRegistrarResponse: IClient = {
clientId: "abcde",
clientType: "dynamic",
Expand Down Expand Up @@ -76,14 +82,6 @@ export const mockClientConfig = (
};
};

export const mockDefaultClient = (): IClient => {
return {
clientId: "a client id",
clientSecret: "a client secret",
clientType: "dynamic",
};
};

export const mockDefaultClientRegistrar = (): IClientRegistrar => {
return {
getClient: async () => mockDefaultClient(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { standardOidcOptions } from "../__mocks__/IOidcOptions";
import ClientCredentialsOidcHandler from "./ClientCredentialsOidcHandler";

import { mockDefaultIssuerConfig } from "../__mocks__/IssuerConfigFetcher";
import { mockDefaultClient } from "../__mocks__/ClientRegistrar";

jest.mock("openid-client");

Expand Down Expand Up @@ -198,11 +199,7 @@ describe("ClientCredentialsOidcHandler", () => {
await expect(
clientCredentialsOidcHandler.canHandle({
...standardOidcOptions,
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
).resolves.toBe(false);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import {
mockDefaultOidcOptions,
mockOidcOptions,
} from "../__mocks__/IOidcOptions";
import {
mockDefaultClient
} from "../__mocks__/ClientRegistrar";
import RefreshTokenOidcHandler from "./RefreshTokenOidcHandler";
import {
mockDefaultTokenRefresher,
Expand Down Expand Up @@ -79,11 +82,7 @@ describe("RefreshTokenOidcHandler", () => {
refreshTokenOidcHandler.canHandle(
mockOidcOptions({
refreshToken: undefined,
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
),
).resolves.toBe(false);
Expand Down Expand Up @@ -122,11 +121,7 @@ describe("RefreshTokenOidcHandler", () => {
refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: undefined,
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
),
).rejects.toThrow("missing one of 'refreshToken', 'clientId'");
Expand All @@ -144,11 +139,7 @@ describe("RefreshTokenOidcHandler", () => {
const result = await refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
);
expect(result?.webId).toBe("https://my.webid/");
Expand All @@ -173,11 +164,7 @@ describe("RefreshTokenOidcHandler", () => {
const result = (await refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
)) as SolidClientAuthnCore.LoginResult;
expect(result).toBeDefined();
Expand Down Expand Up @@ -213,11 +200,7 @@ describe("RefreshTokenOidcHandler", () => {
const result = await refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
);

Expand Down Expand Up @@ -245,11 +228,7 @@ describe("RefreshTokenOidcHandler", () => {
const result = await refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
dpop: false,
}),
);
Expand All @@ -268,7 +247,9 @@ describe("RefreshTokenOidcHandler", () => {
});

it("stores OIDC context in storage so that refreshing the token succeeds later", async () => {
const exampleClientSecret = randomUUID();
const mockedClient = mockDefaultClient();
mockedClient.clientId = EXAMPLE_CLIENT_ID;
mockedClient.clientName = "some client name";

const mockedStorage = mockStorageUtility({});
const refreshTokenOidcHandler = new RefreshTokenOidcHandler(
Expand All @@ -278,12 +259,7 @@ describe("RefreshTokenOidcHandler", () => {
await refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: EXAMPLE_CLIENT_ID,
clientSecret: exampleClientSecret,
clientName: "some client name",
clientType: "dynamic",
},
client: mockedClient,
}),
);
await expect(
Expand All @@ -297,7 +273,7 @@ describe("RefreshTokenOidcHandler", () => {
mockDefaultOidcOptions().sessionId,
"clientSecret",
),
).resolves.toBe(exampleClientSecret);
).resolves.toBe(mockedClient.clientSecret);
await expect(
mockedStorage.getForUser(
mockDefaultOidcOptions().sessionId,
Expand Down Expand Up @@ -340,11 +316,7 @@ describe("RefreshTokenOidcHandler", () => {
const result = refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
);
await expect(result).rejects.toThrow(
Expand Down Expand Up @@ -372,11 +344,7 @@ describe("RefreshTokenOidcHandler", () => {
const result = await refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: EXAMPLE_CLIENT_ID,
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
);
expect(result?.webId).toBe("https://my.webid/");
Expand Down Expand Up @@ -408,11 +376,7 @@ describe("RefreshTokenOidcHandler", () => {
await refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
eventEmitter: mockEmitter,
}),
);
Expand All @@ -438,11 +402,7 @@ describe("RefreshTokenOidcHandler", () => {
const result = refreshTokenOidcHandler.handle(
mockOidcOptions({
refreshToken: "some refresh token",
client: {
clientId: randomUUID(),
clientSecret: randomUUID(),
clientType: "dynamic",
},
client: mockDefaultClient(),
}),
);
await expect(result).rejects.toThrow("Invalid credentials");
Expand Down

0 comments on commit 65c2a2b

Please sign in to comment.