password error, unique loginName

This commit is contained in:
Max Peintner
2023-05-23 09:22:15 +02:00
parent b27c8af65f
commit 5f3696400c
4 changed files with 59 additions and 59 deletions

View File

@@ -0,0 +1 @@
{"type":"serviceaccount","keyId":"215296164894277635","key":"-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAx7HqIdjxIC7pjaA9bH8bec9r63uWrzVvs4GtoxHdJE0+GaaV\nR4ehvXLqVHnLcx6rXgSBwi8HzRJ+1sZSr6nxHVnNjgVK9Ewh08uA0xaHJpwmQH7I\npRiWZ2B1JQ4a7o1S9q+ylHWYpa5b+dm/trySAshhn/1DpEuty6b0DD6hcNsm+Gut\nxkJwPkoe0OK8BC+6b5hd3ISWkHI8k7Ny1zQGNWKkcbcztdBxJbtXRxouiSfcs+FP\ndbCMEXcY8y2ief8/AdBUnfGBr+wJuMnUKEOk7FDub5lkG04+Nzx3G9TAv1poovxu\n63ORWFqbmrUI9ajpZqaO7MjPUkJiRtGrP8EIgQIDAQABAoIBABLSWRABo5B1+3tm\nzANK46sSesiCKIyxC6suoNAK4f4G7cB7K/zG8t6pUx7NL/jUvjApWpWT2zdf8+aI\nYMi/ysMKcM5e/1aRSmrs9mJu8CQaeZyAlqbA74Qk2kW4058CYPxxSPKnNtD+4xpk\nhKZTlat/eXwb4eE9ZM8aGEyDJrI9OwXOKluZaFivxSHV4gH8tUQKl0VX8T1wi8sw\nErKEK0LhV9EY9C9ovZFrUS5SJM4thVJWq/TFbwtgvufGhUiNYA4mpJt2lpUb+2hh\ntcdUWxPIMQ6/xfEEavownMYt/kXZ0pELItAA/lfUe59gRvbwBCkk1kCvrzz3w6Vf\ndak7qbECgYEA6SDd2vUxPUM81PE8FFU3A/ReNORFaTPyOC9w7d+ANg0tSaBhu89Q\n7atP066VBcRihmkTSOwBhZT1EgbbFyPyEY4kWYgkywgWNpslpV4zWdf/YfIA6PV0\nqsYhqGEioCZOBysjcvyEonECMIjhNEV+7r9z2PhivjjNlS8iffM7+h0CgYEA20la\nAvwDUVBWxsLOKF9pI9zVer3X39hYA5Lj0xCy3n6ivPr+oZ6YgVuhvU0JfCQJiIc7\nAt9VGW8hU4gX0Msaie7lw4uzLpt2Jnx14hMfvCytMDJ27JbFJO0/J80bSyrYOg92\nWmuFsT4DG4tYeqmodltloQuRi3TsV4KQbI4nWrUCgYEAnMmJ67RzhSwvQVdsfJBD\nC9nRcekD9ZkGElt1q8VgyPlbt+SxVko7t1w7t7X2a4aEbLlGSwjOjTR9Db6moo1+\nQ+hA8iXp+NJbfiYhz3HnP0lRbGIB0qsh56iRAlkxnFumppOQp8jEuysdud6U9z61\n+4OvOgDzB9PUD70/iU2IzNECgYB3FzcTN9p1ZrQ57cIYmvh2yZAGwONkZhImnres\nAP2jaBLpn0Z6b69TUXhdXmEwUkH7K7YYsUF+NqRawSZt9l/LWTrfIBeH0FYyQJWk\n2c36XCkSa9W6V4dpro8GBpY0FGip34vfP1W0FIGLi/nZZBphbPGcrLO2kSFpxTa6\nPdmRoQKBgQDPAeYf1U/IhUYQlAM/78JuB2pzun3L/PrUWdXvYY5KM/8F2onwbv3S\ny5o9ZwSX8snIeCxXZz2/KNSK1OflW0pZtHegRTjZCsgkiXuEYNh4fnwLGOznl1Mh\nPzUoITq2bR6FGeVrut1IbuyvFmz5oWxnUNoEErtn54Cq2jP1m1nQ8Q==\n-----END RSA PRIVATE KEY-----\n","userId":"215296164893884419"}

View File

