From 450f7e53ba0d52ddfd12edc161b78186d045dd91 Mon Sep 17 00:00:00 2001 From: Birm Date: Mon, 15 Jan 2024 19:57:43 -0500 Subject: [PATCH] add custom login fcn --- caracal.js | 2 ++ handlers/customLogin.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 handlers/customLogin.js diff --git a/caracal.js b/caracal.js index 91b0fd4..e2cdc91 100644 --- a/caracal.js +++ b/caracal.js @@ -22,6 +22,7 @@ const fileHandlers = require('./handlers/fileHandlers.js'); const sanitizeBody = require('./handlers/sanitizeHandler.js'); const DataTransformationHandler = require('./handlers/dataTransformationHandler.js'); const envEcho = require("./handlers/envEcho.js"); +const customLogin = require("./handlers/customLogin.js"); const mongoDB = require('./service/database'); const nodemailer = require('nodemailer'); @@ -86,6 +87,7 @@ var HANDLERS = { "proxyHandler": proxyHandler, "writeFile": fileHandlers.writeFile, "envEcho": envEcho, + "customLogin": customLogin, "iipHandler": function() { return iipHandler; }, diff --git a/handlers/customLogin.js b/handlers/customLogin.js new file mode 100644 index 0000000..972be4f --- /dev/null +++ b/handlers/customLogin.js @@ -0,0 +1,26 @@ +var jwt = require('jsonwebtoken'); + +var EXPIRY = process.env.EXPIRY || '1d'; + + +function customLogin(usernameField, usertypeField) { + return function(req, res) { + let token = {}; + // get user id from env + username = process.env[usernameField] || "unknown user"; + token.sub = username; + token.email = username; + token.name = username; + usertype = process.env[usertypeField]; + token.userType = usertype || "Participant"; + // generate token with this info + // return the token + signedToken = jwt.sign(data, "precision-fda-not-secure-token", { + algorithm: 'RS256', + expiresIn: EXPIRY, + }); + res.json({"token": signedToken}); + }; +} + +module.expoers = customLogin;