Skip to content

Commit 15a2c7d

Browse files
VIA-254 Allow session token to be created even if APIM unavailable
1 parent 846863a commit 15a2c7d

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/utils/auth/callbacks/get-token.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ApimHttpError } from "@src/utils/auth/apim/exceptions";
12
import { getOrRefreshApimCredentials } from "@src/utils/auth/apim/get-or-refresh-apim-credentials";
23
import { getToken } from "@src/utils/auth/callbacks/get-token";
34
import { MaxAgeInSeconds } from "@src/utils/auth/types";
@@ -8,7 +9,6 @@ import { Account, Profile } from "next-auth";
89
import { JWT } from "next-auth/jwt";
910
import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";
1011
import { headers } from "next/headers";
11-
import { ApimHttpError } from "@src/utils/auth/apim/exceptions";
1212

1313
jest.mock("@project/auth", () => ({
1414
auth: jest.fn(),
@@ -172,6 +172,44 @@ describe("getToken", () => {
172172

173173
expect(result).toBeNull();
174174
});
175+
176+
it("should still return login token even if fetching APIM credentials fails", async () => {
177+
(getOrRefreshApimCredentials as jest.Mock).mockRejectedValue(new ApimHttpError("Error getting APIM token"));
178+
179+
(jwtDecode as jest.Mock).mockReturnValue({
180+
jti: "jti_test",
181+
});
182+
const token = { apim: {}, nhs_login: { id_token: "id-token" } } as JWT;
183+
184+
const account = {
185+
expires_at: nowInSeconds + 1000,
186+
access_token: "newAccess",
187+
refresh_token: "newRefresh",
188+
id_token: "newIdToken",
189+
} as Account;
190+
191+
const profile = {
192+
nhs_number: "test_nhs_number",
193+
};
194+
195+
const maxAgeInSeconds = 600 as MaxAgeInSeconds;
196+
197+
const result = await getToken(token, account, profile, mockConfig, maxAgeInSeconds);
198+
199+
expect(result).toMatchObject({
200+
user: {
201+
nhs_number: profile.nhs_number,
202+
},
203+
nhs_login: {
204+
id_token: "newIdToken",
205+
},
206+
apim: {
207+
access_token: "",
208+
expires_at: 0,
209+
},
210+
fixedExpiry: nowInSeconds + maxAgeInSeconds,
211+
});
212+
});
175213
});
176214

177215
describe("when AUTH APIM is not available", () => {

src/utils/auth/callbacks/get-token.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,21 @@ const getToken = async (
4545
return null;
4646
}
4747

48-
// TODO VIA-254 - can we do this only once? https://www.youtube.com/watch?v=A4I9DMSvJxg
49-
const apimAccessCredentials = await getOrRefreshApimCredentials(config, token, nowInSeconds);
48+
let apimAccessCredentials = undefined;
49+
50+
try {
51+
// TODO VIA-254 - can we do this only once? https://www.youtube.com/watch?v=A4I9DMSvJxg
52+
apimAccessCredentials = await getOrRefreshApimCredentials(config, token, nowInSeconds);
53+
} catch (error) {
54+
let errorMessage = undefined;
55+
if (error instanceof Error) {
56+
errorMessage = error.message;
57+
}
58+
log.error(
59+
{ context: { errorMessage: errorMessage } },
60+
"Error fetching APIM credentials; continuing to create session with empty APIM fields",
61+
);
62+
}
5063

5164
// Inspect the token (which was either returned from login or fetched from session), fill missing or blank values with defaults
5265
let updatedToken: JWT = fillMissingFieldsInTokenWithDefaultValues(token, apimAccessCredentials);

0 commit comments

Comments
 (0)