Skip to content

Commit

Permalink
feat(url-handler): allow for URL redirects (#362) (#366)
Browse files Browse the repository at this point in the history
* feat(url-handler): allow for URL redirects
* test: add tests for url-handler redirect scenario

fixes #362

Co-authored-by: Mircea Nistor <mirceanis@gmail.com>
  • Loading branch information
rkreutzer and mirceanis committed Feb 4, 2021
1 parent c1d4230 commit 92a86d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/url-handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

This is an implementation of `AbstractMessageHandler` that can interpret messages presented as a URL.

This is done either by attempting to extract a `c_i` param from the URL query string or by fetching the given URL.
This is done either by attempting to extract a `c_i` param from the URL query string or by fetching the given URL. This also looks for a URL redirect and attempts to extract the parameter from the redirected URL.
28 changes: 27 additions & 1 deletion packages/url-handler/src/__tests__/message-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Message } from '@veramo/message-handler'
import { UrlMessageHandler } from '../index'
import fetchMock from 'jest-fetch-mock'
import fetchMock, { MockParams } from 'jest-fetch-mock'
import { IAgent } from '@veramo/core'
fetchMock.enableMocks()

Expand Down Expand Up @@ -45,4 +45,30 @@ describe('@veramo/url-handler', () => {

expect(message.raw).toEqual('mockbody')
})

it('should try to load data from redirected URL query', async () => {
const message = new Message({ raw: 'https://example.com/public-profile.jwt' })
fetchMock.mockResponse('mockbody', {
counter: 1,
url: 'https://some.other.site.example.com?c_i=asdf',
} as MockParams)
expect.assertions(2)

await expect(messageHandler.handle(message, context)).rejects.toThrow('Unsupported message type')

expect(message.raw).toEqual('asdf')
})

it('should try to load data from redirected URL body', async () => {
const message = new Message({ raw: 'https://example.com/public-profile.jwt' })
fetchMock.mockResponse('otherbody', {
counter: 1,
url: 'https://some.other.example.com/cred.jwt',
} as MockParams)
expect.assertions(2)

await expect(messageHandler.handle(message, context)).rejects.toThrow('Unsupported message type')

expect(message.raw).toEqual('otherbody')
})
})
16 changes: 14 additions & 2 deletions packages/url-handler/src/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ export class UrlMessageHandler extends AbstractMessageHandler {
const url = message.raw
debug('Fetching URL', url)
const response = await fetch(url)
message.raw = await response.text()
message.addMetaData({ type: 'URL', value: url })
if (response?.url && response.url !== url) {
debug('Detected redirect URL')
const parsed2 = parse(response.url, {}, true)
if (parsed2 && parsed2.query && parsed2.query.c_i) {
message.raw = parsed2.query.c_i
message.addMetaData({ type: 'URL', value: parsed2.origin + parsed2.pathname })
} else {
message.raw = await response.text()
message.addMetaData({ type: 'URL', value: url })
}
} else {
message.raw = await response.text()
message.addMetaData({ type: 'URL', value: url })
}
} catch (e) {
console.log(e)
debug(e.message)
Expand Down

0 comments on commit 92a86d6

Please sign in to comment.