thunderhub/pages/api/v1.tsx

97 lines
2.4 KiB
TypeScript
Raw Normal View History

import crypto from 'crypto';
import { ApolloServer } from 'apollo-server-micro';
2020-05-13 07:15:10 +02:00
import { thunderHubSchema } from 'api/schemas';
import { getIp } from 'api/helpers/helpers';
2020-04-14 23:02:33 +02:00
import getConfig from 'next/config';
import jwt from 'jsonwebtoken';
import { logger } from 'api/helpers/logger';
import {
readMacaroons,
readFile,
readCookie,
getAccounts,
} from 'api/helpers/fileHelpers';
import { ContextType } from 'api/types/apiTypes';
import AES from 'crypto-js/aes';
import CryptoJS from 'crypto-js';
import cookie from 'cookie';
const { publicRuntimeConfig, serverRuntimeConfig } = getConfig();
const { apiBaseUrl, nodeEnv } = publicRuntimeConfig;
const {
cookiePath,
macaroonPath,
lnCertPath,
lnServerUrl,
accountConfigPath,
} = serverRuntimeConfig;
const secret =
nodeEnv === 'development'
? '123456789'
: crypto.randomBytes(64).toString('hex');
const ssoMacaroon = readMacaroons(macaroonPath);
const ssoCert = readFile(lnCertPath);
const accountConfig = getAccounts(accountConfigPath);
readCookie(cookiePath);
2020-04-14 23:02:33 +02:00
const apolloServer = new ApolloServer({
schema: thunderHubSchema,
context: ({ req, res }) => {
const ip = getIp(req);
const { AccountAuth, SSOAuth } = cookie.parse(req.headers.cookie ?? '');
let ssoVerified = false;
if (SSOAuth) {
2020-05-23 14:46:47 +02:00
logger.silly('SSOAuth cookie found in request');
if (nodeEnv === 'development') {
ssoVerified = true;
}
try {
jwt.verify(SSOAuth, secret);
ssoVerified = true;
} catch (error) {
logger.silly('SSO authentication cookie failed');
}
}
let account = null;
if (AccountAuth) {
2020-05-23 14:46:47 +02:00
logger.silly('AccountAuth cookie found in request');
try {
const cookieAccount = jwt.verify(AccountAuth, secret);
const id = cookieAccount['id'] || '';
const bytes = AES.decrypt(cookieAccount['password'], secret);
const password = bytes.toString(CryptoJS.enc.Utf8);
account = { id, password };
} catch (error) {
logger.silly('Account authentication cookie failed');
}
}
const context: ContextType = {
ip,
secret,
ssoVerified,
account,
sso: { macaroon: ssoMacaroon, cert: ssoCert, host: lnServerUrl || null },
accounts: accountConfig,
res,
};
return context;
},
});
export const config = {
api: {
bodyParser: false,
},
};
export default apolloServer.createHandler({ path: apiBaseUrl });