Skip to content

Commit

Permalink
feat: Fetching URL to get the message payload
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 16, 2020
1 parent 5c05bb3 commit 72596e4
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 33 deletions.
12 changes: 8 additions & 4 deletions examples/expressjs-ethr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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('/')))
Expand Down
54 changes: 27 additions & 27 deletions examples/expressjs-ethr/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -613,71 +613,71 @@ 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"
ethjs-signer "^0.1.1"
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"
Expand All @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
"tsConfig": "./packages/tsconfig.settings.json"
}
},
"testEnvironment": "node"
"testEnvironment": "node",
"automock": false,
"setupFiles": [
"./setupJest.js"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-cli/src/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ program
)
console.log(message)
} catch (e) {
console.error(e)
console.error(e.message)
}
})
17 changes: 17 additions & 0 deletions packages/daf-url/src/__tests__/message-validator.test.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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')

})
})
11 changes: 11 additions & 0 deletions packages/daf-url/src/message-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions setupJest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('jest-fetch-mock').enableMocks()
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 72596e4

Please sign in to comment.