Skip to content

Commit

Permalink
decline -> reject endpoint, CM through auth
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-s19 committed Jun 21, 2023
1 parent f065c5c commit 41c4f36
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
29 changes: 27 additions & 2 deletions apps/authentication/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
import { HttpService } from '@nestjs/axios';
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Param, Patch, Post, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from './auth-jwt.guard';
import { AuthService } from './auth.service';
import { AuthDto } from './dto/auth.dto';
import { CA } from './dto/ca.dto';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) { }

@Post()
@Post('/verify')
@UseGuards(JwtAuthGuard)
hadleAuth(@Body() body: AuthDto) {
return this.authService.handleAuth(body);
}

@Post('/register')
@UseGuards(JwtAuthGuard)
handleRegister(@Body() ca: CA) {
return this.authService.handleRegister(ca);
}

@Patch('/:caId/accept')
@UseGuards(JwtAuthGuard)
handleAccept(@Param('caId') caId: string) {
return this.authService.handleConsent(caId, 'accept');
}

@Patch('/:caId/reject')
@UseGuards(JwtAuthGuard)
handleReject(@Param('caId') caId: string) {
return this.authService.handleConsent(caId, 'reject');
}

@Patch('/:caId/revoke')
@UseGuards(JwtAuthGuard)
handleRevoke(@Param('caId') caId: string) {
return this.authService.handleConsent(caId, 'revoke');
}
}
50 changes: 50 additions & 0 deletions apps/authentication/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpService } from '@nestjs/axios';
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { lastValueFrom, map } from 'rxjs';
import { AuthDto } from './dto/auth.dto';
import { CA } from './dto/ca.dto';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -126,4 +127,53 @@ export class AuthService {
throw new InternalServerErrorException();
}
}

handleRegister = async (data: CA) => {
try {
let ca = data.consentArtifact
const caRes = await lastValueFrom(
this.httpService
.post(
`${process.env.CONSENT_MANAGER_URI}/register`,
{ ca }
)
.pipe(map((response) => response.data)),
);
if (!caRes.caId) {
return "An error occured while creating Consent Artifact";
}

return caRes;

} catch (err) {
console.log('err: ', err);
if (err?.response?.data)
return err?.response?.data;
throw new InternalServerErrorException();
}
}

handleConsent = async (caId: string, type: string) => {
try {
const caRes = await lastValueFrom(
this.httpService
.patch(
`${process.env.CONSENT_MANAGER_URI}/${caId}/${type}`
)
.pipe(map((response) => response.data)),
);

if (!caRes.caId) {
return "An error occured while performing the requested action";
}

return caRes;

} catch (err) {
console.log('err: ', err);
if (err?.response?.data)
return err?.response?.data;
throw new InternalServerErrorException();
}
}
}
3 changes: 3 additions & 0 deletions apps/authentication/src/auth/dto/ca.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class CA {
consentArtifact: JSON
}
9 changes: 5 additions & 4 deletions apps/consent-manager/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CAObject, CAStates, ConsentAction, GetCAResponse, RequestBody, WebhookR
import { AppService } from './app.service';
import { ConsentArtifact, TheProofSchema } from './types/consentArtifact';
import { CARequests } from '@prisma/client';
import { RegisterCA } from './types/RegisterCA';

@Controller()
export class AppController {
Expand Down Expand Up @@ -117,9 +118,9 @@ export class AppController {
return updatedCARequest;
}

@ApiOperation({ summary: 'Decline CA' })
@ApiOperation({ summary: 'Reject CA' })
@ApiResponse({ type: ConsentAction, status: 200, description: 'Reject a CA Request' })
@Patch('/:caId/decline')
@Patch('/:caId/reject')
async rejectCA(@Param('caId') caId: string): Promise<CARequests> {
const caRequest = await this.appService.updateCaStatus(caId, CAStates.DECLINE);
return caRequest;
Expand All @@ -128,7 +129,7 @@ export class AppController {
@ApiOperation({ summary: 'Register CA' })
@ApiResponse({ type: CAObject, status: 200, description: 'Create a new CA Request entry' })
@Post('/register')
register(@Body() ca: ConsentArtifact): Promise<CARequests> {
return this.appService.register(ca, ca.user.id, ca.consumer.url);
register(@Body() body: RegisterCA): Promise<CARequests> {
return this.appService.register(body.ca, body.ca.user.id, body.ca.consumer.url);
}
}
5 changes: 5 additions & 0 deletions apps/consent-manager/src/types/RegisterCA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ConsentArtifact } from "./consentArtifact"

export type RegisterCA = {
ca: ConsentArtifact
}

0 comments on commit 41c4f36

Please sign in to comment.