extend cookie

This commit is contained in:
peintnermax
2024-03-18 16:30:44 +01:00
parent 7cd59d8c6b
commit e06bf4bd85
4 changed files with 57 additions and 16 deletions

View File

@@ -64,9 +64,7 @@ export async function PUT(request: NextRequest) {
.then((recent) => { .then((recent) => {
console.log("setsession", webAuthN); console.log("setsession", webAuthN);
return setSessionAndUpdateCookie( return setSessionAndUpdateCookie(
recent.id, recent,
recent.token,
recent.loginName,
password, password,
webAuthN, webAuthN,
challenges, challenges,

View File

@@ -115,12 +115,22 @@ export async function createSession(
{ {
checks: { user: { loginName }, password: { password } }, checks: { user: { loginName }, password: { password } },
challenges, challenges,
lifetime: {
seconds: 300,
nanos: 0,
},
}, },
{} {}
) )
: sessionService.createSession( : sessionService.createSession(
{ checks: { user: { loginName } }, challenges }, {
checks: { user: { loginName } },
challenges,
lifetime: {
seconds: 300,
nanos: 0,
},
},
{} {}
); );
} }
@@ -137,6 +147,10 @@ export async function createSessionForUserIdAndIdpIntent(
return sessionService.createSession( return sessionService.createSession(
{ {
checks: { user: { userId }, idpIntent }, checks: { user: { userId }, idpIntent },
lifetime: {
seconds: 300,
nanos: 0,
},
}, },
{} {}
); );

View File

@@ -6,6 +6,8 @@ export type SessionCookie = {
id: string; id: string;
token: string; token: string;
loginName: string; loginName: string;
creationDate: string;
expirationDate: string;
changeDate: string; changeDate: string;
authRequestId?: string; // if its linked to an OIDC flow authRequestId?: string; // if its linked to an OIDC flow
}; };
@@ -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 cookiesList = cookies();
const stringifiedCookie = cookiesList.get("sessions"); const stringifiedCookie = cookiesList.get("sessions");
if (stringifiedCookie?.value) { if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(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 { } else {
return []; 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 cookiesList = cookies();
const stringifiedCookie = cookiesList.get("sessions"); const stringifiedCookie = cookiesList.get("sessions");
if (stringifiedCookie?.value) { if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value); const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
return sessions; return sessions.filter((session) =>
cleanup ? new Date(session.expirationDate) > new Date() : true
);
} else { } else {
return []; return [];
} }

View File

@@ -35,6 +35,8 @@ export async function createSessionAndUpdateCookie(
const sessionCookie: SessionCookie = { const sessionCookie: SessionCookie = {
id: createdSession.sessionId, id: createdSession.sessionId,
token: createdSession.sessionToken, token: createdSession.sessionToken,
creationDate: response.session.creationDate?.toString() ?? "",
expirationDate: (response.session.expirationDate ?? "")?.toString(),
changeDate: response.session.changeDate?.toString() ?? "", changeDate: response.session.changeDate?.toString() ?? "",
loginName: response.session?.factors?.user?.loginName ?? "", loginName: response.session?.factors?.user?.loginName ?? "",
}; };
@@ -79,6 +81,8 @@ export async function createSessionForIdpAndUpdateCookie(
const sessionCookie: SessionCookie = { const sessionCookie: SessionCookie = {
id: createdSession.sessionId, id: createdSession.sessionId,
token: createdSession.sessionToken, token: createdSession.sessionToken,
creationDate: response.session.creationDate?.toString() ?? "",
expirationDate: (response.session.expirationDate ?? "")?.toString(),
changeDate: response.session.changeDate?.toString() ?? "", changeDate: response.session.changeDate?.toString() ?? "",
loginName: response.session?.factors?.user?.loginName ?? "", loginName: response.session?.factors?.user?.loginName ?? "",
}; };
@@ -104,9 +108,7 @@ export type SessionWithChallenges = Session & {
}; };
export async function setSessionAndUpdateCookie( export async function setSessionAndUpdateCookie(
sessionId: string, recentCookie: SessionCookie,
sessionToken: string,
loginName: string,
password: string | undefined, password: string | undefined,
webAuthN: { credentialAssertionData: any } | undefined, webAuthN: { credentialAssertionData: any } | undefined,
challenges: RequestChallenges | undefined, challenges: RequestChallenges | undefined,
@@ -114,18 +116,20 @@ export async function setSessionAndUpdateCookie(
): Promise<SessionWithChallenges> { ): Promise<SessionWithChallenges> {
return setSession( return setSession(
server, server,
sessionId, recentCookie.id,
sessionToken, recentCookie.token,
password, password,
webAuthN, webAuthN,
challenges challenges
).then((updatedSession) => { ).then((updatedSession) => {
if (updatedSession) { if (updatedSession) {
const sessionCookie: SessionCookie = { const sessionCookie: SessionCookie = {
id: sessionId, id: recentCookie.id,
token: updatedSession.sessionToken, token: updatedSession.sessionToken,
creationDate: recentCookie.creationDate,
expirationDate: recentCookie.expirationDate,
changeDate: updatedSession.details?.changeDate?.toString() ?? "", changeDate: updatedSession.details?.changeDate?.toString() ?? "",
loginName: loginName, loginName: recentCookie.loginName,
}; };
if (authRequestId) { if (authRequestId) {
@@ -144,6 +148,8 @@ export async function setSessionAndUpdateCookie(
const newCookie: SessionCookie = { const newCookie: SessionCookie = {
id: sessionCookie.id, id: sessionCookie.id,
token: updatedSession.sessionToken, token: updatedSession.sessionToken,
creationDate: sessionCookie.creationDate,
expirationDate: sessionCookie.expirationDate,
changeDate: session.changeDate?.toString() ?? "", changeDate: session.changeDate?.toString() ?? "",
loginName: session.factors?.user?.loginName ?? "", loginName: session.factors?.user?.loginName ?? "",
}; };