2021-02-03 14:06:44 +00:00
|
|
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
|
|
|
2021-02-04 13:31:26 +00:00
|
|
|
import jwt from "express-jwt";
|
|
|
|
import jwks from "jwks-rsa";
|
|
|
|
import config from "../../src/config";
|
|
|
|
|
|
|
|
// This middleware assumes that the app is secured using ORY Oathkeeper, in which case we
|
|
|
|
// verify the JSON Web Token issued by ORY Oathkeeper using the jwt-express middleware.
|
|
|
|
|
|
|
|
const middleware = jwt({
|
|
|
|
// Dynamically provide a signing key based on the kid in the header and the signing keys provided by the JWKS endpoint.
|
|
|
|
secret: jwks.expressJwtSecret({
|
|
|
|
cache: true,
|
|
|
|
jwksRequestsPerMinute: 5,
|
|
|
|
jwksUri: config.jwksUrl,
|
|
|
|
}),
|
|
|
|
algorithms: ["RS256"],
|
|
|
|
});
|
|
|
|
|
2021-02-05 10:54:05 +00:00
|
|
|
// Helper method to wait for a middleware to execute before continuing
|
|
|
|
// And to throw an error when an error happens in a middleware
|
|
|
|
function runMiddleware(req, res, fn) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fn(req, res, (result) => {
|
|
|
|
if (result instanceof Error) {
|
|
|
|
return reject(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-05 10:55:10 +00:00
|
|
|
export default async (req, res) => {
|
|
|
|
await runMiddleware(req, res, middleware);
|
2021-02-05 10:54:05 +00:00
|
|
|
|
2021-02-04 13:31:26 +00:00
|
|
|
console.log(Object.keys(req));
|
|
|
|
console.log(req);
|
|
|
|
console.log(JSON.stringify(req));
|
2021-02-03 14:06:44 +00:00
|
|
|
res.statusCode = 200;
|
|
|
|
res.json({ name: "John Doe" });
|
2021-02-05 10:54:05 +00:00
|
|
|
};
|