Skip to content

Commit

Permalink
extend cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
peintnermax committed Mar 18, 2024
1 parent 7cd59d8 commit e06bf4b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
4 changes: 1 addition & 3 deletions apps/login/app/api/session/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ export async function PUT(request: NextRequest) {
.then((recent) => {
console.log("setsession", webAuthN);
return setSessionAndUpdateCookie(
recent.id,
recent.token,
recent.loginName,
recent,
password,
webAuthN,
challenges,
Expand Down
18 changes: 16 additions & 2 deletions apps/login/lib/zitadel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,22 @@ export async function createSession(
{
checks: { user: { loginName }, password: { password } },
challenges,
lifetime: {
seconds: 300,
nanos: 0,
},
},
{}
)
: sessionService.createSession(
{ checks: { user: { loginName } }, challenges },

{
checks: { user: { loginName } },
challenges,
lifetime: {
seconds: 300,
nanos: 0,
},
},
{}
);
}
Expand All @@ -137,6 +147,10 @@ export async function createSessionForUserIdAndIdpIntent(
return sessionService.createSession(
{
checks: { user: { userId }, idpIntent },
lifetime: {
seconds: 300,
nanos: 0,
},
},
{}
);
Expand Down
31 changes: 27 additions & 4 deletions apps/login/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type SessionCookie = {
id: string;
token: string;
loginName: string;
creationDate: string;
expirationDate: string;
changeDate: string;
authRequestId?: string; // if its linked to an OIDC flow
};
Expand Down Expand Up @@ -135,25 +137,46 @@ export async function getSessionCookieByLoginName(
}
}

export async function getAllSessionCookieIds(): Promise<any> {
/**
*
* @param cleanup when true, removes all expired sessions, default true
* @returns Session Cookies
*/
export async function getAllSessionCookieIds(
cleanup: boolean = true
): Promise<any> {
const cookiesList = cookies();
const stringifiedCookie = cookiesList.get("sessions");

if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
return sessions.map((session) => session.id);

return sessions
.filter((session) =>
cleanup ? new Date(session.expirationDate) > new Date() : true
)
.map((session) => session.id);
} else {
return [];
}
}

export async function getAllSessions(): Promise<SessionCookie[]> {
/**
*
* @param cleanup when true, removes all expired sessions, default true
* @returns Session Cookies
*/
export async function getAllSessions(
cleanup: boolean = true
): Promise<SessionCookie[]> {
const cookiesList = cookies();
const stringifiedCookie = cookiesList.get("sessions");

if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
return sessions;
return sessions.filter((session) =>
cleanup ? new Date(session.expirationDate) > new Date() : true
);
} else {
return [];
}
Expand Down
20 changes: 13 additions & 7 deletions apps/login/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export async function createSessionAndUpdateCookie(
const sessionCookie: SessionCookie = {
id: createdSession.sessionId,
token: createdSession.sessionToken,
creationDate: response.session.creationDate?.toString() ?? "",
expirationDate: (response.session.expirationDate ?? "")?.toString(),
changeDate: response.session.changeDate?.toString() ?? "",
loginName: response.session?.factors?.user?.loginName ?? "",
};
Expand Down Expand Up @@ -79,6 +81,8 @@ export async function createSessionForIdpAndUpdateCookie(
const sessionCookie: SessionCookie = {
id: createdSession.sessionId,
token: createdSession.sessionToken,
creationDate: response.session.creationDate?.toString() ?? "",
expirationDate: (response.session.expirationDate ?? "")?.toString(),
changeDate: response.session.changeDate?.toString() ?? "",
loginName: response.session?.factors?.user?.loginName ?? "",
};
Expand All @@ -104,28 +108,28 @@ export type SessionWithChallenges = Session & {
};

export async function setSessionAndUpdateCookie(
sessionId: string,
sessionToken: string,
loginName: string,
recentCookie: SessionCookie,
password: string | undefined,
webAuthN: { credentialAssertionData: any } | undefined,
challenges: RequestChallenges | undefined,
authRequestId: string | undefined
): Promise<SessionWithChallenges> {
return setSession(
server,
sessionId,
sessionToken,
recentCookie.id,
recentCookie.token,
password,
webAuthN,
challenges
).then((updatedSession) => {
if (updatedSession) {
const sessionCookie: SessionCookie = {
id: sessionId,
id: recentCookie.id,
token: updatedSession.sessionToken,
creationDate: recentCookie.creationDate,
expirationDate: recentCookie.expirationDate,
changeDate: updatedSession.details?.changeDate?.toString() ?? "",
loginName: loginName,
loginName: recentCookie.loginName,
};

if (authRequestId) {
Expand All @@ -144,6 +148,8 @@ export async function setSessionAndUpdateCookie(
const newCookie: SessionCookie = {
id: sessionCookie.id,
token: updatedSession.sessionToken,
creationDate: sessionCookie.creationDate,
expirationDate: sessionCookie.expirationDate,
changeDate: session.changeDate?.toString() ?? "",
loginName: session.factors?.user?.loginName ?? "",
};
Expand Down

0 comments on commit e06bf4b

Please sign in to comment.