chore: changes to auth

This commit is contained in:
AP 2019-11-30 11:49:47 +01:00
parent a3155e236a
commit d045d5be6c
3 changed files with 49 additions and 31 deletions

7
.prettierrc Normal file
View file

@ -0,0 +1,7 @@
{
"printWidth": 100,
"trailingComma": "all",
"tabWidth": 4,
"semi": true,
"singleQuote": true
}

View file

@ -52,7 +52,7 @@
}, },
"husky": { "husky": {
"hooks": { "hooks": {
"pre-commit": "pretty-quick --staged" "pre-commit": "pretty-quick"
} }
} }
} }

View file

@ -2,47 +2,58 @@ import base64url from "base64url";
import { authenticatedLndGrpc } from "ln-service"; import { authenticatedLndGrpc } from "ln-service";
export const getIp = (req: any) => { export const getIp = (req: any) => {
if (!req || !req.headers) { if (!req || !req.headers) {
return ""; return "";
} }
const forwarded = req.headers["x-forwarded-for"]; const forwarded = req.headers["x-forwarded-for"];
const before = forwarded const before = forwarded
? forwarded.split(/, /)[0] ? forwarded.split(/, /)[0]
: req.connection.remoteAddress; : req.connection.remoteAddress;
const ip = process.env.NODE_ENV === "development" ? "1.2.3.4" : before; const ip = process.env.NODE_ENV === "development" ? "1.2.3.4" : before;
return ip; return ip;
}; };
export const getBase64CertfromDerFormat = (url: string) => { export const getBase64CertfromDerFormat = (url: string) => {
if (!url) return null; if (!url) return null;
const base64 = base64url.toBase64(url); const base64 = base64url.toBase64(url);
const prefix = "-----BEGIN CERTIFICATE-----\n"; const prefix = "-----BEGIN CERTIFICATE-----\n";
const postfix = "-----END CERTIFICATE-----"; const postfix = "-----END CERTIFICATE-----";
const pem = base64.match(/.{0,64}/g) || []; const pem = base64.match(/.{0,64}/g) || [];
const pemString = pem.join("\n"); const pemString = pem.join("\n");
const pemComplete = prefix + pemString + postfix; const pemComplete = prefix + pemString + postfix;
const pemText = base64url.encode(pemComplete); const pemText = base64url.encode(pemComplete);
return pemText; return pemText;
}; };
export const getAuthLnd = (auth: string) => { export const getAuthLnd = (auth: string) => {
const url = new URL(auth); const url = new URL(auth);
const encodedCert = url.searchParams.get("cert") || ""; // console.log(url);
const encodedMacaroon = url.searchParams.get("macaroon") || ""; const encodedCert = url.searchParams.get("cert") || "";
const socket = url.host; const encodedMacaroon = url.searchParams.get("macaroon") || "";
const socket = url.host;
const cert = getBase64CertfromDerFormat(encodedCert); const cert = getBase64CertfromDerFormat(encodedCert);
const macaroon = base64url.toBase64(encodedMacaroon); // const macaroon = base64url.toBase64(encodedMacaroon);
const { lnd } = authenticatedLndGrpc({ // const cert = base64url.toBase64(encodedCert);
cert, const macaroon = base64url.toBase64(encodedMacaroon);
macaroon,
socket
});
return lnd; // console.log(encodedCert);
const params =
encodedCert !== ""
? {
cert,
macaroon,
socket
}
: { macaroon, socket };
const { lnd } = authenticatedLndGrpc(params);
return lnd;
}; };