Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/openpassport-static/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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);
}
Expand Down
47 changes: 10 additions & 37 deletions apps/vc-api/src/api/credentials/credentials.controller.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<string, Subject<SubjectData>> = new Map();
private heartbeatInterval: NodeJS.Timer;

constructor(
@Inject(CREDENTIAL_CREATOR_FACADE)
Expand All @@ -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(
Expand All @@ -78,6 +51,7 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
authId
)


res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Expand All @@ -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);
},
Expand All @@ -100,7 +74,7 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
res.end();
this.authSubjects.delete(authId);
}
)
);
}

@Get('socials/:authName/callback')
Expand All @@ -121,7 +95,6 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {
const subject = this.authSubjects.get(authId);
subject?.next({
authId,
heartbeat: false,
result: {
verifiableCredential,
dataKey
Expand Down Expand Up @@ -205,4 +178,4 @@ export class CredentialsController implements OnModuleInit, OnModuleDestroy {

this.authSubjects.delete(authId);
}
}
}
5 changes: 2 additions & 3 deletions apps/vc-api/src/api/credentials/isubject.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { VerifiableEthereumEip712Signature2021 } from '../../core/domain/entitie

export interface SubjectData {
authId: string;
heartbeat: boolean
result?: {
result: {
verifiableCredential: VerifiableEthereumEip712Signature2021;
dataKey: string;
};
}
}
Loading