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/lib/config.js b/lib/config.js index f6120da..21a7294 100644 --- a/lib/config.js +++ b/lib/config.js @@ -4,10 +4,11 @@ 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'; -cfg.routes = { - basePath -}; +// 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 a57c31d..19f593d 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,14 +1,61 @@ /*! - * 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'; +import {didIo} from 'bedrock-did-io'; +import logger from './logger'; const {config} = bedrock; bedrock.events.on('bedrock-express.configure.routes', app => { - const {routes} = config['module-template-http']; - app.post( + const {routes, supportedMethods} = config['bedrock-did-resolver-http']; + app.get( routes.basePath, - asyncHandler(async (/*req, res*/) => { + asyncHandler(async (req, res) => { + // this can be resolution or dereferencing meta data + const metaData = {}; + const resolutionResult = { + '@context': 'https://w3id.org/did-resolution/v1', + didDocument: {}, + didDocumentMetadata: null, + 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 + const didUrl = (parsedDid.hash || parsedDid.search); + if(didUrl) { + // 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)) { + 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 reasons unrelated to the did such + // as database timeouts. + metaData.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; diff --git a/package.json b/package.json index fb148e6..257276e 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,15 +19,16 @@ "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" }, "peerDependencies": { - "bedrock": "^4.1.1", - "bedrock-express": "^3.2.0" + "bedrock": "^4.4.3", + "bedrock-express": "^6.3.0" }, "directories": { "lib": "./lib" 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();