From f402b2e30eac5ba2dc3d6d58adb8ce922bfb0131 Mon Sep 17 00:00:00 2001 From: AngeloAyranji Date: Wed, 8 Jan 2025 10:48:18 +0200 Subject: [PATCH 1/2] chore(vc-api): removed heartbeat --- .../api/credentials/credentials.controller.ts | 47 ++++--------------- .../src/api/credentials/isubject.data.ts | 5 +- 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/apps/vc-api/src/api/credentials/credentials.controller.ts b/apps/vc-api/src/api/credentials/credentials.controller.ts index 216cfcb..3da44dc 100644 --- a/apps/vc-api/src/api/credentials/credentials.controller.ts +++ b/apps/vc-api/src/api/credentials/credentials.controller.ts @@ -1,12 +1,12 @@ -import {Body, Controller, Get, Inject, Param, Post, Query, Req, Res, UseGuards, OnModuleInit, OnModuleDestroy} from '@nestjs/common'; -import { filter, Subject, take } from 'rxjs'; -import { v4 as uuidv4 } from 'uuid'; -import { Response } from 'express'; +import {Body, Controller, Get, Inject, Param, Post, Query, Req, Res, Session, UseGuards} from '@nestjs/common'; import { CREDENTIAL_CREATOR_FACADE, ICredentialCreatorFacade } from '../../core/applications/credentials/facade/icredential.facade'; +import { Response } from 'express'; import { AUTH_CONTROLLER_MAPPER, IcredentialsControllerMapper } from './mapper/icredentials.controller.mapper'; +import { v4 as uuidv4 } from 'uuid'; +import { filter, Subject, take } from 'rxjs'; import { SubjectData } from './isubject.data'; import { JwtGuard } from '../../guards/jwt.guard'; import {CredentialsGenerateEmailOtpApiRequestQuery} from "./requests/credentials.generate-email-otp.request.api"; @@ -21,10 +21,9 @@ import { ChainId } from '../../core/domain/entities/environment'; type Siwens = { address: string, ens: string, chainId: ChainId }; @Controller('credentials') -export class CredentialsController implements OnModuleInit, OnModuleDestroy { +export class CredentialsController { private authSubjects: Map> = new Map(); - private heartbeatInterval: NodeJS.Timer; constructor( @Inject(CREDENTIAL_CREATOR_FACADE) @@ -34,32 +33,6 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy { private readonly authControllerMapper: IcredentialsControllerMapper ) {} - onModuleInit() { - this.heartbeatInterval = setInterval(() => { - this.sendHeartbeats(); - }, 10000); - } - - onModuleDestroy() { - if (this.heartbeatInterval) { - clearInterval(this.heartbeatInterval); - } - } - - private sendHeartbeats() { - this.authSubjects.forEach((subject, authId) => { - try { - subject.next({ - authId, - heartbeat: true, - }); - } catch (error) { - this.authSubjects.delete(authId); - throw Error(`Failed to send heartbeat to ${authId}: ${error}`); - } - }); - } - @UseGuards(JwtGuard) @Get('socials/:authName') async getAuthUrl( @@ -78,6 +51,7 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy { authId ) + res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', @@ -87,11 +61,11 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy { res.write(`data: ${JSON.stringify({ redirectUrl })}\n\n`); subject.pipe( - filter(data => data.authId === authId && !data.heartbeat), + filter(data => data.authId === authId), take(1) ).subscribe( (data) => { - res.write(`data: ${JSON.stringify({ result: data.result })}\n\n`); + res.write(`data: ${JSON.stringify({ result:data.result })}\n\n`); res.end(); this.authSubjects.delete(authId); }, @@ -100,7 +74,7 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy { res.end(); this.authSubjects.delete(authId); } - ) + ); } @Get('socials/:authName/callback') @@ -121,7 +95,6 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy { const subject = this.authSubjects.get(authId); subject?.next({ authId, - heartbeat: false, result: { verifiableCredential, dataKey @@ -205,4 +178,4 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy { this.authSubjects.delete(authId); } -} +} \ No newline at end of file diff --git a/apps/vc-api/src/api/credentials/isubject.data.ts b/apps/vc-api/src/api/credentials/isubject.data.ts index 66d5b46..8448f14 100644 --- a/apps/vc-api/src/api/credentials/isubject.data.ts +++ b/apps/vc-api/src/api/credentials/isubject.data.ts @@ -2,9 +2,8 @@ import { VerifiableEthereumEip712Signature2021 } from '../../core/domain/entitie export interface SubjectData { authId: string; - heartbeat: boolean - result?: { + result: { verifiableCredential: VerifiableEthereumEip712Signature2021; dataKey: string; }; -} +} \ No newline at end of file From c7933a0d162489eb7ce3917ad15d57c5d377c2e4 Mon Sep 17 00:00:00 2001 From: AngeloAyranji Date: Wed, 8 Jan 2025 11:14:58 +0200 Subject: [PATCH 2/2] feat(openpassport-static): execute returned script --- apps/openpassport-static/src/app/app.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/openpassport-static/src/app/app.tsx b/apps/openpassport-static/src/app/app.tsx index 9bc3b8d..0e8d353 100644 --- a/apps/openpassport-static/src/app/app.tsx +++ b/apps/openpassport-static/src/app/app.tsx @@ -18,7 +18,7 @@ export function App() { try { const encodedAttestation = btoa(JSON.stringify(attestation)); - await axios.get( + const response = await axios.get( `${import.meta.env.VITE_APP_API_DOMAIN}/credentials/socials/openpassport/callback`, { params: { @@ -27,6 +27,15 @@ export function App() { }, } ); + + const tempDiv = document.createElement('div'); + tempDiv.innerHTML = response.data; + + const scripts = tempDiv.getElementsByTagName('script'); + for (let i = 0; i < scripts.length; i++) { + const script = scripts[i]; + eval(script.textContent || ''); + } } catch (error) { console.error('Error in callback:', error); }