Skip to content

Commit

Permalink
Fix issue when serviceEndpoint is an object (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello authored Nov 3, 2022
1 parent 4a6b240 commit 46f42d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/verifier/WellKnownDidVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,17 @@ export class WellKnownDidVerifier {
if (!descriptor.serviceEndpoint)
return Promise.reject({ status: ValidationStatusEnum.INVALID, message: WDCErrors.PROPERTY_SERVICE_ENDPOINT_NOT_PRESENT_IN_SERVICE })

// The object serviceEndpoint property can be a string and the value MUST be an origin string
if (new URL(descriptor.serviceEndpoint).origin !== descriptor.serviceEndpoint)
return Promise.reject({status: ValidationStatusEnum.INVALID, message: WDCErrors.PROPERTY_SERVICE_ENDPOINT_NOT_CONTAIN_VALID_ORIGIN})
if (new URL(descriptor.serviceEndpoint).protocol !== 'https:') return Promise.reject({
status: ValidationStatusEnum.INVALID,
message: WDCErrors.PROPERTY_ORIGIN_NOT_SECURE
})

if (typeof descriptor.serviceEndpoint === 'string') {
// The object serviceEndpoint property can be a string and the value MUST be an origin string
if (new URL(descriptor.serviceEndpoint).origin !== descriptor.serviceEndpoint)
return Promise.reject({ status: ValidationStatusEnum.INVALID, message: WDCErrors.PROPERTY_SERVICE_ENDPOINT_NOT_CONTAIN_VALID_ORIGIN })
if (new URL(descriptor.serviceEndpoint).protocol !== 'https:')
return Promise.reject({
status: ValidationStatusEnum.INVALID,
message: WDCErrors.PROPERTY_ORIGIN_NOT_SECURE
})
}

if (typeof descriptor.serviceEndpoint === 'object') {
// The object serviceEndpoint property can be an object which MUST contain an origins property
Expand Down Expand Up @@ -395,7 +399,14 @@ export class WellKnownDidVerifier {
* @param descriptor The endpoint descriptor.
*/
private getOrigins(descriptor: ServiceEndpoint): Array<string> {
return [descriptor.serviceEndpoint]
if (typeof descriptor.serviceEndpoint === 'string') {
return [descriptor.serviceEndpoint]
} else {
// This break with did-resolver@3
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return descriptor.serviceEndpoint.origins;
}
}

}
Expand Down
16 changes: 16 additions & 0 deletions test/verifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ describe('Domain Linkage Verifier', () => {
expect(result.status).toEqual(ValidationStatusEnum.VALID);
});

it('should verify when serviceEndpoint is an object containing an array of origins', async () => {
nock(ORIGIN).get('/.well-known/did-configuration.json').times(1).reply(200, DID_CONFIGURATION);

const endpointDescriptor: ServiceEndpoint = {
id: DID,
type: ServiceTypesEnum.LINKED_DOMAINS,
// This breaks the types in did-resolver@4
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
serviceEndpoint: { origins: [ORIGIN] },
};

const result = await verifier.verifyEndpointDescriptor({ descriptor: endpointDescriptor });
expect(result.status).toEqual(ValidationStatusEnum.VALID);
});

it('should verify when serviceEndpoint is of type IServiceEndpoint', async () => {
nock(ORIGIN).get('/.well-known/did-configuration.json').times(1).reply(200, DID_CONFIGURATION);

Expand Down

0 comments on commit 46f42d9

Please sign in to comment.