Skip to content

Commit

Permalink
Fix/onboarding detection (#8)
Browse files Browse the repository at this point in the history
* fix: QR-Creation in server

* refactor: error logs and session resave

* fix: keycloak realm

* fix: update session maps after login

* fix: correctly parse application data to fill form

---------

Co-authored-by: Korbinian Flietel <[email protected]>
  • Loading branch information
kojofl and kojofl authored Feb 21, 2023
1 parent 645111c commit 5f36a5e
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 14 deletions.
10 changes: 5 additions & 5 deletions packages/server/src/enmeshed/createRegistrationQRCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function createRegistrationQRCode(
"@type": "ProposeAttributeRequestItem",
attribute: {
"@type": "IdentityAttribute",
owner: identity.address,
owner: "",
value: {
"@type": `${translationMap[element.name]?.valueType}`,
value: proposedValue
Expand Down Expand Up @@ -123,7 +123,7 @@ export async function createRegistrationQRCode(
"@type": "ProposeAttributeRequestItem",
attribute: {
"@type": "IdentityAttribute",
owner: identity.address,
owner: "",
value: {
"@type": `${translationMap[optionalElement.name]?.valueType}`,
value: proposedValue
Expand All @@ -149,21 +149,21 @@ export async function createRegistrationQRCode(

const createObject: ConnectorRequestContentItemGroup = {
"@type": "RequestItemGroup",
mustBeAccepted: true,
mustBeAccepted: createItems.some((el) => el.mustBeAccepted),
title: "Shared Attributes",
items: createItems
};

const proposedObject: ConnectorRequestContentItemGroup = {
"@type": "RequestItemGroup",
mustBeAccepted: true,
mustBeAccepted: proposedItems.some((el) => el.mustBeAccepted),
title: "Requested Attributes",
items: proposedItems
};

const requestObject: ConnectorRequestContentItemGroup = {
"@type": "RequestItemGroup",
mustBeAccepted: true,
mustBeAccepted: requestItems.some((el) => el.mustBeAccepted),
title: "Requested Attributes",
items: requestItems
};
Expand Down
34 changes: 34 additions & 0 deletions packages/server/src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { PrivateRouter } from "./routing/PrivateRouter";
import { PrivateRoutes } from "./routing/PrivateRoutes";
import { PublicRouter } from "./routing/PublicRouter";
import { PublicRoutes } from "./routing/PublicRoutes";
import {
extractSessionId,
getSocketFromCookie,
handleConnect,
handleDeprecatedSessionId
} from "./routing/sessionHelper";

export function configRouting(s: EnmeshedLoginDemoServer): void {
const publicRouter = new PublicRouter();
Expand All @@ -22,6 +28,34 @@ export function configRouting(s: EnmeshedLoginDemoServer): void {

s.app.use("/api/v1", publicRouter.router);

// If the session get's overriden by a successful login attempt, we need to update our socket maps to still be able to find them by session.
s.app.use(function (req, res, next) {
res.on("finish", () => {
if (res.hasHeader("Set-Cookie")) {
const newSession = res.getHeader("Set-Cookie");
const sessionString = newSession?.toString();
const oldSession = extractSessionId(req);

if (sessionString?.includes(config.get("server.session.name")) && oldSession) {
const socket = getSocketFromCookie(oldSession);
if (socket) {
const pairs = sessionString.split(";");
const splittedPairs = pairs.filter((el) => el.includes("=")).map((cookie) => cookie.split("="));
const cookieObj = splittedPairs.reduce(function (obj: any, cookie) {
obj[decodeURIComponent(cookie[0].trim())] = decodeURIComponent(cookie[1].trim());
return obj;
}, {});
const newSession = cookieObj[`${config.get("server.session.name")}`];

handleDeprecatedSessionId(oldSession);
handleConnect(newSession, socket);
}
}
}
});
next();
});

const privateRouter = new PrivateRouter();
privateRouter.initialize();
// If there are additional Private Routes in ./routing/PrivateRoutes.ts add them here like
Expand Down
11 changes: 9 additions & 2 deletions packages/server/src/routing/PublicRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ async function onboardingRegistration(
const user = await KeycloakHelper.getUser(userData.userName);
const keycloakTokens = await KeycloakHelper.impersonate(user!.id);

socket?.emit("onboard", keycloakTokens);
if (socket) {
socket.emit("onboard", keycloakTokens);
} else {
console.log("could not find socket");
}
}
} else {
await CONNECTOR_CLIENT.relationships.rejectRelationshipChange(relationshipId, changeId);
Expand Down Expand Up @@ -374,7 +378,10 @@ function getUserData(

for (const entry of entries) {
for (const item of entry.items) {
if (item["@type"] === "ReadAttributeAcceptResponseItem") {
if (
item["@type"] === "ReadAttributeAcceptResponseItem" ||
item["@type"] === "ProposeAttributeAcceptResponseItem"
) {
const el: any = (item as any).attribute;
if (el?.value) {
if (!attr.enmeshedAddress) {
Expand Down
5 changes: 5 additions & 0 deletions packages/server/src/routing/sessionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export function handleConnect(connectSId: string, socket: Socket): void {
cookieToSocket.set(connectSId, socket);
}

export function handleDeprecatedSessionId(sessionId: string): void {
cookieToSocket.delete(sessionId);
adminCookieToSocket.delete(sessionId);
}

export function handleDisconnect(sessionId: string, socketId: string): void {
cookieToSocket.delete(sessionId);
adminCookieToSocket.delete(sessionId);
Expand Down
17 changes: 13 additions & 4 deletions packages/university-frontend/src/components/application.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
}
}
function padNumber(d: number | undefined): string {
if (!d) {
return "";
}
return d < 10 ? `0${d.toString()}` : d.toString();
}
onMount(async () => {
await startOnboarding();
});
Expand All @@ -61,7 +68,9 @@
type="date"
name="Geburtsdatum"
value={$application.birthDate
? `${$application.birthDate.year}-${$application.birthDate.month}-${$application.birthDate.day}`
? `${$application.birthDate.year}-${padNumber($application.birthDate.month)}-${padNumber(
$application.birthDate.day
)}`
: ""}
/>
</FormGroup>
Expand All @@ -73,9 +82,9 @@
</FormGroup>
<FormGroup floating label="Geschlecht*">
<select id="gender" class="form-select form-select-sm" aria-label=".form-select-sm example" required>
<option value="M">Männlich</option>
<option value="W">Weiblich</option>
<option value="D">Divers</option>
<option value="M" selected={$application.sex === "male"}>Männlich</option>
<option value="W" selected={$application.sex === "female"}>Weiblich</option>
<option value="D" selected={$application.sex === "intersex"}>Divers</option>
</select>
</FormGroup>
<FormGroup floating label="E-Mail">
Expand Down
2 changes: 1 addition & 1 deletion packages/university-server/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"site": {
"keycloak": {
"realm": "demo",
"realm": "university",
"auth-server-url": "/kc/",
"ssl-required": "external",
"resource": "demo-client",
Expand Down
5 changes: 3 additions & 2 deletions packages/university-server/src/EnmeshedLoginDemoServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { extractSessionId, handleConnect, handleDisconnect } from "./routing/ses

interface ServerToClientEvents {
register(token: object): void;
onboard(token: object): void;
onboard(application: object): void;
error(error: string): void;
asd(error: string): void;
}

interface ClientToServerEvents {}
Expand Down Expand Up @@ -64,7 +65,7 @@ export class EnmeshedLoginDemoServer {
store: this.store,
secret: config.get("server.session.secret"),
name: config.get("server.session.name"),
resave: true,
resave: false,
cookie: { maxAge: 60000000000000 }
});
this.app.use(this.session);
Expand Down
34 changes: 34 additions & 0 deletions packages/university-server/src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { PrivateRouter } from "./routing/PrivateRouter";
import { PrivateRoutes } from "./routing/PrivateRoutes";
import { PublicRouter } from "./routing/PublicRouter";
import { PublicRoutes } from "./routing/PublicRoutes";
import {
extractSessionId,
getSocketFromCookie,
handleConnect,
handleDeprecatedSessionId
} from "./routing/sessionHelper";

export function configRouting(s: EnmeshedLoginDemoServer): void {
const publicRouter = new PublicRouter();
Expand All @@ -23,6 +29,34 @@ export function configRouting(s: EnmeshedLoginDemoServer): void {

s.app.use("/api/v1", publicRouter.router);

// If the session get's overriden by a successful login attempt, we need to update our socket maps to still be able to find them by session.
s.app.use(function (req, res, next) {
res.on("finish", () => {
if (res.hasHeader("Set-Cookie")) {
const newSession = res.getHeader("Set-Cookie");
const sessionString = newSession?.toString();
const oldSession = extractSessionId(req);

if (sessionString?.includes(config.get("server.session.name")) && oldSession) {
const socket = getSocketFromCookie(oldSession);
if (socket) {
const pairs = sessionString.split(";");
const splittedPairs = pairs.filter((el) => el.includes("=")).map((cookie) => cookie.split("="));
const cookieObj = splittedPairs.reduce(function (obj: any, cookie) {
obj[decodeURIComponent(cookie[0].trim())] = decodeURIComponent(cookie[1].trim());
return obj;
}, {});
const newSession = cookieObj[`${config.get("server.session.name")}`];

handleDeprecatedSessionId(oldSession);
handleConnect(newSession, socket);
}
}
}
});
next();
});

const privateRouter = new PrivateRouter();
privateRouter.initialize();
// If there are additional Private Routes in ./routing/PrivateRoutes.ts add them here like
Expand Down
5 changes: 5 additions & 0 deletions packages/university-server/src/routing/sessionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export function handleConnect(connectSId: string, socket: Socket): void {
cookieToSocket.set(connectSId, socket);
}

export function handleDeprecatedSessionId(sessionId: string): void {
cookieToSocket.delete(sessionId);
adminCookieToSocket.delete(sessionId);
}

export function handleDisconnect(sessionId: string, socketId: string): void {
cookieToSocket.delete(sessionId);
adminCookieToSocket.delete(sessionId);
Expand Down

0 comments on commit 5f36a5e

Please sign in to comment.