From 72596e4fd71d66a395c109c88ae2419187459269 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Mon, 16 Mar 2020 18:52:17 +0200 Subject: [PATCH] feat: Fetching URL to get the message payload --- examples/expressjs-ethr/src/index.ts | 12 +++-- examples/expressjs-ethr/yarn.lock | 54 +++++++++---------- jest.json | 6 ++- package.json | 1 + packages/daf-cli/src/msg.ts | 2 +- .../src/__tests__/message-validator.test.ts | 17 ++++++ packages/daf-url/src/message-validator.ts | 11 ++++ setupJest.js | 1 + yarn.lock | 13 +++++ 9 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 setupJest.js diff --git a/examples/expressjs-ethr/src/index.ts b/examples/expressjs-ethr/src/index.ts index f0597c967..eb9a9834c 100644 --- a/examples/expressjs-ethr/src/index.ts +++ b/examples/expressjs-ethr/src/index.ts @@ -183,7 +183,13 @@ async function main() { const { views } = req.session - // Sign verifiable credential + const url = encodeURI(process.env.HOST + '/public-profile') + + res.render('about', { views, url }) + }) + + app.get('/public-profile', async(req, res) => { + // Sign verifiable presentation const jwt = await core.handleAction({ type: W3C.ActionTypes.signVc, did: identity.did, @@ -201,9 +207,7 @@ async function main() { }, } as W3C.ActionSignW3cVc) - const url = encodeURI(process.env.HOST + '/?c_i=') + jwt - - res.render('about', { views, url }) + res.send(jwt) }) app.get('/logout', (req, res) => req.session?.destroy(() => res.redirect('/'))) diff --git a/examples/expressjs-ethr/yarn.lock b/examples/expressjs-ethr/yarn.lock index 10d04e16f..81a4347b1 100644 --- a/examples/expressjs-ethr/yarn.lock +++ b/examples/expressjs-ethr/yarn.lock @@ -613,57 +613,57 @@ cross-fetch@^3.0.4: node-fetch "2.6.0" whatwg-fetch "3.0.0" -daf-core@../../packages/daf-core, daf-core@^2.5.0: - version "2.5.0" +daf-core@../../packages/daf-core, daf-core@^3.0.2: + version "3.0.2" dependencies: blakejs "^1.1.0" debug "^4.1.1" events "^3.0.0" typeorm "^0.2.24" -daf-data-store@../../packages/daf-data-store, daf-data-store@^2.5.0: - version "2.5.0" +daf-data-store@../../packages/daf-data-store, daf-data-store@^3.0.2: + version "3.0.2" dependencies: blakejs "^1.1.0" - daf-core "^2.5.0" + daf-core "^3.0.2" debug "^4.1.1" sql-bricks-sqlite "^0.1.0" daf-debug@../../packages/daf-debug: - version "2.5.0" + version "3.0.2" dependencies: - daf-core "^2.5.0" + daf-core "^3.0.2" debug "^4.1.1" daf-did-comm@../../packages/daf-did-comm: - version "2.5.0" + version "3.0.2" dependencies: - daf-core "^2.5.0" + daf-core "^3.0.2" debug "^4.1.1" uuid "^3.3.3" -daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^2.5.0: - version "2.5.0" +daf-did-jwt@../../packages/daf-did-jwt, daf-did-jwt@^3.0.2: + version "3.0.2" dependencies: - daf-core "^2.5.0" + daf-core "^3.0.2" debug "^4.1.1" did-jwt "^4.0.0" did-resolver "^1.1.0" daf-ethr-did@../../packages/daf-ethr-did: - version "2.5.0" + version "3.0.2" dependencies: - daf-core "^2.5.0" + daf-core "^3.0.2" debug "^4.1.1" ethjs-provider-signer "^0.1.4" ethr-did "^1.1.0" js-sha3 "^0.8.0" daf-libsodium@../../packages/daf-libsodium: - version "2.5.0" + version "3.0.2" dependencies: base-58 "^0.0.1" - daf-core "^2.5.0" + daf-core "^3.0.2" debug "^4.1.1" did-jwt "^4.0.0" elliptic "^6.5.2" @@ -671,13 +671,13 @@ daf-libsodium@../../packages/daf-libsodium: libsodium-wrappers "^0.7.6" daf-resolver-universal@../../packages/daf-resolver-universal: - version "2.3.18" + version "3.0.1" dependencies: cross-fetch "^3.0.4" debug "^4.1.1" daf-resolver@../../packages/daf-resolver: - version "2.3.18" + version "3.0.1" dependencies: debug "^4.1.1" did-resolver "^1.1.0" @@ -686,28 +686,28 @@ daf-resolver@../../packages/daf-resolver: web-did-resolver "^1.2.0" daf-selective-disclosure@../../packages/daf-selective-disclosure: - version "2.5.0" + version "3.0.2" dependencies: blakejs "^1.1.0" - daf-core "^2.5.0" - daf-data-store "^2.5.0" - daf-did-jwt "^2.5.0" + daf-core "^3.0.2" + daf-data-store "^3.0.2" + daf-did-jwt "^3.0.2" debug "^4.1.1" did-jwt "^4.0.0" daf-url@../../packages/daf-url: - version "2.5.0" + version "3.0.2" dependencies: - daf-core "^2.5.0" + daf-core "^3.0.2" debug "^4.1.1" url-parse "^1.4.7" daf-w3c@../../packages/daf-w3c: - version "2.5.0" + version "3.0.2" dependencies: blakejs "^1.1.0" - daf-core "^2.5.0" - daf-did-jwt "^2.5.0" + daf-core "^3.0.2" + daf-did-jwt "^3.0.2" debug "^4.1.1" did-jwt-vc "^0.1.3" did-resolver "^1.1.0" diff --git a/jest.json b/jest.json index 6120c9f0a..ceb46a104 100644 --- a/jest.json +++ b/jest.json @@ -20,5 +20,9 @@ "tsConfig": "./packages/tsconfig.settings.json" } }, - "testEnvironment": "node" + "testEnvironment": "node", + "automock": false, + "setupFiles": [ + "./setupJest.js" + ] } diff --git a/package.json b/package.json index eba040ede..39b8424ac 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "codecov": "^3.6.5", "husky": "^4.2.3", "jest": "^25.1.0", + "jest-fetch-mock": "^3.0.3", "lerna": "^3.20.2", "lerna-changelog": "^1.0.1", "prettier": "^1.19.1", diff --git a/packages/daf-cli/src/msg.ts b/packages/daf-cli/src/msg.ts index bb7488f62..737519342 100644 --- a/packages/daf-cli/src/msg.ts +++ b/packages/daf-cli/src/msg.ts @@ -15,6 +15,6 @@ program ) console.log(message) } catch (e) { - console.error(e) + console.error(e.message) } }) diff --git a/packages/daf-url/src/__tests__/message-validator.test.ts b/packages/daf-url/src/__tests__/message-validator.test.ts index 31b3a9864..dc1a486e1 100644 --- a/packages/daf-url/src/__tests__/message-validator.test.ts +++ b/packages/daf-url/src/__tests__/message-validator.test.ts @@ -1,5 +1,8 @@ import { Message, Core } from 'daf-core' import { MessageValidator } from '../index' +import fetchMock from "jest-fetch-mock" +fetchMock.enableMocks() + describe('daf-url', () => { const validator = new MessageValidator() @@ -27,4 +30,18 @@ describe('daf-url', () => { expect(validator.validate(message, core)).rejects.toEqual('Unsupported message type') expect(message.raw).toEqual(JWT) }) + + it('should try to load data from URL if URL is not standard', async() => { + const message = new Message({raw: 'https://example.com/public-profile.jwt'}) + fetchMock.mockResponse('mockbody') + expect.assertions(2) + try { + await validator.validate(message, core) + } catch (e) { + expect(e).toMatch('Unsupported message type'); + } + + expect(message.raw).toEqual('mockbody') + + }) }) diff --git a/packages/daf-url/src/message-validator.ts b/packages/daf-url/src/message-validator.ts index db48bc941..15c0edc3f 100644 --- a/packages/daf-url/src/message-validator.ts +++ b/packages/daf-url/src/message-validator.ts @@ -12,6 +12,17 @@ export class MessageValidator extends AbstractMessageValidator { debug('Detected standard URL') message.raw = parsed.query.c_i message.addMetaData({ type: 'URL', value: parsed.origin + parsed.pathname }) + } else if (parsed) { + try { + const url = message.raw + debug('Fetching URL', url) + const response = await fetch(url) + message.raw = await response.text() + message.addMetaData({ type: 'URL', value: url }) + } catch (e) { + console.log(e) + debug(e.message) + } } return super.validate(message, core) diff --git a/setupJest.js b/setupJest.js new file mode 100644 index 000000000..aa8a2f30f --- /dev/null +++ b/setupJest.js @@ -0,0 +1 @@ +require('jest-fetch-mock').enableMocks() \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 78383b05b..979919721 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6247,6 +6247,14 @@ jest-environment-node@^25.1.0: jest-mock "^25.1.0" jest-util "^25.1.0" +jest-fetch-mock@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz#31749c456ae27b8919d69824f1c2bd85fe0a1f3b" + integrity sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw== + dependencies: + cross-fetch "^3.0.4" + promise-polyfill "^8.1.3" + jest-get-type@^25.1.0: version "25.1.0" resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-25.1.0.tgz#1cfe5fc34f148dc3a8a3b7275f6b9ce9e2e8a876" @@ -8785,6 +8793,11 @@ promise-inflight@^1.0.1, promise-inflight@~1.0.1: resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3" integrity sha1-mEcocL8igTL8vdhoEputEsPAKeM= +promise-polyfill@^8.1.3: + version "8.1.3" + resolved "https://registry.yarnpkg.com/promise-polyfill/-/promise-polyfill-8.1.3.tgz#8c99b3cf53f3a91c68226ffde7bde81d7f904116" + integrity sha512-MG5r82wBzh7pSKDRa9y+vllNHz3e3d4CNj1PQE4BQYxLme0gKYYBm9YENq+UkEikyZ0XbiGWxYlVw3Rl9O/U8g== + promise-retry@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/promise-retry/-/promise-retry-1.1.1.tgz#6739e968e3051da20ce6497fb2b50f6911df3d6d"