Created
August 25, 2015 05:47
-
-
Save rksm/8df2a8efbf51cf7495d0 to your computer and use it in GitHub Desktop.
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
var exec = require("child_process").exec; | |
var path = require("path"); | |
var fs = require('fs'); | |
var util = require("util"); | |
var enabled = false; | |
var sslDir = path.join(process.env.HOME, "lively-web.org/http/ssl/"); | |
var outDir = path.join(sslDir, "lively-web-CA"); | |
var registerCertScript = path.join(sslDir, "register-cert.sh"); | |
var registerURL = "/certificate-request"; | |
var registerHTML = "<html>\n" | |
+ "<head><title>Register an internal.lively-web.org certificate</title></head>\n" | |
+ "<body>\n" | |
+ " <h1>Register an internal.lively-web.org certificate</h1>\n" | |
+ " <h2 style=\"display: none;\" id=\"note\"></h2>\n" | |
+ " <form method=\"post\" action=\"" + registerURL + "\">\n" | |
+ " <p><input type=\"text\" name=\"name\" value=\"\" placeholder=\"your name\"></p>\n" | |
+ " <p><input type=\"email\" name=\"email\" valude=\"\" placeholder=\"email\"></p>\n" | |
+ " <p><input type=\"password\" name=\"password\" value=\"\" placeholder=\"password\"></p>\n" | |
+ " <p class=\"submit\"><input type=\"submit\" name=\"action\" value=\"Request certificate\"></p>\n" | |
+ " </form>\n" | |
+ "</body>\n" | |
+ "</html>\n" | |
function registerCert(userName, email, password, thenDo) { | |
if (!userName.match(/^[0-9_\-a-z]+$/i)) return thenDo(new Error("Invalid username: " + userName)); | |
var certFile = path.join(outDir, "certs/client_" + userName + ".p12"); | |
if (fs.existsSync(certFile)) return thenDo(new Error("Certificate for " + userName + " already exists!")); | |
var cmd = util.format("%s %s '%s' '%s' '%s'", registerCertScript, outDir, userName, email, password); | |
exec(cmd, {}, function(code, out, err) { | |
if (code) thenDo(new Error(out + "\n" + err)); | |
else if (!fs.existsSync(certFile)) thenDo(new Error("Could not create cert file " + certFile)); | |
else thenDo(null, path.basename(certFile), fs.createReadStream(certFile)); | |
}); | |
} | |
module.exports = function(route, app) { | |
if (enabled) { | |
app.get(route+"test", function(req, res) { | |
res.set("Content-disposition", "attachment; filename=fname.ext"); | |
res.set("Content-Type", "application/octet-stream"); | |
res.end("LivelyInternalCertGenerator is running!"); | |
}); | |
app.get(registerURL, function(req, res) { | |
res.contentType("text/html"); | |
res.end(registerHTML); | |
}); | |
app.post(registerURL, function(req, res) { | |
var data = req.body, err; | |
if (!data.name) err = "no user name"; | |
else if (!data.email) err = "no email"; | |
else if (!data.password) err = "no password"; | |
if (err) { req.status(400).end(err); return; } | |
registerCert(data.name, data.email, data.password, function(err, fileName, certStream) { | |
if (err) { res.status(400).end(String(err)); return; } | |
res.header({ | |
"Content-Type": "application/octet-stream", | |
"Content-disposition": "attachment; filename=" + fileName}); | |
certStream.pipe(res); | |
}); | |
}); | |
} | |
app.get(route, function(req, res) { | |
res.end("LivelyInternalCertGenerator is running!"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment