From 89ce31bf00eec0c884800dc3427a34cccecba23e Mon Sep 17 00:00:00 2001 From: Jean-Christophe Drouin Date: Wed, 12 Jul 2023 13:44:20 -0400 Subject: [PATCH] Fix for the bug when using a deeplink combine with a shortened url Signed-off-by: Jean-Christophe Drouin --- packages/legacy/core/App/utils/helpers.ts | 9 +++++- .../core/__tests__/utils/helpers.test.ts | 28 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/legacy/core/App/utils/helpers.ts b/packages/legacy/core/App/utils/helpers.ts index 2152bb58b..fa0a974b8 100644 --- a/packages/legacy/core/App/utils/helpers.ts +++ b/packages/legacy/core/App/utils/helpers.ts @@ -456,6 +456,13 @@ export const receiveMessageFromDeepLink = async (url: string, agent: Agent | und return message } +export const normalizeInvitationUri = (uri: string) => { + const regexStartUri = /(http:\/\/|https:\/\/)/ + const matches = uri.match(regexStartUri) + const normalizeUri = uri.substring(matches?.index ?? 0, uri.length) + return normalizeUri +} + /** * * @param uri a URI containing a base64 encoded connection invite in the query parameter @@ -463,7 +470,7 @@ export const receiveMessageFromDeepLink = async (url: string, agent: Agent | und * @returns a connection record from parsing and receiving the invitation */ export const connectFromInvitation = async (uri: string, agent: Agent | undefined) => { - const invitation = await agent?.oob.parseInvitation(uri) + const invitation = await agent?.oob.parseInvitation(normalizeInvitationUri(uri)) if (!invitation) { throw new Error('Could not parse invitation from URL') diff --git a/packages/legacy/core/__tests__/utils/helpers.test.ts b/packages/legacy/core/__tests__/utils/helpers.test.ts index db79f5322..547131009 100644 --- a/packages/legacy/core/__tests__/utils/helpers.test.ts +++ b/packages/legacy/core/__tests__/utils/helpers.test.ts @@ -1,7 +1,7 @@ import fs from 'fs' import path from 'path' -import { credentialSortFn, formatIfDate } from '../../App/utils/helpers' +import { credentialSortFn, formatIfDate, normalizeInvitationUri } from '../../App/utils/helpers' const proofCredentialPath = path.join(__dirname, '../fixtures/proof-credential.json') const credentials = JSON.parse(fs.readFileSync(proofCredentialPath, 'utf8')) @@ -52,3 +52,29 @@ describe('formatIfDate', () => { expect(setter).toBeCalledTimes(0) }) }) + +describe('normalizeInvitationUri', () => { + const query = 'c_i=eyJAdHlwZSI6IIsImF13IiOiJodHRwczovL' + const shortenedUrl = `https://bit.ly/3f2X2lM?` + + test('with a connection invitation uri normalize invitation should return the same url', () => { + const uri = `https://example.com?${query}` + expect(normalizeInvitationUri(uri)).toBe(uri) + }) + + test('with a deeplink connection invitation uri normalize invitation should return the same deeplink uri', () => { + const deeplink = `didcomm://invite?${query}` + + expect(normalizeInvitationUri(deeplink)).toBe(deeplink) + }) + + test('with a shortened url normalize invitation should return the same url', () => { + expect(normalizeInvitationUri(shortenedUrl)).toBe(shortenedUrl) + }) + + test('with a deeplink shortened url normalize invitation should return the shortened url', () => { + const deeplink = `didcomm://invite?${shortenedUrl}` + + expect(normalizeInvitationUri(deeplink)).toBe(shortenedUrl) + }) +})