Skip to content

bugfix/delete redundant txn cookies #2221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions src/server/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@
: {},
returnTo: searchParams.returnTo
};
return this.startInteractiveLogin(options);
const requestCookies = req.cookies;
const interactiveLoginResponse = await this.startInteractiveLogin(options);
this.transactionStore.deleteAll(requestCookies, interactiveLoginResponse);

Check failure on line 423 in src/server/auth-client.ts

View workflow job for this annotation

GitHub Actions / Build Package

Argument of type 'NextResponse<unknown>' is not assignable to parameter of type 'ResponseCookies'.

Check failure on line 423 in src/server/auth-client.ts

View workflow job for this annotation

GitHub Actions / Lint Code

Argument of type 'NextResponse<unknown>' is not assignable to parameter of type 'ResponseCookies'.
return interactiveLoginResponse;
}

async handleLogout(req: NextRequest): Promise<NextResponse> {
Expand Down Expand Up @@ -501,7 +504,7 @@
}
}

// Clean up session and transaction cookies
// Clean up session
await this.sessionStore.delete(req.cookies, logoutResponse.cookies);
addCacheControlHeadersForSession(logoutResponse);

Expand All @@ -524,7 +527,6 @@
if (!transactionStateCookie) {
return this.onCallback(new InvalidStateError(), {}, null);
}

const transactionState = transactionStateCookie.payload;
const onCallbackCtx: OnCallbackContext = {
returnTo: transactionState.returnTo
Expand Down Expand Up @@ -606,34 +608,45 @@
null
);
}
try {
const idTokenClaims = oauth.getValidatedIdTokenClaims(oidcRes)!;
let session: SessionData = {
user: idTokenClaims,
tokenSet: {
accessToken: oidcRes.access_token,
idToken: oidcRes.id_token,
scope: oidcRes.scope,
refreshToken: oidcRes.refresh_token,
expiresAt: Math.floor(Date.now() / 1000) + Number(oidcRes.expires_in)
},
internal: {
sid: idTokenClaims.sid as string,
createdAt: Math.floor(Date.now() / 1000)
}
};

const idTokenClaims = oauth.getValidatedIdTokenClaims(oidcRes)!;
let session: SessionData = {
user: idTokenClaims,
tokenSet: {
accessToken: oidcRes.access_token,
idToken: oidcRes.id_token,
scope: oidcRes.scope,
refreshToken: oidcRes.refresh_token,
expiresAt: Math.floor(Date.now() / 1000) + Number(oidcRes.expires_in)
},
internal: {
sid: idTokenClaims.sid as string,
createdAt: Math.floor(Date.now() / 1000)
}
};

const res = await this.onCallback(null, onCallbackCtx, session);
const res = await this.onCallback(null, onCallbackCtx, session);

// call beforeSessionSaved callback if present
// if not then filter id_token claims with default rules
session = await this.finalizeSession(session, oidcRes.id_token);
// call beforeSessionSaved callback if present
// if not then filter id_token claims with default rules
session = await this.finalizeSession(session, oidcRes.id_token);

await this.sessionStore.set(req.cookies, res.cookies, session, true);
addCacheControlHeadersForSession(res);
await this.transactionStore.delete(res.cookies, state);
await this.sessionStore.set(req.cookies, res.cookies, session, true);
addCacheControlHeadersForSession(res);

return res;
// delete this txn cookie
await this.transactionStore.delete(res.cookies, state);
return res;
} catch (error) {
const errorResponse = await this.onCallback(
error as SdkError,
onCallbackCtx,
null
);
// delete this txn cookie
await this.transactionStore.delete(errorResponse.cookies, state);
return errorResponse;
}
}

async handleProfile(req: NextRequest): Promise<NextResponse> {
Expand Down
4 changes: 3 additions & 1 deletion src/server/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ export function addCacheControlHeadersForSession(res: NextResponse): void {
res.headers.set("Pragma", "no-cache");
res.headers.set("Expires", "0");
}

/**
* Mutates resCookies, to delete cookie by name from resCookies.cookies
*/
export function deleteCookie(resCookies: ResponseCookies, name: string) {
resCookies.set(name, "", {
maxAge: 0 // Ensure the cookie is deleted immediately
Expand Down
Loading
Loading