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

fix: Bug when using a deeplink combine with a shortened url #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/legacy/core/App/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,21 @@ 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
* @param agent an Agent instance
* @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')
Expand Down
28 changes: 27 additions & 1 deletion packages/legacy/core/__tests__/utils/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -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'))
Expand Down Expand Up @@ -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)
})
})