mirror of
https://github.com/apotdevin/thunderhub.git
synced 2025-02-24 06:47:41 +01:00
* chore: 🔧 remove client * chore: 🔧 change cookie name * chore: 🔧 remove auth param * chore: 🔧 remove auth components * chore: 🔧 add getaccount query * fix: 🐛 tests * chore: 🔧 get account * chore: 🔧 status check * chore: 🔧 remove log * chore: 🔧 update apollo client * refactor: ♻️ server side props * chore: 🔧 ssr queries * chore: 🔧 more ssr queries * chore: 🔧 type check * chore: 🔧 increase nodeinfo limit Co-authored-by: apotdevin <apotdevincab@gmail.com>
72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
import { IncomingMessage, ServerResponse } from 'http';
|
|
import { getIp } from 'server/helpers/helpers';
|
|
import jwt from 'jsonwebtoken';
|
|
import { logger } from 'server/helpers/logger';
|
|
import {
|
|
readMacaroons,
|
|
readFile,
|
|
getAccounts,
|
|
} from 'server/helpers/fileHelpers';
|
|
import getConfig from 'next/config';
|
|
import { ContextType, SSOType } from 'server/types/apiTypes';
|
|
import cookie from 'cookie';
|
|
import { LndObject } from 'server/types/ln-service.types';
|
|
import { getAuthLnd } from 'server/helpers/auth';
|
|
import { appConstants } from 'server/utils/appConstants';
|
|
import { secret } from 'pages/api/v1';
|
|
|
|
const { serverRuntimeConfig } = getConfig();
|
|
const {
|
|
macaroonPath,
|
|
lnCertPath,
|
|
lnServerUrl,
|
|
accountConfigPath,
|
|
} = serverRuntimeConfig;
|
|
|
|
const ssoMacaroon = readMacaroons(macaroonPath);
|
|
const ssoCert = readFile(lnCertPath);
|
|
const accountConfig = getAccounts(accountConfigPath);
|
|
|
|
let sso: SSOType | null = null;
|
|
|
|
if (ssoMacaroon && lnServerUrl) {
|
|
sso = {
|
|
macaroon: ssoMacaroon,
|
|
socket: lnServerUrl,
|
|
cert: ssoCert,
|
|
};
|
|
}
|
|
|
|
export const getContext = (req: IncomingMessage, res: ServerResponse) => {
|
|
const ip = getIp(req);
|
|
|
|
const cookies = cookie.parse(req.headers.cookie ?? '') || {};
|
|
const auth = cookies[appConstants.cookieName];
|
|
|
|
let lnd: LndObject | null = null;
|
|
let id: string | null = null;
|
|
|
|
if (auth) {
|
|
try {
|
|
const data = jwt.verify(auth, secret) as { id: string };
|
|
if (data && data.id) {
|
|
lnd = getAuthLnd(data.id, sso, accountConfig);
|
|
id = data.id;
|
|
}
|
|
} catch (error) {
|
|
logger.silly('Authentication cookie failed');
|
|
}
|
|
}
|
|
|
|
const context: ContextType = {
|
|
ip,
|
|
lnd,
|
|
secret,
|
|
id,
|
|
sso,
|
|
accounts: accountConfig,
|
|
res,
|
|
};
|
|
|
|
return context;
|
|
};
|