Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #97

Merged
merged 3 commits into from
Jan 7, 2025
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
45 changes: 36 additions & 9 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, Session, UseGuards} from '@nestjs/common';
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 {
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,9 +21,10 @@ import { ChainId } from '../../core/domain/entities/environment';
type Siwens = { address: string, ens: string, chainId: ChainId };

@Controller('credentials')
export class CredentialsController {
export class CredentialsController implements OnModuleInit, OnModuleDestroy {

private authSubjects: Map<string, Subject<SubjectData>> = new Map();
private heartbeatInterval: NodeJS.Timer;

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


res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Expand All @@ -61,11 +87,11 @@ export class CredentialsController {
res.write(`data: ${JSON.stringify({ redirectUrl })}\n\n`);

subject.pipe(
filter(data => data.authId === authId),
filter(data => data.authId === authId && !data.heartbeat),
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 @@ -74,7 +100,7 @@ export class CredentialsController {
res.end();
this.authSubjects.delete(authId);
}
);
)
}

@Get('socials/:authName/callback')
Expand All @@ -95,6 +121,7 @@ export class CredentialsController {
const subject = this.authSubjects.get(authId);
subject?.next({
authId,
heartbeat: false,
result: {
verifiableCredential,
dataKey
Expand Down
3 changes: 2 additions & 1 deletion apps/vc-api/src/api/credentials/isubject.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { VerifiableEthereumEip712Signature2021 } from '../../core/domain/entitie

export interface SubjectData {
authId: string;
result: {
heartbeat: boolean
result?: {
verifiableCredential: VerifiableEthereumEip712Signature2021;
dataKey: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const CALLBACK_URL = 'CALLBACK_URL';

const RESOLVER: Partial<AbstractSocialResolver<{ state: string }, {}>> = {
getCredentialName: () => CREDENTIAL_NAME,
getAuthUrl: () => AUTH_URL,
getAuthUrl: async () => AUTH_URL,
getCallbackUrl: () => CALLBACK_URL,
}

Expand Down
Loading