Created
December 14, 2020 19:36
-
-
Save tetratorus/31faca57da2d76e16e07782e85384413 to your computer and use it in GitHub Desktop.
Test JWT generator utilities
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
const FetchNodeDetails = require("@toruslabs/fetch-node-details/dist/fetchNodeDetails-node"); | |
const jwt = require("jsonwebtoken"); | |
const fs = require("fs"); | |
const SIGNING_KEY = fs.readFileSync("./keys/key.pem"); | |
const NETWORK_MAP = { | |
mainnet: { proxyAddress: "0x638646503746d5456209e33a2ff5e3226d698bea", network: "mainnet" }, | |
testnet: { proxyAddress: "0x4023d2a0D330bF11426B12C6144Cfb96B7fa6183", network: "ropsten" }, | |
}; | |
exports.proxyNetworkMiddleware = async (req, res, next) => { | |
try { | |
const { network = "mainnet" } = req.body; | |
const networkArgs = NETWORK_MAP[network]; | |
if (!networkArgs) { | |
return res.status(404).json({ error: { message: "Unsupported network" }, success: false }); | |
} | |
const nodeDetailsInstance = new FetchNodeDetails(networkArgs); | |
const nodeDetails = await nodeDetailsInstance.getNodeDetails(); | |
req.nodeDetails = nodeDetails; | |
return next(); | |
} catch (error) { | |
console.error(error); | |
return res.status(500).json({ error: error.message, stack: error.stack, success: false }); | |
} | |
}; | |
exports.generateIdToken = (email) => { | |
const payload = { | |
email, | |
nickname: email.split("@")[0], | |
name: email, | |
picture: "", | |
email_verified: true, | |
}; | |
return jwt.sign(payload, SIGNING_KEY, { | |
algorithm: "RS256", | |
subject: `email|${email.split("@")[0]}`, | |
audience: "torus-key-test", | |
expiresIn: "1h", | |
issuer: "torus-key-test", | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment