Skip to content

Commit ffeb704

Browse files
feat: Validations for organization and template ledgerId in all issuance methods (#1200)
* fix: ledgerId validations in issuance process Signed-off-by: bhavanakarwade <[email protected]> * fix: added validations for not found exception Signed-off-by: bhavanakarwade <[email protected]> * chore: added comment Signed-off-by: bhavanakarwade <[email protected]> --------- Signed-off-by: bhavanakarwade <[email protected]>
1 parent d1f0679 commit ffeb704

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

apps/issuance/src/issuance.repository.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { IIssuedCredentialSearchParams } from 'apps/api-gateway/src/issuance/int
2828
import { IUserRequest } from '@credebl/user-request/user-request.interface';
2929
import { PrismaService } from '@credebl/prisma-service';
3030
import { ResponseMessages } from '@credebl/common/response-messages';
31+
3132
@Injectable()
3233
export class IssuanceRepository {
3334
constructor(
@@ -275,7 +276,8 @@ export class IssuanceRepository {
275276

276277
async getSchemaDetails(schemaId: string): Promise<schema> {
277278
try {
278-
const schemaDetails = await this.prisma.schema.findFirstOrThrow({
279+
//Todo: Enhance this query by using FindFirstOrThrow
280+
const schemaDetails = await this.prisma.schema.findFirst({
279281
where: {
280282
schemaLedgerId: schemaId
281283
}
@@ -317,7 +319,7 @@ export class IssuanceRepository {
317319
return credentialDefRes;
318320
} catch (error) {
319321
this.logger.error(`Error in getCredentialDefinitionDetails: ${error.message}`);
320-
throw new InternalServerErrorException(error.message);
322+
throw error;
321323
}
322324
}
323325

apps/issuance/src/issuance.service.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ export class IssuanceService {
135135
}
136136

137137
const getSchemaDetails = await this.issuanceRepository.getSchemaDetails(schemaUrl);
138+
139+
if (!getSchemaDetails) {
140+
throw new NotFoundException(ResponseMessages.schema.error.notFound);
141+
}
142+
138143
const schemaAttributes = JSON.parse(getSchemaDetails?.attributes);
139144

140145
return schemaAttributes;
@@ -206,6 +211,21 @@ export class IssuanceService {
206211
comment
207212
};
208213
} else if (payload.credentialType === IssueCredentialType.JSONLD) {
214+
const schemaIds = credentialData?.map((item) => {
215+
const context: string[] = item?.credential?.['@context'];
216+
return Array.isArray(context) && 1 < context.length ? context[1] : undefined;
217+
});
218+
219+
const schemaDetails = await this._getSchemaDetails(schemaIds);
220+
221+
const ledgerIds = schemaDetails?.map((item) => item?.ledgerId);
222+
223+
for (const ledgerId of ledgerIds) {
224+
if (agentDetails?.ledgerId !== ledgerId) {
225+
throw new BadRequestException(ResponseMessages.issuance.error.ledgerMismatched);
226+
}
227+
}
228+
209229
issueData = {
210230
protocolVersion: payload.protocolVersion || 'v2',
211231
connectionId,
@@ -379,6 +399,20 @@ export class IssuanceService {
379399
}
380400

381401
if (credentialType === IssueCredentialType.JSONLD) {
402+
const context = credential?.['@context'][1];
403+
404+
const schemaDetails = await this.issuanceRepository.getSchemaDetails(String(context));
405+
406+
if (!schemaDetails) {
407+
throw new NotFoundException(ResponseMessages.schema.error.notFound);
408+
}
409+
410+
const ledgerId = schemaDetails?.ledgerId;
411+
412+
if (agentDetails?.ledgerId !== ledgerId) {
413+
throw new BadRequestException(ResponseMessages.issuance.error.ledgerMismatched);
414+
}
415+
382416
issueData = {
383417
protocolVersion: protocolVersion || 'v2',
384418
credentialFormats: {
@@ -733,8 +767,25 @@ export class IssuanceService {
733767
isValidateSchema
734768
} = outOfBandCredential;
735769

770+
const agentDetails = await this.issuanceRepository.getAgentEndPoint(orgId);
771+
736772
if (IssueCredentialType.JSONLD === credentialType) {
737773
await validateAndUpdateIssuanceDates(credentialOffer);
774+
775+
const schemaIds = credentialOffer?.map((item) => {
776+
const context: string[] = item?.credential?.['@context'];
777+
return Array.isArray(context) && 1 < context.length ? context[1] : undefined;
778+
});
779+
780+
const schemaDetails = await this._getSchemaDetails(schemaIds);
781+
782+
const ledgerIds = schemaDetails?.map((item) => item?.ledgerId);
783+
784+
for (const ledgerId of ledgerIds) {
785+
if (agentDetails?.ledgerId !== ledgerId) {
786+
throw new BadRequestException(ResponseMessages.issuance.error.ledgerMismatched);
787+
}
788+
}
738789
}
739790

740791
if (IssueCredentialType.INDY === credentialType) {
@@ -780,7 +831,6 @@ export class IssuanceService {
780831
}
781832
}
782833
}
783-
const agentDetails = await this.issuanceRepository.getAgentEndPoint(orgId);
784834

785835
const { organisation } = agentDetails;
786836
if (!agentDetails) {
@@ -1177,6 +1227,10 @@ export class IssuanceService {
11771227

11781228
const schemaDetails = await this.issuanceRepository.getSchemaDetails(schemaResponse.schemaLedgerId);
11791229

1230+
if (!schemaDetails) {
1231+
throw new NotFoundException(ResponseMessages.schema.error.notFound);
1232+
}
1233+
11801234
if (orgDetails?.ledgerId !== schemaDetails?.ledgerId) {
11811235
throw new BadRequestException(ResponseMessages.issuance.error.ledgerMismatched);
11821236
}
@@ -1261,6 +1315,10 @@ export class IssuanceService {
12611315
credentialDetails = await this.issuanceRepository.getCredentialDefinitionDetails(templateId);
12621316
const schemaDetails = await this.issuanceRepository.getSchemaDetails(credentialDetails.schemaLedgerId);
12631317

1318+
if (!schemaDetails) {
1319+
throw new NotFoundException(ResponseMessages.schema.error.notFound);
1320+
}
1321+
12641322
if (orgDetails?.ledgerId !== schemaDetails?.ledgerId) {
12651323
throw new BadRequestException(ResponseMessages.issuance.error.ledgerMismatched);
12661324
}

0 commit comments

Comments
 (0)