2020-05-19 07:50:16 +02:00
|
|
|
import crypto from 'crypto';
|
2020-04-12 18:27:01 +02:00
|
|
|
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';
|
2020-05-19 07:50:16 +02:00
|
|
|
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';
|
2020-04-12 18:27:01 +02:00
|
|
|
|
2020-05-19 07:50:16 +02:00
|
|
|
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
|
|
|
|
2020-04-12 18:27:01 +02:00
|
|
|
const apolloServer = new ApolloServer({
|
|
|
|
schema: thunderHubSchema,
|
2020-05-19 07:50:16 +02:00
|
|
|
context: async ({ req }) => {
|
2020-04-12 18:27:01 +02:00
|
|
|
const ip = getIp(req);
|
2020-05-19 07:50:16 +02:00
|
|
|
|
|
|
|
let ssoVerified = false;
|
|
|
|
if (req?.cookies?.SSOAuth) {
|
|
|
|
try {
|
|
|
|
jwt.verify(req.cookies.SSOAuth, secret);
|
|
|
|
ssoVerified = true;
|
|
|
|
} catch (error) {
|
2020-05-21 09:14:56 +02:00
|
|
|
logger.silly('SSO authentication cookie failed');
|
2020-05-19 07:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let account = null;
|
|
|
|
if (req?.cookies?.AccountAuth) {
|
|
|
|
try {
|
|
|
|
const bytes = AES.decrypt(req.cookies.AccountAuth, secret);
|
|
|
|
const decrypted = bytes.toString(CryptoJS.enc.Utf8);
|
|
|
|
const cookieAccount = jwt.verify(decrypted, secret);
|
|
|
|
const accountId = cookieAccount['id'] || '';
|
|
|
|
const accountMacaroon = cookieAccount['macaroon'] || '';
|
|
|
|
const accountCert = cookieAccount['cert'] || '';
|
|
|
|
const accountHost = cookieAccount['host'] || '';
|
|
|
|
|
|
|
|
account = {
|
|
|
|
id: accountId,
|
|
|
|
host: accountHost,
|
|
|
|
cert: accountCert,
|
|
|
|
macaroon: accountMacaroon,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
2020-05-21 09:14:56 +02:00
|
|
|
logger.silly('Account authentication cookie failed');
|
2020-05-19 07:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const context: ContextType = {
|
|
|
|
ip,
|
|
|
|
secret,
|
|
|
|
ssoVerified,
|
|
|
|
account,
|
|
|
|
sso: { macaroon: ssoMacaroon, cert: ssoCert, host: lnServerUrl || null },
|
|
|
|
accounts: accountConfig,
|
|
|
|
};
|
|
|
|
|
|
|
|
return context;
|
2020-04-12 18:27:01 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-16 10:57:22 +02:00
|
|
|
const handler = apolloServer.createHandler({ path: apiBaseUrl });
|
2020-04-12 18:27:01 +02:00
|
|
|
|
|
|
|
export const config = {
|
|
|
|
api: {
|
|
|
|
bodyParser: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-21 10:25:56 +02:00
|
|
|
export default handler;
|