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-05-26 07:03:33 +02:00
|
|
|
import cookie from 'cookie';
|
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-26 07:03:33 +02:00
|
|
|
context: ({ req, res }) => {
|
2020-04-12 18:27:01 +02:00
|
|
|
const ip = getIp(req);
|
2020-05-19 07:50:16 +02:00
|
|
|
|
2020-05-26 07:03:33 +02:00
|
|
|
const { AccountAuth, SSOAuth } = cookie.parse(req.headers.cookie ?? '');
|
|
|
|
|
2020-05-19 07:50:16 +02:00
|
|
|
let ssoVerified = false;
|
2020-05-26 07:03:33 +02:00
|
|
|
if (SSOAuth) {
|
2020-05-23 14:46:47 +02:00
|
|
|
logger.silly('SSOAuth cookie found in request');
|
2020-05-26 07:03:33 +02:00
|
|
|
if (nodeEnv === 'development') {
|
|
|
|
ssoVerified = true;
|
|
|
|
}
|
2020-05-19 07:50:16 +02:00
|
|
|
try {
|
2020-05-26 07:03:33 +02:00
|
|
|
jwt.verify(SSOAuth, secret);
|
2020-05-19 07:50:16 +02:00
|
|
|
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;
|
2020-05-26 07:03:33 +02:00
|
|
|
if (AccountAuth) {
|
2020-05-23 14:46:47 +02:00
|
|
|
logger.silly('AccountAuth cookie found in request');
|
2020-05-19 07:50:16 +02:00
|
|
|
try {
|
2020-05-26 07:03:33 +02:00
|
|
|
const cookieAccount = jwt.verify(AccountAuth, secret);
|
|
|
|
const id = cookieAccount['id'] || '';
|
|
|
|
const bytes = AES.decrypt(cookieAccount['password'], secret);
|
|
|
|
const password = bytes.toString(CryptoJS.enc.Utf8);
|
2020-05-19 07:50:16 +02:00
|
|
|
|
2020-05-26 07:03:33 +02:00
|
|
|
account = { id, password };
|
2020-05-19 07:50:16 +02:00
|
|
|
} 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,
|
2020-05-26 07:03:33 +02:00
|
|
|
res,
|
2020-05-19 07:50:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return context;
|
2020-04-12 18:27:01 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
api: {
|
|
|
|
bodyParser: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-26 07:03:33 +02:00
|
|
|
export default apolloServer.createHandler({ path: apiBaseUrl });
|