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

feat: Update accreditation verifyResult [DEV-4436] #605

Merged
merged 10 commits into from
Oct 11, 2024
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 | undefined = undefined;

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 (!initialVerifyResult) {
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
13 changes: 5 additions & 8 deletions src/static/swagger-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,6 @@
}
}
},
"connector": {
"type": "string",
"enum": [
"verida",
"resource"
]
},
"required": [
"issuerDid",
"subjectDid",
Expand Down Expand Up @@ -730,23 +723,27 @@
"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"
},
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>;
11 changes: 5 additions & 6 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
Loading