Skip to content

Commit b20d02a

Browse files
linting fixes
1 parent 94bd7d0 commit b20d02a

File tree

4 files changed

+103
-80
lines changed

4 files changed

+103
-80
lines changed

src/server/auth-client.test.ts

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
394394
// When a route doesn't match, the handler returns a NextResponse.next() with status 200
395395
expect(response.status).toBe(200);
396396
});
397-
397+
398398
it("should use the default value (true) for enableAccessTokenEndpoint when not explicitly provided", async () => {
399399
const secret = await generateSecret(32);
400400
const transactionStore = new TransactionStore({
@@ -4374,53 +4374,65 @@ ca/T0LLtgmbMmxSv/MmzIg==
43744374
const authClient = await createAuthClient({
43754375
signInReturnToPath: defaultReturnTo
43764376
});
4377-
4377+
43784378
// Mock the transactionStore.save method to verify the saved state
4379-
const originalSave = authClient['transactionStore'].save;
4380-
authClient['transactionStore'].save = vi.fn(async (cookies, state) => {
4379+
const originalSave = authClient["transactionStore"].save;
4380+
authClient["transactionStore"].save = vi.fn(async (cookies, state) => {
43814381
expect(state.returnTo).toBe(defaultReturnTo);
4382-
return originalSave.call(authClient['transactionStore'], cookies, state);
4382+
return originalSave.call(
4383+
authClient["transactionStore"],
4384+
cookies,
4385+
state
4386+
);
43834387
});
43844388

43854389
await authClient.startInteractiveLogin();
4386-
4387-
expect(authClient['transactionStore'].save).toHaveBeenCalled();
4390+
4391+
expect(authClient["transactionStore"].save).toHaveBeenCalled();
43884392
});
43894393

43904394
it("should sanitize and use the provided returnTo parameter", async () => {
43914395
const authClient = await createAuthClient();
43924396
const returnTo = "/custom-return-path";
4393-
4397+
43944398
// Mock the transactionStore.save method to verify the saved state
4395-
const originalSave = authClient['transactionStore'].save;
4396-
authClient['transactionStore'].save = vi.fn(async (cookies, state) => {
4399+
const originalSave = authClient["transactionStore"].save;
4400+
authClient["transactionStore"].save = vi.fn(async (cookies, state) => {
43974401
// The full URL is saved, not just the path
43984402
expect(state.returnTo).toBe("https://example.com/custom-return-path");
4399-
return originalSave.call(authClient['transactionStore'], cookies, state);
4403+
return originalSave.call(
4404+
authClient["transactionStore"],
4405+
cookies,
4406+
state
4407+
);
44004408
});
44014409

44024410
await authClient.startInteractiveLogin({ returnTo });
4403-
4404-
expect(authClient['transactionStore'].save).toHaveBeenCalled();
4411+
4412+
expect(authClient["transactionStore"].save).toHaveBeenCalled();
44054413
});
44064414

44074415
it("should reject unsafe returnTo URLs", async () => {
44084416
const authClient = await createAuthClient({
44094417
signInReturnToPath: "/safe-path"
44104418
});
44114419
const unsafeReturnTo = "https://malicious-site.com";
4412-
4420+
44134421
// Mock the transactionStore.save method to verify the saved state
4414-
const originalSave = authClient['transactionStore'].save;
4415-
authClient['transactionStore'].save = vi.fn(async (cookies, state) => {
4422+
const originalSave = authClient["transactionStore"].save;
4423+
authClient["transactionStore"].save = vi.fn(async (cookies, state) => {
44164424
// Should use the default safe path instead of the malicious one
44174425
expect(state.returnTo).toBe("/safe-path");
4418-
return originalSave.call(authClient['transactionStore'], cookies, state);
4426+
return originalSave.call(
4427+
authClient["transactionStore"],
4428+
cookies,
4429+
state
4430+
);
44194431
});
44204432

44214433
await authClient.startInteractiveLogin({ returnTo: unsafeReturnTo });
4422-
4423-
expect(authClient['transactionStore'].save).toHaveBeenCalled();
4434+
4435+
expect(authClient["transactionStore"].save).toHaveBeenCalled();
44244436
});
44254437

44264438
it("should pass authorization parameters to the authorization URL", async () => {
@@ -4429,10 +4441,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
44294441
audience: "https://api.example.com",
44304442
scope: "openid profile email custom_scope"
44314443
};
4432-
4444+
44334445
// Spy on the authorizationUrl method to verify the passed params
4434-
const originalAuthorizationUrl = authClient['authorizationUrl'];
4435-
authClient['authorizationUrl'] = vi.fn(async (params) => {
4446+
const originalAuthorizationUrl = authClient["authorizationUrl"];
4447+
authClient["authorizationUrl"] = vi.fn(async (params) => {
44364448
// Verify the audience is set correctly
44374449
expect(params.get("audience")).toBe(authorizationParameters.audience);
44384450
// Verify the scope is set correctly
@@ -4441,8 +4453,8 @@ ca/T0LLtgmbMmxSv/MmzIg==
44414453
});
44424454

44434455
await authClient.startInteractiveLogin({ authorizationParameters });
4444-
4445-
expect(authClient['authorizationUrl']).toHaveBeenCalled();
4456+
4457+
expect(authClient["authorizationUrl"]).toHaveBeenCalled();
44464458
});
44474459

44484460
it("should handle pushed authorization requests (PAR) correctly", async () => {
@@ -4452,11 +4464,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
44524464
parRequestCalled = true;
44534465
}
44544466
});
4455-
4467+
44564468
const secret = await generateSecret(32);
44574469
const transactionStore = new TransactionStore({ secret });
44584470
const sessionStore = new StatelessSessionStore({ secret });
4459-
4471+
44604472
const authClient = new AuthClient({
44614473
transactionStore,
44624474
sessionStore,
@@ -4471,33 +4483,41 @@ ca/T0LLtgmbMmxSv/MmzIg==
44714483
},
44724484
fetch: mockFetch
44734485
});
4474-
4486+
44754487
await authClient.startInteractiveLogin();
4476-
4488+
44774489
// Verify that PAR was used
44784490
expect(parRequestCalled).toBe(true);
44794491
});
4480-
4492+
44814493
it("should save the transaction state with correct values", async () => {
44824494
const authClient = await createAuthClient();
44834495
const returnTo = "/custom-path";
4484-
4496+
44854497
// Instead of mocking the oauth functions, we'll just check the structure of the transaction state
4486-
const originalSave = authClient['transactionStore'].save;
4487-
authClient['transactionStore'].save = vi.fn(async (cookies, transactionState) => {
4488-
expect(transactionState).toEqual(expect.objectContaining({
4489-
nonce: expect.any(String),
4490-
codeVerifier: expect.any(String),
4491-
responseType: "code",
4492-
state: expect.any(String),
4493-
returnTo: "https://example.com/custom-path"
4494-
}));
4495-
return originalSave.call(authClient['transactionStore'], cookies, transactionState);
4496-
});
4498+
const originalSave = authClient["transactionStore"].save;
4499+
authClient["transactionStore"].save = vi.fn(
4500+
async (cookies, transactionState) => {
4501+
expect(transactionState).toEqual(
4502+
expect.objectContaining({
4503+
nonce: expect.any(String),
4504+
codeVerifier: expect.any(String),
4505+
responseType: "code",
4506+
state: expect.any(String),
4507+
returnTo: "https://example.com/custom-path"
4508+
})
4509+
);
4510+
return originalSave.call(
4511+
authClient["transactionStore"],
4512+
cookies,
4513+
transactionState
4514+
);
4515+
}
4516+
);
44974517

44984518
await authClient.startInteractiveLogin({ returnTo });
4499-
4500-
expect(authClient['transactionStore'].save).toHaveBeenCalled();
4519+
4520+
expect(authClient["transactionStore"].save).toHaveBeenCalled();
45014521
});
45024522

45034523
it("should merge configuration authorizationParameters with method arguments", async () => {
@@ -4509,13 +4529,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
45094529
audience: configAudience
45104530
}
45114531
});
4512-
4532+
45134533
const methodScope = "openid profile email custom_scope";
45144534
const methodAudience = "https://custom-api.example.com";
4515-
4535+
45164536
// Spy on the authorizationUrl method to verify the passed params
4517-
const originalAuthorizationUrl = authClient['authorizationUrl'];
4518-
authClient['authorizationUrl'] = vi.fn(async (params) => {
4537+
const originalAuthorizationUrl = authClient["authorizationUrl"];
4538+
authClient["authorizationUrl"] = vi.fn(async (params) => {
45194539
// Method's authorization parameters should override config
45204540
expect(params.get("audience")).toBe(methodAudience);
45214541
expect(params.get("scope")).toBe(methodScope);
@@ -4528,14 +4548,14 @@ ca/T0LLtgmbMmxSv/MmzIg==
45284548
audience: methodAudience
45294549
}
45304550
});
4531-
4532-
expect(authClient['authorizationUrl']).toHaveBeenCalled();
4551+
4552+
expect(authClient["authorizationUrl"]).toHaveBeenCalled();
45334553
});
45344554

45354555
// Add tests for handleLogin method
45364556
it("should create correct options in handleLogin with returnTo parameter", async () => {
45374557
const authClient = await createAuthClient();
4538-
4558+
45394559
// Mock startInteractiveLogin to check what options are passed to it
45404560
const originalStartInteractiveLogin = authClient.startInteractiveLogin;
45414561
authClient.startInteractiveLogin = vi.fn(async (options) => {
@@ -4546,19 +4566,21 @@ ca/T0LLtgmbMmxSv/MmzIg==
45464566
return originalStartInteractiveLogin.call(authClient, options);
45474567
});
45484568

4549-
const reqUrl = new URL("https://example.com/auth/login?foo=bar&returnTo=custom-return");
4569+
const reqUrl = new URL(
4570+
"https://example.com/auth/login?foo=bar&returnTo=custom-return"
4571+
);
45504572
const req = new NextRequest(reqUrl, { method: "GET" });
4551-
4573+
45524574
await authClient.handleLogin(req);
4553-
4575+
45544576
expect(authClient.startInteractiveLogin).toHaveBeenCalled();
45554577
});
45564578

45574579
it("should handle PAR correctly in handleLogin by not forwarding params", async () => {
45584580
const authClient = await createAuthClient({
45594581
pushedAuthorizationRequests: true
45604582
});
4561-
4583+
45624584
// Mock startInteractiveLogin to check what options are passed to it
45634585
const originalStartInteractiveLogin = authClient.startInteractiveLogin;
45644586
authClient.startInteractiveLogin = vi.fn(async (options) => {
@@ -4569,11 +4591,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
45694591
return originalStartInteractiveLogin.call(authClient, options);
45704592
});
45714593

4572-
const reqUrl = new URL("https://example.com/auth/login?foo=bar&returnTo=custom-return");
4594+
const reqUrl = new URL(
4595+
"https://example.com/auth/login?foo=bar&returnTo=custom-return"
4596+
);
45734597
const req = new NextRequest(reqUrl, { method: "GET" });
4574-
4598+
45754599
await authClient.handleLogin(req);
4576-
4600+
45774601
expect(authClient.startInteractiveLogin).toHaveBeenCalled();
45784602
});
45794603
});

src/server/auth-client.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import packageJson from "../../package.json";
66
import {
77
AccessTokenError,
88
AccessTokenErrorCode,
9+
AccessTokenForConnectionError,
10+
AccessTokenForConnectionErrorCode,
911
AuthorizationCodeGrantError,
1012
AuthorizationError,
1113
BackchannelLogoutError,
1214
DiscoveryError,
13-
AccessTokenForConnectionError,
14-
AccessTokenForConnectionErrorCode,
1515
InvalidStateError,
1616
MissingStateError,
1717
OAuth2Error,
1818
SdkError
1919
} from "../errors";
2020
import {
21+
AccessTokenForConnectionOptions,
2122
AuthorizationParameters,
2223
ConnectionTokenSet,
23-
AccessTokenForConnectionOptions,
2424
LogoutToken,
2525
SessionData,
2626
StartInteractiveLoginOptions,
@@ -65,7 +65,6 @@ const DEFAULT_SCOPES = ["openid", "profile", "email", "offline_access"].join(
6565
" "
6666
);
6767

68-
6968
/**
7069
* A constant representing the grant type for federated connection access token exchange.
7170
*
@@ -1016,19 +1015,20 @@ export class AuthClient {
10161015
tokenSet: TokenSet,
10171016
connectionTokenSet: ConnectionTokenSet | undefined,
10181017
options: AccessTokenForConnectionOptions
1019-
): Promise<[AccessTokenForConnectionError, null] | [null, ConnectionTokenSet]> {
1018+
): Promise<
1019+
[AccessTokenForConnectionError, null] | [null, ConnectionTokenSet]
1020+
> {
10201021
// If we do not have a refresh token
10211022
// and we do not have a connection token set in the cache or the one we have is expired,
10221023
// there is noting to retrieve and we return an error.
10231024
if (
10241025
!tokenSet.refreshToken &&
1025-
(!connectionTokenSet ||
1026-
connectionTokenSet.expiresAt <= Date.now() / 1000)
1026+
(!connectionTokenSet || connectionTokenSet.expiresAt <= Date.now() / 1000)
10271027
) {
10281028
return [
10291029
new AccessTokenForConnectionError(
10301030
AccessTokenForConnectionErrorCode.MISSING_REFRESH_TOKEN,
1031-
"A refresh token was not present, Connection Access Token requires a refresh token. The user needs to re-authenticate.",
1031+
"A refresh token was not present, Connection Access Token requires a refresh token. The user needs to re-authenticate."
10321032
),
10331033
null
10341034
];
@@ -1039,8 +1039,7 @@ export class AuthClient {
10391039
// we need to exchange the refresh token for a connection access token.
10401040
if (
10411041
tokenSet.refreshToken &&
1042-
(!connectionTokenSet ||
1043-
connectionTokenSet.expiresAt <= Date.now() / 1000)
1042+
(!connectionTokenSet || connectionTokenSet.expiresAt <= Date.now() / 1000)
10441043
) {
10451044
const params = new URLSearchParams();
10461045

@@ -1111,10 +1110,7 @@ export class AuthClient {
11111110
];
11121111
}
11131112

1114-
return [null, connectionTokenSet] as [
1115-
null,
1116-
ConnectionTokenSet
1117-
];
1113+
return [null, connectionTokenSet] as [null, ConnectionTokenSet];
11181114
}
11191115
}
11201116

src/server/session/stateful-session-store.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ describe("Stateful Session Store", async () => {
690690
});
691691
});
692692

693-
694693
it("should remove the legacy cookie if it exists", async () => {
695694
const currentTime = Date.now();
696695
const createdAt = Math.floor(currentTime / 1000);
@@ -718,7 +717,7 @@ describe("Stateful Session Store", async () => {
718717

719718
const sessionStore = new StatefulSessionStore({
720719
secret,
721-
store,
720+
store
722721
});
723722

724723
vi.spyOn(requestCookies, "has").mockReturnValue(true);

0 commit comments

Comments
 (0)