Skip to content

Commit

Permalink
fix(oob): allow encoding in content type header (#1037)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Sep 26, 2022
1 parent 34db14b commit e1d6592
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 16 additions & 5 deletions packages/core/src/utils/__tests__/shortenedUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { OutOfBandInvitation } from '../../modules/oob'
import { convertToNewInvitation } from '../../modules/oob/helpers'
import { JsonTransformer } from '../JsonTransformer'
import { MessageValidator } from '../MessageValidator'
import { oobInvitationfromShortUrl } from '../parseInvitation'
import { oobInvitationFromShortUrl } from '../parseInvitation'

const mockOobInvite = {
'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.0/invitation',
Expand Down Expand Up @@ -89,21 +89,32 @@ beforeAll(async () => {

describe('shortened urls resolving to oob invitations', () => {
test('Resolve a mocked response in the form of a oob invitation as a json object', async () => {
const short = await oobInvitationfromShortUrl(mockedResponseOobJson)
const short = await oobInvitationFromShortUrl(mockedResponseOobJson)
expect(short).toEqual(outOfBandInvitationMock)
})
test('Resolve a mocked response in the form of a oob invitation encoded in an url', async () => {
const short = await oobInvitationfromShortUrl(mockedResponseOobUrl)
const short = await oobInvitationFromShortUrl(mockedResponseOobUrl)
expect(short).toEqual(outOfBandInvitationMock)
})

test("Resolve a mocked response in the form of a oob invitation as a json object with header 'application/json; charset=utf-8'", async () => {
const short = await oobInvitationFromShortUrl({
...mockedResponseOobJson,
headers: new Headers({
'content-type': 'application/json; charset=utf-8',
}),
} as Response)
expect(short).toEqual(outOfBandInvitationMock)
})
})

describe('shortened urls resolving to connection invitations', () => {
test('Resolve a mocked response in the form of a connection invitation as a json object', async () => {
const short = await oobInvitationfromShortUrl(mockedResponseConnectionJson)
const short = await oobInvitationFromShortUrl(mockedResponseConnectionJson)
expect(short).toEqual(connectionInvitationToNew)
})
test('Resolve a mocked Response in the form of a connection invitation encoded in an url', async () => {
const short = await oobInvitationfromShortUrl(mockedResponseConnectionUrl)
const short = await oobInvitationFromShortUrl(mockedResponseConnectionUrl)
expect(short).toEqual(connectionInvitationToNew)
})
})
6 changes: 3 additions & 3 deletions packages/core/src/utils/parseInvitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export const parseInvitationUrl = (invitationUrl: string): OutOfBandInvitation =
}

//This currently does not follow the RFC because of issues with fetch, currently uses a janky work around
export const oobInvitationfromShortUrl = async (response: Response): Promise<OutOfBandInvitation> => {
export const oobInvitationFromShortUrl = async (response: Response): Promise<OutOfBandInvitation> => {
if (response) {
if (response.headers.get('Content-Type') === 'application/json' && response.ok) {
if (response.headers.get('Content-Type')?.startsWith('application/json') && response.ok) {
const invitationJson = await response.json()
const parsedMessageType = parseMessageType(invitationJson['@type'])
if (supportsIncomingMessageType(parsedMessageType, OutOfBandInvitation.type)) {
Expand Down Expand Up @@ -107,7 +107,7 @@ export const parseInvitationShortUrl = async (
return convertToNewInvitation(invitation)
} else {
try {
return oobInvitationfromShortUrl(await fetchShortUrl(invitationUrl, dependencies))
return oobInvitationFromShortUrl(await fetchShortUrl(invitationUrl, dependencies))
} catch (error) {
throw new AriesFrameworkError(
'InvitationUrl is invalid. It needs to contain one, and only one, of the following parameters: `oob`, `c_i` or `d_m`, or be valid shortened URL'
Expand Down

0 comments on commit e1d6592

Please sign in to comment.