Skip to content

Commit

Permalink
feat: Update accreditation verifyResult
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Oct 9, 2024
1 parent 8415ff5 commit 6f2a6b6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/controllers/api/accreditation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export class AccreditationController {
if (result.success) {
return response.status(StatusCodes.OK).json(result.data);
} else {
return response.status(result.status).json({ verified: false, error: result.error });
return response.status(result.status).json({ ...result.data, verified: false, error: result.error });
}
} catch (error) {
return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
Expand Down
44 changes: 27 additions & 17 deletions src/services/api/accreditation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SafeAPIResponse } from '../../types/common.js';
import { AccreditationSchemaType, DIDAccreditationTypes, VerfifiableAccreditation } from '../../types/accreditation.js';
import { isCredentialIssuerDidDeactivated } from '../helpers.js';
import { IdentityServiceStrategySetup } from '../identity/index.js';
import type { VerificationPolicies } from '@veramo/core';
import type { IVerifyResult, VerificationPolicies } from '@veramo/core';
import { CheqdW3CVerifiableCredential } from '../w3c-credential.js';
import { StatusCodes } from 'http-status-codes';

Expand All @@ -25,6 +25,8 @@ export class AccreditationService {

let accreditationUrl = didUrl;
let accreditedSubject = subjectDid;
let initialVerifyResult: IVerifyResult = { verified: false };

while (true) {
const res = await identityServiceStrategySetup.agent.resolve(accreditationUrl);

Expand All @@ -34,7 +36,8 @@ export class AccreditationService {
return {
success: false,
status: StatusCodes.NOT_FOUND,
error: `DID Url ${accreditationUrl} is not found`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: DID Url ${accreditationUrl} is not found`,
};
}

Expand All @@ -48,7 +51,8 @@ export class AccreditationService {
return {
success: false,
status: StatusCodes.BAD_REQUEST,
error: `Issuer DID is deactivated`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: Issuer DID is deactivated`,
};
}

Expand All @@ -57,7 +61,8 @@ export class AccreditationService {
return {
success: false,
status: StatusCodes.BAD_REQUEST,
error: `Accreditation mismatch: Expected accreditation to be linked to subject DID ${accreditedSubject}, but found it linked to DID ${accreditation.credentialSubject.id} instead.`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: Expected accreditation to be linked to subject DID ${accreditedSubject}, but found it linked to DID ${accreditation.credentialSubject.id} instead.`,
};
}

Expand All @@ -76,7 +81,8 @@ export class AccreditationService {
return {
success: false,
status: StatusCodes.UNAUTHORIZED,
error: `Accreditation does not have the permissions for the given schema`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: Accreditation does not have the permissions for the given schema`,
};
}

Expand All @@ -89,19 +95,25 @@ export class AccreditationService {
customer
);

if (!result) {
initialVerifyResult = { ...verifyResult, rootAuthorization };
}

if (verifyResult.error) {
return {
success: false,
status: StatusCodes.OK,
error: `verify: ${verifyResult.error.message}`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: ${verifyResult.error.message}`,
};
}

if (!Array.isArray(accreditation.type)) {
return {
success: false,
status: StatusCodes.BAD_REQUEST,
error: `Invalid accreditation type`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: Invalid accreditation type`,
};
}

Expand All @@ -112,7 +124,8 @@ export class AccreditationService {
return {
success: false,
status: StatusCodes.BAD_REQUEST,
error: `Invalid accreditation type`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: Invalid accreditation type`,
};
}

Expand All @@ -125,7 +138,8 @@ export class AccreditationService {
return {
success: false,
status: StatusCodes.BAD_REQUEST,
error: `Missing parentAccreditaiton and rootAuthorization in termsOfUse for accreditation: ${accreditationUrl}`,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: Missing parentAccreditaiton and rootAuthorization in termsOfUse for accreditation: ${accreditationUrl}`,
};
}

Expand All @@ -139,11 +153,9 @@ export class AccreditationService {
if (rootAuthorization && rootAuthorization !== termsOfUse.rootAuthorization) {
return {
status: StatusCodes.OK,
success: true,
data: {
...verifyResult,
verified: false,
},
success: false,
data: initialVerifyResult,
error: `Error on verifying accreditation ${accreditationUrl}: Expected accreditation to be linked to root accreditation ${rootAuthorization}, but found it linked to DID ${termsOfUse.rootAuthorization} instead`,
};
}

Expand All @@ -152,9 +164,7 @@ export class AccreditationService {
return {
status: StatusCodes.OK,
success: true,
data: {
...verifyResult,
},
data: initialVerifyResult,
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type APIErrorResponse<Input> = {
success: false;
status: number;
error: Input;
data?: any;
};

export type SafeAPIResponse<Output, Input = string> = APISuccessResponse<Output> | APIErrorResponse<Input>;
17 changes: 10 additions & 7 deletions src/types/swagger-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,6 @@
* documentPresence: Physical,
* licenseNumber: 123AB4567
* }
* connector:
* type: string
* enum:
* - verida
* - resource
* required:
* - issuerDid
* - subjectDid
Expand Down Expand Up @@ -488,19 +483,23 @@
* type: string
* example: did:cheqd:testnet:5efa5126-c070-420f-a9c2-d22ae6eefb92
* didUrl:
* description: Verifiable Accreditation to be verified as a VC-JWT string or a JSON object.
* description: DID Url of the Verifiable Accreditation to be verified as a VC-JWT string or a JSON object.
* type: string
* example: did:cheqd:testnet:7c2b990c-3d05-4ebf-91af-f4f4d0091d2e?resourceName=cheqd-issuer-logo&resourceType=CredentialArtwork
* did:
* description: DID of the Verifiable Accreditation holder/subject
* type: string
* example: did:cheqd:testnet:7c2b990c-3d05-4ebf-91af-f4f4d0091d2e
* resourceId:
* description: Unique resource identifier of the Verifiable Accreditation
* type: string
* example: 398cee0a-efac-4643-9f4c-74c48c72a14b
* resourceName:
* description: Resource name of the Verifiable Accreditation
* type: string
* example: cheqd-issuer-logo
* resourceType:
* description: Resource type of the Verifiable Accreditation
* type: string
* example: CredentialArtwork
* schemas:
Expand Down Expand Up @@ -530,19 +529,23 @@
* type: object
* properties:
* didUrl:
* description: Verifiable Accreditation to be verified as a VC-JWT string or a JSON object.
* description: DID Url of the Verifiable Accreditation to be verified as a VC-JWT string or a JSON object.
* type: string
* example: did:cheqd:testnet:7c2b990c-3d05-4ebf-91af-f4f4d0091d2e?resourceName=cheqd-issuer-logo&resourceType=CredentialArtwork
* did:
* description: DID of the Verifiable Accreditation holder/subject
* type: string
* example: did:cheqd:testnet:7c2b990c-3d05-4ebf-91af-f4f4d0091d2e
* resourceId:
* description: Unique resource identifier of the Verifiable Accreditation
* type: string
* example: 398cee0a-efac-4643-9f4c-74c48c72a14b
* resourceName:
* description: Resource name of the Verifiable Accreditation
* type: string
* example: cheqd-issuer-logo
* resourceType:
* description: Resource type of the Verifiable Accreditation
* type: string
* example: CredentialArtwork
* symmetricKey:
Expand Down

0 comments on commit 6f2a6b6

Please sign in to comment.