Skip to content

Instantly share code, notes, and snippets.

@rawberg
Last active April 9, 2020 23:53
Show Gist options
  • Save rawberg/ee61e6a364ecbb487eec3a2682ee288e to your computer and use it in GitHub Desktop.
Save rawberg/ee61e6a364ecbb487eec3a2682ee288e to your computer and use it in GitHub Desktop.
const config = require("./config.js");
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
const crypto = require("crypto");
// activate UTC plugin
dayjs.extend(utc);
function getClientIp(req) {
return (req.headers["X-Forwarded-For"] ||
req.headers["x-forwarded-for"] ||
'').split(',')[0] ||
req.connection.remoteAddress;
}
function getOloServerAuthHeaders(req, proxyPath) {
const oloId = config.OLO_CLIENT_ID;
const oloSecret = config.OLO_SECRET;
const contentType = req.headers["content-type"] || "application/json";
// RFC1123Pattern
const timestamp = `${dayjs.utc().format("ddd, DD MMM YYYY HH:mm:ss")} GMT`;
const hashedBody = crypto.createHash("sha256")
.update(req.body || "")
.digest("base64");
const signatureString = [
oloId,
req.method,
contentType,
hashedBody,
proxyPath,
timestamp
].join("\n");
console.log(signatureString);
const hmac = crypto.createHmac("sha256", oloSecret);
const hashedSignature = hmac.update(signatureString).digest("base64");
return {
"Content-Type": contentType,
"Authorization": `OloSignature ${oloId}:${hashedSignature}`,
"Date": timestamp,
"X-Forwarded-For": getClientIp(req),
"User-Agent": "Olo-Proxy",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment