-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
190 lines (169 loc) · 5.09 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const dotenv = require("dotenv");
const express = require("express");
const session = require("express-session");
const service = require("./src/services");
const { requestLogger } = require("./src/utils");
// express app
const app = express();
// express router
const router = express();
// register view engine
app.set("view engine", "ejs");
let callbackState = {};
// Launch the Express server
(async function bootstrap() {
// Load environment variables
dotenv.config();
const port = process.env.PORT || "3000";
// If running locally, need to expose through ngrok or some tunnelling and set the public callback URL in the env configs.
app.listen(port, () => {
console.log(`Mattr Verifiable Credential Issuer`);
console.log(`Local: http://localhost:${port}`);
});
})().catch((err) => {
console.error("Failed to launch server", err);
process.exit(1);
});
// middleware & static files
// Set up a simple server side session store.
// The session store will briefly cache issuance requests
// to facilitate QR code scanning.
var sessionStore = new session.MemoryStore();
app.use(
session({
secret: "cookie-secret-key",
resave: false,
saveUninitialized: true,
store: sessionStore,
})
);
app.use(router);
app.use(express.static("public"));
// router.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Authorization, Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
module.exports.sessionStore = sessionStore;
module.exports.app = app;
// routes
router.post("/callback", function (req, res) {
var body = "";
req.on("data", function (data) {
body += data;
});
req.on("end", function () {
requestLogger(req);
var callbackResponse = JSON.parse(body.toString());
var challenge = callbackResponse.challengeId;
sessionStore.get(callbackState[challenge], (error, session) => {
var cacheData = {
status: "verified",
callbackResponse: callbackResponse.claims,
challenge: callbackResponse.challengeId,
};
session.sessionData = cacheData;
sessionStore.set(callbackState[challenge], session, (error) => {
res.send();
});
console.log("Session: ", session);
});
});
console.log("\n Data from the Presentation is shown below \n", body);
res.send(body);
});
app.get("/api/verifier/presentation-response", async (req, res) => {
var id = req.query.id;
var challenge = req.query.challenge;
requestLogger(req);
sessionStore.get(id, (error, session) => {
if (session && session.sessionData) {
console.log(
`status: ${session.sessionData.status}, callbackResponse: ${session.sessionData.callbackResponse}`
);
var data = session.sessionData;
res.status(200).json({ data, challenge });
}
});
});
router.get("/", async (req, res) => {
requestLogger(req);
res.render("index", { title: "Coffee Co. | Customer" });
});
router.get("/qr", async (req, res) => {
requestLogger(req);
const body = res.body;
const jwsUrl = await service.getJwsUrl(req.query.challenge);
res.redirect(jwsUrl);
});
// Get QR code for retrieving verifiable credentials.
router.get(
"/present/getVerifiableCredentials",
express.json(),
async (req, res, next) => {
requestLogger(req);
try {
console.log("Presenting QR Code");
const qrCode = await service.getCredentials();
res.send(qrCode);
} catch (err) {
console.error("Failed to present QR code", err, err?.response?.body);
next(err);
}
}
);
// Get QR code for validating credentials.
router.get(
"/present/validateCredentials",
express.json(),
async (req, res, next) => {
const { qrCode, challenge } = await service.validateCredentials();
requestLogger(req);
var id = req.session.id;
// prep a session state of 0
sessionStore.get(id, (error, session) => {
var sessionData = {
status: 0,
message: "Verification process initiated.",
};
if (session) {
session.sessionData = sessionData;
sessionStore.set(id, session);
callbackState[challenge] = id;
console.log("Session: ", session, callbackState[challenge]);
}
});
try {
console.log("Presenting QR Code");
res.status(200).json({ id: id, challenge: challenge, qrCode: qrCode });
} catch (err) {
console.error("Failed to present QR code", err, err?.response?.body);
next(err);
}
}
);
router.get("/waiting", (req, res) => {
requestLogger(req);
res.render("waiting", { title: "Coffee Co. | Cashier" });
});
router.get("/billing", (req, res) => {
requestLogger(req);
res.render("billing", { title: "Coffee Co. | Cashier" });
});
router.get("/customer-verify", (req, res) => {
requestLogger(req);
res.render("verify", { title: "Coffee Co. | Customer" });
});
router.get("/customer-checkout", (req, res) => {
requestLogger(req);
res.render("checkout", { title: "Coffee Co. | Customer" });
});
// 404 page
app.use((req, res) => {
requestLogger(req);
res.status(404).render("404", { title: "404" });
});