@@ -2,25 +2,18 @@ import { listSessions, server } from "#/lib/zitadel";
import Alert from "#/ui/Alert";
import { Avatar } from "#/ui/Avatar";
import { getAllSessionIds } from "#/utils/cookies";
import {
ExclamationTriangleIcon,
XCircleIcon,
} from "@heroicons/react/24/outline";
import { UserPlusIcon, XCircleIcon } from "@heroicons/react/24/outline";
import moment from "moment";
import Link from "next/link";
async function loadSessions() {
const ids = await getAllSessionIds().catch((error) => {
console.log("err", error);
});
const ids = await getAllSessionIds();
if (ids && ids.length) {
return listSessions(
server,
ids.filter((id: string | undefined) => !!id)
).then((sessions) => {
return sessions;
});
);
} else {
return [];
}
@@ -38,7 +31,6 @@ export default async function Page() {
{sessions ? (
sessions.map((session: any, index: number) => {
const validPassword = session.factors.password?.verifiedAt;
console.log(session);
return (
<Link
key={"session-" + index}
@@ -91,6 +83,14 @@ export default async function Page() {
) : (
<Alert>No Sessions available!</Alert>
)}
<Link href="/username">
<div className="flex flex-row items-center py-3 px-4 hover:bg-black/10 dark:hover:bg-white/10 rounded-md">
<div className="w-8 h-8 mr-4 flex flex-row justify-center items-center rounded-full bg-black/5 dark:bg-white/5">
<UserPlusIcon className="h-5 w-5" />
</div>
<span className="text-sm">Add another account</span>
</div>
</Link>
</div>
</div>
);

View File

@@ -45,35 +45,42 @@ export async function PUT(request: NextRequest) {
const { password } = body;
const recent = await getMostRecentSessionCookie();
const session = await setSession(server, recent.id, recent.token, password);
const sessionCookie: SessionCookie = {
id: recent.id,
token: session.sessionToken,
changeDate: session.details.changeDate,
loginName: recent.loginName,
};
return getSession(server, sessionCookie.id, sessionCookie.token).then(
({ session }) => {
const newCookie: SessionCookie = {
id: sessionCookie.id,
token: sessionCookie.token,
changeDate: session.changeDate,
loginName: session.factors.user.loginName,
return setSession(server, recent.id, recent.token, password)
.then((session) => {
const sessionCookie: SessionCookie = {
id: recent.id,
token: session.sessionToken,
changeDate: session.details.changeDate,
loginName: recent.loginName,
};
return updateSessionCookie(sessionCookie.id, sessionCookie)
.then(() => {
console.log("updatedRecent:", sessionCookie);
return NextResponse.json({ factors: session.factors });
})
.catch((error) => {
console.error("errr", error);
return NextResponse.json(error, { status: 500 });
});
}
);
return getSession(server, sessionCookie.id, sessionCookie.token).then(
({ session }) => {
const newCookie: SessionCookie = {
id: sessionCookie.id,
token: sessionCookie.token,
changeDate: session.changeDate,
loginName: session.factors.user.loginName,
};
return updateSessionCookie(sessionCookie.id, newCookie)
.then(() => {
return NextResponse.json({ factors: session.factors });
})
.catch((error) => {
return NextResponse.json(
{ details: "could not set cookie" },
{ status: 500 }
);
});
}
);
})
.catch((error) => {
console.error("erasd", error);
return NextResponse.json(error, { status: 500 });
});
} else {
return NextResponse.error();
}

View File

@@ -22,24 +22,29 @@ async function set(sessions: SessionCookie[]) {
export async function addSessionToCookie(session: SessionCookie): Promise<any> {
const cookiesList = cookies();
// const hasSessions = cookiesList.has("sessions");
// if (hasSessions) {
const stringifiedCookie = cookiesList.get("sessions");
const currentSessions: SessionCookie[] = stringifiedCookie?.value
let currentSessions: SessionCookie[] = stringifiedCookie?.value
? JSON.parse(stringifiedCookie?.value)
: [];
const index = currentSessions.findIndex(
(s) => s.loginName === session.loginName
);
if (index > -1) {
currentSessions[index] = session;
} else {
currentSessions = [...currentSessions, session];
}
// @ts-ignore
return cookiesList.set({
name: "sessions",
value: JSON.stringify([...currentSessions, session]),
value: JSON.stringify(currentSessions),
httpOnly: true,
path: "/",
});
// } else {
// return set([session]);
// }
}
export async function updateSessionCookie(
@@ -47,8 +52,7 @@ export async function updateSessionCookie(
session: SessionCookie
): Promise<any> {
const cookiesList = cookies();
// const hasSessions = cookiesList.has("sessions");
// if (hasSessions) {
const stringifiedCookie = cookiesList.get("sessions");
const sessions: SessionCookie[] = stringifiedCookie?.value
@@ -65,17 +69,12 @@ export async function updateSessionCookie(
httpOnly: true,
path: "/",
});
// } else {
// return Promise.reject();
// }
}
export async function removeSessionFromCookie(
session: SessionCookie
): Promise<any> {
const cookiesList = cookies();
// const hasSessions = cookiesList.has("sessions");
// if (hasSessions) {
const stringifiedCookie = cookiesList.get("sessions");
const sessions: SessionCookie[] = stringifiedCookie?.value
@@ -88,14 +87,11 @@ export async function removeSessionFromCookie(
// @ts-ignore
return cookiesList.set({
name: "__Secure-sessions",
name: "sessions",
value: JSON.stringify(filteredSessions),
httpOnly: true,
path: "/",
});
// } else {
// return Promise.reject();
// }
}
export async function getMostRecentSessionCookie(): Promise<any> {
@@ -105,7 +101,6 @@ export async function getMostRecentSessionCookie(): Promise<any> {
if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
console.log(sessions);
const latest = sessions.reduce((prev, current) => {
return new Date(prev.changeDate).getTime() >
new Date(current.changeDate).getTime()
@@ -147,12 +142,9 @@ export async function getMostRecentCookieWithLoginname(
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
const filtered = sessions.filter((cookie) => {
console.log(!!loginName);
return !!loginName ? cookie.loginName === loginName : true;
});
console.log(filtered);
const latest =
filtered && filtered.length
? filtered.reduce((prev, current) => {