Skip to content

Commit

Permalink
feat: Include hash in verify URL when credential is stored in the sto…
Browse files Browse the repository at this point in the history
…rage service
  • Loading branch information
ldhyen99 committed Nov 5, 2024
1 parent 7518b02 commit 92e1c84
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
91 changes: 91 additions & 0 deletions packages/services/src/__tests__/linkResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,52 @@ describe('create link resolve service', () => {
);
});

it('should return url when creating link resolver with target URL containing hash', async () => {
let expectParamsCallAPI: any;
jest.spyOn(privateAPI, 'post').mockImplementation((value, params) => {
expectParamsCallAPI = params;
return Promise.resolve({});
});

let expectToken = '';
jest.spyOn(privateAPI, 'setBearerTokenAuthorizationHeaders' as any).mockImplementation((token) => {
expectToken = token as string;
return Promise.resolve({});
});

const mockValue = {
eventLink: {
hash: 'c865b6cc188b099c2a61a36f9acaefba72140eb945b46d69205fdb385d004194',
uri: 'http://localhost:3334/v1/verifiable-credentials/c9fbfdd9-7b29-48de-82f8-23e355406c09.json',
},
identificationKeyType: IdentificationKeyType.nlisid,
identificationKey: 'gtin-key',
itemDescription: 'EPCIS transformation event VC',
verificationPage: 'https://verify.com/dev/verifyCredential',
linkType: LinkType.epcisLinkType,
dlrAPIUrl: 'https://dlr.com',
dlrAPIKey: 'dlr-key',
namespace: 'gtin',
qualifierPath: '',
};

const resolverUrl = await registerLinkResolver(
mockValue.eventLink,
mockValue.identificationKeyType,
mockValue.identificationKey,
mockValue.itemDescription,
mockValue.linkType,
mockValue.verificationPage,
mockValue.dlrAPIUrl,
mockValue.dlrAPIKey,
mockValue.namespace,
);

expect(resolverUrl).toEqual(
`${mockValue.dlrAPIUrl}/${mockValue.namespace}/${mockValue.identificationKeyType}/${mockValue.identificationKey}/?linkType=all`,
);
});

it('should throw error when creating link resolver', async () => {
const errorMessage = 'Error creating link resolver';
try {
Expand All @@ -167,6 +213,51 @@ describe('create link resolve service', () => {
expect(error.message).toEqual(errorMessage);
}
});

it('should throw error when creating link resolver with target URL invalid', async () => {
try {
let expectParamsCallAPI: any;
jest.spyOn(privateAPI, 'post').mockImplementation((value, params) => {
expectParamsCallAPI = params;
return Promise.resolve({});
});

let expectToken = '';
jest.spyOn(privateAPI, 'setBearerTokenAuthorizationHeaders' as any).mockImplementation((token) => {
expectToken = token as string;
return Promise.resolve({});
});

const mockValue = {
eventLink: {},
identificationKeyType: IdentificationKeyType.nlisid,
identificationKey: 'gtin-key',
itemDescription: 'EPCIS transformation event VC',
verificationPage: 'https://verify.com/dev/verifyCredential',
linkType: LinkType.epcisLinkType,
dlrAPIUrl: 'https://dlr.com',
dlrAPIKey: 'dlr-key',
namespace: 'gtin',
qualifierPath: '/10/ABC123',
};

await registerLinkResolver(
mockValue.eventLink,
mockValue.identificationKeyType,
mockValue.identificationKey,
mockValue.itemDescription,
mockValue.linkType,
mockValue.verificationPage,
mockValue.dlrAPIUrl,
mockValue.dlrAPIKey,
mockValue.namespace,
mockValue.qualifierPath,
mockValue.linkType,
);
} catch (error: any) {
expect(error.message).toEqual('URI is required');
}
});
});

describe('getLinkResolverIdentifier', () => {
Expand Down
17 changes: 9 additions & 8 deletions packages/services/src/linkResolver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import GS1DigitalLinkToolkit from 'digitallink_toolkit_server/src/GS1DigitalLink
import { IDLRAI, MimeTypeEnum } from './types/index.js';
import { GS1ServiceEnum } from './identityProviders/GS1Provider.js';
import { privateAPI } from './utils/httpService.js';
import { extractDomain } from './utils/helpers.js';
import { constructVerifyURL, extractDomain } from './utils/helpers.js';
/**
* Generates a link resolver URL based on the provided linkResolver and linkResponse objects.
*
Expand Down Expand Up @@ -174,7 +174,7 @@ export const constructLinkResolver = (
};

export const registerLinkResolver = async (
url: string,
storageCredential: any,
identificationKeyType: IdentificationKeyType,
identificationKey: string,
linkTitle: string,
Expand All @@ -191,9 +191,11 @@ export const registerLinkResolver = async (
identificationKey: identificationKey,
itemDescription: linkTitle,
};
const query = encodeURIComponent(JSON.stringify({ payload: { uri: url } }));
const queryString = `q=${query}`;
const verificationPassportPage = `${verificationPage}/?${queryString}`;
let verifyURL = storageCredential;
if (typeof storageCredential !== 'string') {
verifyURL = constructVerifyURL(storageCredential);
}

const linkResponses: ILinkResponse[] = [
{
linkType: `${namespace}:${LinkType.verificationLinkType}`,
Expand All @@ -204,13 +206,13 @@ export const registerLinkResolver = async (
{
linkType: `${namespace}:${linkType}`,
linkTitle: linkTitle,
targetUrl: url,
targetUrl: storageCredential?.uri ?? storageCredential ?? '',
mimeType: MimeType.applicationJson,
},
{
linkType: `${namespace}:${linkType}`,
linkTitle: linkTitle,
targetUrl: verificationPassportPage,
targetUrl: verifyURL,
mimeType: MimeType.textHtml,
defaultLinkType: true,
defaultIanaLanguage: true,
Expand All @@ -223,7 +225,6 @@ export const registerLinkResolver = async (
namespace,
linkResolver,
linkResponses,
queryString,
dlrAPIKey,
qualifierPath: qualifierPath ?? '/',
responseLinkType,
Expand Down

0 comments on commit 92e1c84

Please sign in to comment.