-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(identity): resolve DID from url and tests (#263)
Fix [#261](#261) --------- Co-authored-by: Luckydd99 <[email protected]>
- Loading branch information
1 parent
378d393
commit ba1cbb7
Showing
9 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@restroom-mw/identity", | ||
"version": "0.13.0", | ||
"description": "Identity utilities middleware for Restroom", | ||
"author": "Rebecca Selvaggini <[email protected]>", | ||
"contributors": [ | ||
"Luca Di Domenico <[email protected]>" | ||
], | ||
"homepage": "https://dyne.github.io/restroom-mw/", | ||
"license": "AGPL-3.0", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"src", | ||
"dist" | ||
], | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/dyne/restroom-mw.git" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf dist && rimraf tsconfig.tsbuildinfo", | ||
"doc": "documentation readme src/** -g -f md -s 'API' --readme-file ../../docs/packages/identity.md --shallow", | ||
"links": "yarn link", | ||
"unlinks": "yarn unlink", | ||
"watch": "tsc -b tsconfig.json -w", | ||
"build": "tsc -b tsconfig.json" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/dyne/restroom-mw/issues" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.26.0" | ||
}, | ||
"gitHead": "5015ad3c33829d0690ed914900c45f0576b70aee" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum Action { | ||
/** | ||
* `Given I resolve the did 'myDid' from 'myUrl' and save the output into 'myOutput'`<br><br> | ||
* Resolve 'myDid' using the resolver in 'myUrl' and save the resolved did json inside 'myOutput'. | ||
* By repeting the statement *n* times, it will perform *n* parallel get. | ||
* @param {string} myDid - Name of the variable containing the did to be resolved | ||
* @param {string} myUrl - Name of the variable containing the url of the resolver | ||
* @param {string} myOutput - Name of the variable where the output json will be stored | ||
*/ | ||
RESOLVE_DID = "resolve the did {} from {} and save the output into {}", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Restroom } from "@restroom-mw/core"; | ||
import { ObjectLiteral } from "@restroom-mw/types"; | ||
import { Zencode } from "@restroom-mw/zencode"; | ||
import { NextFunction, Request, Response } from "express"; | ||
import axios from "axios"; | ||
import { Action } from "./actions"; | ||
import { chunks } from "@restroom-mw/http/src/utils"; | ||
|
||
export default (req: Request, res: Response, next: NextFunction) => { | ||
const rr = new Restroom(req, res); | ||
let content: ObjectLiteral = {}; | ||
let promises: Promise<any>[] = []; | ||
let outputs: string[] = []; | ||
|
||
rr.onBefore( | ||
async (params: { | ||
zencode: Zencode; | ||
keys: ObjectLiteral; | ||
data: ObjectLiteral; | ||
}) => { | ||
let { zencode, keys, data } = params; | ||
content = rr.combineDataKeys(data, keys); | ||
|
||
if (zencode.match(Action.RESOLVE_DID)) { | ||
for (let [did, urlName, o] of chunks(zencode.paramsOf(Action.RESOLVE_DID), 3)) { | ||
let check_url : URL; | ||
let url_resolver : URL; | ||
try { | ||
check_url = new URL(content[urlName]); | ||
} catch (err) { | ||
throw new Error("The string " + content[urlName] + " is an invalid URL"); | ||
} | ||
try{ | ||
url_resolver = new URL(check_url.toString() + "/" + content[did]); | ||
} catch(err){ | ||
throw new Error("The string " + content[did] + " is an invalid URL"); | ||
} | ||
promises.push(axios.get(url_resolver.toString())); | ||
outputs.push(o); | ||
} | ||
} | ||
if(promises.length){ | ||
const result = await Promise.all(promises); | ||
result.map((r, i) => { | ||
const out = outputs[i]; | ||
data[out] = r.data; | ||
}); | ||
} | ||
} | ||
) | ||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import test from "ava"; | ||
import request from "supertest"; | ||
import express from "express"; | ||
import bodyParser from "body-parser"; | ||
|
||
process.env.ZENCODE_DIR = "./test/identity"; | ||
const identity = require("..").default; | ||
const zencode = require("../../core").default; | ||
|
||
test("Resolve did", | ||
async (t) => { | ||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use(identity); | ||
app.use("/*", zencode); | ||
const res = await request(app).post("/resolve-did"); | ||
t.true( | ||
Object.keys(res.body).includes("myOutput"), | ||
'could not find "myOutput " in response' | ||
); | ||
let output = res.body.myOutput; | ||
t.true( | ||
Object.keys(output).includes("didDocument"), | ||
'could not find "didDocument" in myOutput' | ||
); | ||
t.true( | ||
Object.keys(output).includes("didDocumentMetadata"), | ||
'could not find "didDocumentMetadata" in myOutput' | ||
); | ||
t.true( | ||
Object.keys(output).includes("@context"), | ||
'could not find "@context" in myOutput' | ||
); | ||
t.true( | ||
Object.keys(res.body).includes("myOutput2"), | ||
'could not find "myOutput2 " in response' | ||
); | ||
output = res.body.myOutput2; | ||
t.true( | ||
Object.keys(output).includes("didDocument"), | ||
'could not find "didDocument" in myOutput2' | ||
); | ||
t.true( | ||
Object.keys(output).includes("didDocumentMetadata"), | ||
'could not find "didDocumentMetadata" in myOutput2' | ||
); | ||
t.true( | ||
Object.keys(output).includes("@context"), | ||
'could not find "@context" in myOutput2' | ||
); | ||
t.is(res.status, 200); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"rootDir": "./src" | ||
}, | ||
"references": [{ | ||
"path": "../core/" | ||
}, { | ||
"path": "../types" | ||
}], | ||
"exclude": ["dist", "tests"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"myUrl": "https://did.dyne.org/dids", | ||
"myDid": "did:dyne:sandbox.zenswarm:HwSjX94eXVmVpqGnN6L5HD3rjSBwxJeNc9xByJKB83wB", | ||
"myDid2": "did:dyne:sandbox.test:AR4tjXRTHzoiUoR8pcPrZRbHPFeR9LLqQLWfAhJrehEv" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Rule caller restroom-mw | ||
|
||
Given I have a 'string' named 'myDid' | ||
Given I have a 'string' named 'myDid2' | ||
Given I have a 'string' named 'myUrl' | ||
Given I have a 'string dictionary' named 'myOutput' | ||
Given I have a 'string dictionary' named 'myOutput2' | ||
Given I resolve the did 'myDid' from 'myUrl' and save the output into 'myOutput' | ||
Given I resolve the did 'myDid2' from 'myUrl' and save the output into 'myOutput2' | ||
Then print all data |