From 3112ec8cba7a6732a470000ac7465b9ee5682932 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 28 Jan 2022 13:06:54 -0500 Subject: [PATCH 01/13] Add bedrock-did-io to project and http.js. --- lib/config.js | 4 ++-- lib/http.js | 1 + package.json | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/config.js b/lib/config.js index f6120da..bdd2ea2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -4,10 +4,10 @@ import bedrock from 'bedrock'; const {config} = bedrock; -const namespace = 'module-template-http'; +const namespace = 'bedrock-did-resolver-http'; const cfg = config[namespace] = {}; -const basePath = '/foo'; +const basePath = '/did'; cfg.routes = { basePath }; diff --git a/lib/http.js b/lib/http.js index a57c31d..3510d94 100644 --- a/lib/http.js +++ b/lib/http.js @@ -3,6 +3,7 @@ */ import {asyncHandler} from 'bedrock-express'; import bedrock from 'bedrock'; +import {didIo} from 'bedrock-did-io'; const {config} = bedrock; bedrock.events.on('bedrock-express.configure.routes', app => { diff --git a/package.json b/package.json index fb148e6..1203a5b 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "homepage": "https://github.com/digitalbazaar/bedrock-module-template-http", "dependencies": { + "bedrock-did-io": "^5.0.0", "esm": "^3.2.25" }, "peerDependencies": { From 960e010d6e6011bbe286aea137fa615a074de1a7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 28 Jan 2022 17:09:32 -0500 Subject: [PATCH 02/13] Add initial implementation with resolutionResult. --- lib/config.js | 5 ++++- lib/http.js | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/config.js b/lib/config.js index bdd2ea2..7d598cd 100644 --- a/lib/config.js +++ b/lib/config.js @@ -7,7 +7,10 @@ const {config} = bedrock; const namespace = 'bedrock-did-resolver-http'; const cfg = config[namespace] = {}; -const basePath = '/did'; +// support did:key and veres one keys by default +cfg.supportedMethods = ['key', 'v1']; + +const basePath = '/1.0/resolve/identifiers/:did'; cfg.routes = { basePath }; diff --git a/lib/http.js b/lib/http.js index 3510d94..34ff925 100644 --- a/lib/http.js +++ b/lib/http.js @@ -7,9 +7,27 @@ import {didIo} from 'bedrock-did-io'; const {config} = bedrock; bedrock.events.on('bedrock-express.configure.routes', app => { - const {routes} = config['module-template-http']; + const {routes, supportedMethods} = config['bedrock-did-resolver-http']; app.post( routes.basePath, - asyncHandler(async (/*req, res*/) => { + asyncHandler(async (req, res) => { + const resolutionResult = { + '@context': 'https://w3id.org/did-resolution/v1', + didDocument: null, + didDocumentMetadata: null, + did-dereferencing-metadata: {} + }; + const {did} = req.params; + const [did, method] = did.split(':'); + if(did != 'did') { + resolutionResult.did-dereferencing-metadata.error = 'invalid-didUrl'; + res.status(400).json(resolutionResult); + } + if(!supportedMethods.includes(method)){ + resolutionResult.did-dereferencing-metadata.error = 'method-not-supported'; + res.status(406).json(resolutionResult); + } + resolutionResult.didDocument = await didIo.get({did}); + res.json(resolutionResult); })); }); From 42106100be071d4b7f49319aca713132caea7b5d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 31 Jan 2022 09:48:17 -0500 Subject: [PATCH 03/13] Replace template with bedrock did resolver code. --- CHANGELOG.md | 2 +- README.md | 2 +- package.json | 8 ++++---- test/package.json | 8 ++++---- test/test.config.js | 2 +- test/test.js | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fc0dad..72069ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# bedrock-module-template-http ChangeLog +# bedrock-did-resolver-http ChangeLog ## 1.0.0 - TBD diff --git a/README.md b/README.md index 06e90df..ec170f2 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# bedrock-module-template-http \ No newline at end of file +# bedrock-did-resolver-http diff --git a/package.json b/package.json index 1203a5b..089b7bf 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "bedrock-module-template-http", + "name": "bedrock-did-resolver-http", "version": "0.0.1-0", "description": "Bedrock HTTP API", "main": "./lib", @@ -8,7 +8,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/digitalbazaar/bedrock-module-template-http" + "url": "https://github.com/digitalbazaar/bedrock-did-resolver-http" }, "keywords": [ "bedrock" @@ -19,9 +19,9 @@ "url": "https://digitalbazaar.com" }, "bugs": { - "url": "https://github.com/digitalbazaar/bedrock-module-template-http/issues" + "url": "https://github.com/digitalbazaar/bedrock-did-resolver-http/issues" }, - "homepage": "https://github.com/digitalbazaar/bedrock-module-template-http", + "homepage": "https://github.com/digitalbazaar/bedrock-did-resolver-http", "dependencies": { "bedrock-did-io": "^5.0.0", "esm": "^3.2.25" diff --git a/test/package.json b/test/package.json index 752b392..735c2f9 100644 --- a/test/package.json +++ b/test/package.json @@ -1,5 +1,5 @@ { - "name": "bedrock-module-template-http-test", + "name": "bedrock-did-resolver-http-test", "version": "0.0.1-0", "private": true, "scripts": { @@ -12,7 +12,7 @@ "bedrock": "^4.1.1", "bedrock-express": "^3.2.0", "bedrock-https-agent": "^2.0.0", - "bedrock-module-template-http": "file:..", + "bedrock-did-resolver-http": "file:..", "bedrock-mongodb": "^8.2.0", "bedrock-server": "^2.7.0", "bedrock-test": "^5.3.2", @@ -22,10 +22,10 @@ "nyc": { "excludeNodeModules": false, "include": [ - "node_modules/bedrock-module-template-http/**" + "node_modules/bedrock-did-resolver-http/**" ], "exclude": [ - "node_modules/bedrock-module-template-http/node_modules/**" + "node_modules/bedrock-did-resolver-http/node_modules/**" ] } } diff --git a/test/test.config.js b/test/test.config.js index 2bd6421..b5bb5cc 100644 --- a/test/test.config.js +++ b/test/test.config.js @@ -7,7 +7,7 @@ const {config} = require('bedrock'); const path = require('path'); // MongoDB -config.mongodb.name = 'bedrock_module_template_http_test'; +config.mongodb.name = 'bedrock_did_resolver_http_test'; config.mongodb.dropCollections.onInit = true; config.mongodb.dropCollections.collections = []; diff --git a/test/test.js b/test/test.js index cad3ac1..83a89e1 100644 --- a/test/test.js +++ b/test/test.js @@ -6,7 +6,7 @@ const bedrock = require('bedrock'); require('bedrock-https-agent'); require('bedrock-mongodb'); -require('bedrock-module-template-http'); +require('bedrock-did-resolver-http'); require('bedrock-test'); bedrock.start(); From 23d60f5383b8fb5a032dd64e72b61b548d289def Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 31 Jan 2022 10:05:10 -0500 Subject: [PATCH 04/13] Clean up code & add try catch to did resolution. --- lib/http.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/http.js b/lib/http.js index 34ff925..5fd85a3 100644 --- a/lib/http.js +++ b/lib/http.js @@ -15,19 +15,29 @@ bedrock.events.on('bedrock-express.configure.routes', app => { '@context': 'https://w3id.org/did-resolution/v1', didDocument: null, didDocumentMetadata: null, - did-dereferencing-metadata: {} + 'did-dereferencing-metadata': {} }; - const {did} = req.params; - const [did, method] = did.split(':'); - if(did != 'did') { - resolutionResult.did-dereferencing-metadata.error = 'invalid-didUrl'; - res.status(400).json(resolutionResult); + const {did} = req.params; + const [prefix, method] = did.split(':'); + if(prefix != 'did') { + resolutionResult['did-dereferencing-metadata'].error = 'invalid-didUrl'; + return res.status(400).json(resolutionResult); } - if(!supportedMethods.includes(method)){ - resolutionResult.did-dereferencing-metadata.error = 'method-not-supported'; - res.status(406).json(resolutionResult); + if(!supportedMethods.includes(method)) { + resolutionResult['did-dereferencing-metadata'].error = + 'method-not-supported'; + return res.status(406).json(resolutionResult); + } + try { + resolutionResult.didDocument = await didIo.get({did}); + } catch(e) { + // the spec doesn't seem to handle what occurs if the + // did resolver fails for reason unrelated to the did such + // as database timeouts. + resolutionResult['did-dereferencing-metadata'].error = + 'server-error'; + return res.status(500).json(resolutionResult); } - resolutionResult.didDocument = await didIo.get({did}); res.json(resolutionResult); })); }); From 47f526e66a2e099770d0184f81d498f25ada42d6 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 1 Feb 2022 08:54:02 -0500 Subject: [PATCH 05/13] Use InternalError & add logger. --- lib/http.js | 4 +++- lib/logger.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 lib/logger.js diff --git a/lib/http.js b/lib/http.js index 5fd85a3..a7f964f 100644 --- a/lib/http.js +++ b/lib/http.js @@ -4,6 +4,7 @@ import {asyncHandler} from 'bedrock-express'; import bedrock from 'bedrock'; import {didIo} from 'bedrock-did-io'; +import logger from './logger'; const {config} = bedrock; bedrock.events.on('bedrock-express.configure.routes', app => { @@ -31,11 +32,12 @@ bedrock.events.on('bedrock-express.configure.routes', app => { try { resolutionResult.didDocument = await didIo.get({did}); } catch(e) { + logger.error('DiD Resolution error', {error: e}); // the spec doesn't seem to handle what occurs if the // did resolver fails for reason unrelated to the did such // as database timeouts. resolutionResult['did-dereferencing-metadata'].error = - 'server-error'; + 'InternalError'; return res.status(500).json(resolutionResult); } res.json(resolutionResult); diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..73c0f40 --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,11 @@ + +/*! + * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved. + */ +'use strict'; + +import bedrock from 'bedrock'; + +const logger = bedrock.loggers.get('app').child('bedrock-did-resolver-http'); + +export default logger; From a25fe06430f24a8583d3750faa2ae4057aedc95c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 1 Feb 2022 11:03:29 -0500 Subject: [PATCH 06/13] Make valid did check better & camelCase didDerefencingMetadata. --- lib/http.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/http.js b/lib/http.js index a7f964f..b2f17b2 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,5 +1,5 @@ /*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved. */ import {asyncHandler} from 'bedrock-express'; import bedrock from 'bedrock'; @@ -16,16 +16,18 @@ bedrock.events.on('bedrock-express.configure.routes', app => { '@context': 'https://w3id.org/did-resolution/v1', didDocument: null, didDocumentMetadata: null, - 'did-dereferencing-metadata': {} + didDereferencingMetadata: {} }; const {did} = req.params; - const [prefix, method] = did.split(':'); - if(prefix != 'did') { - resolutionResult['did-dereferencing-metadata'].error = 'invalid-didUrl'; + // the second value should always be the method + const [prefix, method, id] = did.split(':'); + // a did must have a did prefix, method, and id + if(!((prefix === 'did') && method && id)) { + resolutionResult.didDereferencingMetadata.error = 'invalid-didUrl'; return res.status(400).json(resolutionResult); } if(!supportedMethods.includes(method)) { - resolutionResult['did-dereferencing-metadata'].error = + resolutionResult.didDereferencingMetadata.error = 'method-not-supported'; return res.status(406).json(resolutionResult); } @@ -36,8 +38,7 @@ bedrock.events.on('bedrock-express.configure.routes', app => { // the spec doesn't seem to handle what occurs if the // did resolver fails for reason unrelated to the did such // as database timeouts. - resolutionResult['did-dereferencing-metadata'].error = - 'InternalError'; + resolutionResult.didDereferencingMetadata.error = 'InternalError'; return res.status(500).json(resolutionResult); } res.json(resolutionResult); From 420187b21666ae8035ce615ab85ec25db7d1949e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 1 Feb 2022 12:49:46 -0500 Subject: [PATCH 07/13] Change route from default post to get. --- lib/http.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/http.js b/lib/http.js index b2f17b2..4262931 100644 --- a/lib/http.js +++ b/lib/http.js @@ -9,7 +9,8 @@ const {config} = bedrock; bedrock.events.on('bedrock-express.configure.routes', app => { const {routes, supportedMethods} = config['bedrock-did-resolver-http']; - app.post( + logger.info('ROUTES CONFIGURED', {routes, supportedMethods}); + app.get( routes.basePath, asyncHandler(async (req, res) => { const resolutionResult = { @@ -19,6 +20,7 @@ bedrock.events.on('bedrock-express.configure.routes', app => { didDereferencingMetadata: {} }; const {did} = req.params; + logger.debug('dereferencing DiD', {did}); // the second value should always be the method const [prefix, method, id] = did.split(':'); // a did must have a did prefix, method, and id @@ -34,7 +36,7 @@ bedrock.events.on('bedrock-express.configure.routes', app => { try { resolutionResult.didDocument = await didIo.get({did}); } catch(e) { - logger.error('DiD Resolution error', {error: e}); + logger.error('DID Resolution error', {error: e}); // the spec doesn't seem to handle what occurs if the // did resolver fails for reason unrelated to the did such // as database timeouts. From f59a6cc12ffe3fc9de210c1ea4f40ba0ec58ac68 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 1 Feb 2022 14:58:03 -0500 Subject: [PATCH 08/13] Remove excessive logging. --- lib/http.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/http.js b/lib/http.js index 4262931..99017be 100644 --- a/lib/http.js +++ b/lib/http.js @@ -9,7 +9,6 @@ const {config} = bedrock; bedrock.events.on('bedrock-express.configure.routes', app => { const {routes, supportedMethods} = config['bedrock-did-resolver-http']; - logger.info('ROUTES CONFIGURED', {routes, supportedMethods}); app.get( routes.basePath, asyncHandler(async (req, res) => { @@ -20,7 +19,6 @@ bedrock.events.on('bedrock-express.configure.routes', app => { didDereferencingMetadata: {} }; const {did} = req.params; - logger.debug('dereferencing DiD', {did}); // the second value should always be the method const [prefix, method, id] = did.split(':'); // a did must have a did prefix, method, and id From 8acffce8c3f8941acb91f14b139b065ffb453d8f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 1 Feb 2022 16:44:58 -0500 Subject: [PATCH 09/13] Use Camel Cased error names. --- lib/http.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/http.js b/lib/http.js index 99017be..bb534ba 100644 --- a/lib/http.js +++ b/lib/http.js @@ -23,12 +23,12 @@ bedrock.events.on('bedrock-express.configure.routes', app => { const [prefix, method, id] = did.split(':'); // a did must have a did prefix, method, and id if(!((prefix === 'did') && method && id)) { - resolutionResult.didDereferencingMetadata.error = 'invalid-didUrl'; + resolutionResult.didDereferencingMetadata.error = 'invalidDidUrl'; return res.status(400).json(resolutionResult); } if(!supportedMethods.includes(method)) { resolutionResult.didDereferencingMetadata.error = - 'method-not-supported'; + 'representationNotSupported'; return res.status(406).json(resolutionResult); } try { From f94234d7e56127ec49c03269af508add7b10f5cf Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 4 Feb 2022 17:04:30 -0500 Subject: [PATCH 10/13] Reduce size of basePath in configs. --- lib/config.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/config.js b/lib/config.js index 7d598cd..21a7294 100644 --- a/lib/config.js +++ b/lib/config.js @@ -11,6 +11,4 @@ const cfg = config[namespace] = {}; cfg.supportedMethods = ['key', 'v1']; const basePath = '/1.0/resolve/identifiers/:did'; -cfg.routes = { - basePath -}; +cfg.routes = {basePath}; From bccb3e7c2c5876010f243e436db594bf2cb13c04 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 7 Feb 2022 10:14:13 -0500 Subject: [PATCH 11/13] Toggle between derefencing and resolution metadata. --- lib/http.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/http.js b/lib/http.js index bb534ba..436243e 100644 --- a/lib/http.js +++ b/lib/http.js @@ -12,23 +12,33 @@ bedrock.events.on('bedrock-express.configure.routes', app => { app.get( routes.basePath, asyncHandler(async (req, res) => { + // this can be resolution or dereferencing meta data + const metaData = {}; const resolutionResult = { '@context': 'https://w3id.org/did-resolution/v1', - didDocument: null, + didDocument: {}, didDocumentMetadata: null, - didDereferencingMetadata: {} + didResolutionMetadata: metaData }; const {did} = req.params; + const parsedDid = new URL(did); + // if there is a fragment `#` or a service `?service` + // then we are dereferencing a did url + if(parsedDid.hash || parsedDid.search) { + // add a did dereferencing meta + resolutionResult.didDereferencingMetadata = metaData; + // delete the resolution metadata + delete resolutionResult.didResolutionMetadata; + } // the second value should always be the method const [prefix, method, id] = did.split(':'); // a did must have a did prefix, method, and id if(!((prefix === 'did') && method && id)) { - resolutionResult.didDereferencingMetadata.error = 'invalidDidUrl'; + metaData.error = 'invalidDidUrl'; return res.status(400).json(resolutionResult); } if(!supportedMethods.includes(method)) { - resolutionResult.didDereferencingMetadata.error = - 'representationNotSupported'; + metaData.error = 'representationNotSupported'; return res.status(406).json(resolutionResult); } try { @@ -38,7 +48,7 @@ bedrock.events.on('bedrock-express.configure.routes', app => { // the spec doesn't seem to handle what occurs if the // did resolver fails for reason unrelated to the did such // as database timeouts. - resolutionResult.didDereferencingMetadata.error = 'InternalError'; + metaData.error = 'InternalError'; return res.status(500).json(resolutionResult); } res.json(resolutionResult); From dd3e42e691fdd96ea8192b23afe0d5e780c0903b Mon Sep 17 00:00:00 2001 From: Matthew Collier Date: Mon, 7 Feb 2022 10:20:52 -0500 Subject: [PATCH 12/13] Update peer dependencies. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 089b7bf..257276e 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "esm": "^3.2.25" }, "peerDependencies": { - "bedrock": "^4.1.1", - "bedrock-express": "^3.2.0" + "bedrock": "^4.4.3", + "bedrock-express": "^6.3.0" }, "directories": { "lib": "./lib" From bac73bc45b13f9c8ef252a763bfada17ef849c50 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 10 Feb 2022 09:30:21 -0500 Subject: [PATCH 13/13] Add propery invalidDid error when resolving + FIXMEs. --- lib/http.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/http.js b/lib/http.js index 436243e..19f593d 100644 --- a/lib/http.js +++ b/lib/http.js @@ -24,7 +24,8 @@ bedrock.events.on('bedrock-express.configure.routes', app => { const parsedDid = new URL(did); // if there is a fragment `#` or a service `?service` // then we are dereferencing a did url - if(parsedDid.hash || parsedDid.search) { + const didUrl = (parsedDid.hash || parsedDid.search); + if(didUrl) { // add a did dereferencing meta resolutionResult.didDereferencingMetadata = metaData; // delete the resolution metadata @@ -34,19 +35,23 @@ bedrock.events.on('bedrock-express.configure.routes', app => { const [prefix, method, id] = did.split(':'); // a did must have a did prefix, method, and id if(!((prefix === 'did') && method && id)) { - metaData.error = 'invalidDidUrl'; + metaData.error = didUrl ? 'invalidDidUrl' : 'invalidDid'; return res.status(400).json(resolutionResult); } if(!supportedMethods.includes(method)) { + //FIXME this might not be the right error code metaData.error = 'representationNotSupported'; return res.status(406).json(resolutionResult); } try { resolutionResult.didDocument = await didIo.get({did}); } catch(e) { + //FIXME the did resolver error could contain invalidDid, notFound, + //or representationNotSupported we need to check for those errors + //and newer errors and add that information in the future logger.error('DID Resolution error', {error: e}); // the spec doesn't seem to handle what occurs if the - // did resolver fails for reason unrelated to the did such + // did resolver fails for reasons unrelated to the did such // as database timeouts. metaData.error = 'InternalError'; return res.status(500).json(resolutionResult);