Skip to content

Commit 259af96

Browse files
committed
Fix creating Session from tokens
1 parent 071d4b6 commit 259af96

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

packages/node/src/Session.static.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { validate } from "uuid";
2525
import type * as Jose from "jose";
2626
import type * as OpenIdClient from "openid-client";
2727
import { Session } from "./Session";
28+
import { randomUUID } from "crypto";
2829

2930
// Camelcase identifiers are required in the OIDC specification.
3031
/* eslint-disable camelcase*/
@@ -166,9 +167,10 @@ describe("Session static functions", () => {
166167

167168
describe("Session.fromTokens", () => {
168169
it("creates a session with the provided tokens", async () => {
170+
const mockedAccessToken = randomUUID();
169171
const mockedIdToken = await mockIdToken({});
170172
const tokenSet: SessionTokenSet = {
171-
accessToken: "access.token.jwt",
173+
accessToken: mockedAccessToken,
172174
idToken: mockedIdToken,
173175
clientId: mockClientId,
174176
issuer: mockOpConfig().issuer,
@@ -195,7 +197,9 @@ describe("Session static functions", () => {
195197
}) as typeof fetch;
196198
await session.fetch("https://some.resource");
197199
// @ts-expect-error We know headers is initialized.
198-
expect(headers.get("Authorization")).toBe(`Bearer ${mockedIdToken}`);
200+
expect(headers.get("Authorization")).toBe(
201+
`Bearer ${mockedAccessToken}`,
202+
);
199203
globalThis.fetch = globalFetch;
200204
});
201205

@@ -207,9 +211,10 @@ describe("Session static functions", () => {
207211
publicKey: await exportJWK(dpopKeyPair.publicKey),
208212
};
209213

214+
const mockedAccessToken = randomUUID();
210215
const mockedIdToken = await mockIdToken({});
211216
const tokenSet: SessionTokenSet = {
212-
accessToken: "access.token.jwt",
217+
accessToken: mockedAccessToken,
213218
idToken: mockedIdToken,
214219
clientId: mockClientId,
215220
issuer: mockOpConfig().issuer,
@@ -230,7 +235,7 @@ describe("Session static functions", () => {
230235
}) as typeof fetch;
231236
await session.fetch("https://some.resource");
232237
// @ts-expect-error We know headers is initialized.
233-
expect(headers.get("Authorization")).toBe(`DPoP ${mockedIdToken}`);
238+
expect(headers.get("Authorization")).toBe(`DPoP ${mockedAccessToken}`);
234239
// @ts-expect-error We know headers is initialized.
235240
expect(headers.get("dpop")).not.toBeNull();
236241
globalThis.fetch = globalFetch;

packages/node/src/Session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,12 @@ export class Session implements IHasSessionEventListener {
284284
if (
285285
isExpired ||
286286
webId === undefined ||
287-
sessionTokenSet.idToken === undefined
287+
sessionTokenSet.accessToken === undefined
288288
) {
289289
return session;
290290
}
291291

292-
const fetch = buildAuthenticatedFetch(sessionTokenSet.idToken, {
292+
const fetch = buildAuthenticatedFetch(sessionTokenSet.accessToken, {
293293
dpopKey: sessionTokenSet.dpopKey,
294294
expiresIn: sessionTokenSet.expiresAt
295295
? sessionTokenSet.expiresAt - Date.now()

0 commit comments

Comments
 (0)