Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(identity): resolve DID from url and tests #263

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ function initializeSingleContext(block:string):BlockContext{
export const addMiddlewares =
async (baseUrl: string, app: Express) => {
const mws = [
'timestamp', 'git', 'db', 'files', 'redis', 'influxdb', 'http',
'timestamp', 'identity', 'git', 'db', 'files', 'redis', 'influxdb', 'http',
'fabric', 'planetmint', 'sawroom', 'ethereum', 'logger', 'ui',

]
Expand Down
6 changes: 6 additions & 0 deletions packages/create-restroom/templates/restroom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import zencode from "@restroom-mw/core";
{{#@restroom-mw/timestamp}}
import timestamp from "@restroom-mw/timestamp";
{{/@restroom-mw/timestamp}}
{{#@restroom-mw/identity}}
import identity from "@restroom-mw/identity";
{{/@restroom-mw/identity}}
{{#@restroom-mw/git}}
import git from "@restroom-mw/git";
{{/@restroom-mw/git}}
Expand Down Expand Up @@ -63,6 +66,9 @@ app.use(bodyParser.json());
app.use(morgan("dev"));
app.set("json spaces", 2);

{{#@restroom-mw/identity}}
app.use(identity.default);
{{/@restroom-mw/identity}}
{{#@restroom-mw/db}}
app.use(db.default);
{{/@restroom-mw/db}}
Expand Down
38 changes: 38 additions & 0 deletions packages/identity/package.json
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"
}
11 changes: 11 additions & 0 deletions packages/identity/src/actions.ts
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 {}",
}
52 changes: 52 additions & 0 deletions packages/identity/src/index.ts
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();
}
52 changes: 52 additions & 0 deletions packages/identity/tests/index.js
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);
});
13 changes: 13 additions & 0 deletions packages/identity/tsconfig.json
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"]
}
5 changes: 5 additions & 0 deletions test/identity/resolve-did.keys
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"
}
10 changes: 10 additions & 0 deletions test/identity/resolve-did.zen
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