Skip to content

Commit

Permalink
Separate non-sensitive app config with secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
rafasofizada committed May 8, 2022
1 parent 9a32548 commit 60605dd
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# passwords and keys
.*.env
*.configuration.ts
secrets.ts

# compiled output
/node_modules
Expand Down
128 changes: 128 additions & 0 deletions app.configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import secrets, { EtherealEmailConfig, MailchimpEmailConfig } from 'secrets';

export default (() => {
const shared = (domain: string) => ({
domain,
nodeEnv: process.env.APP_ENV as 'dev' | 'prod',
stripe: {
apiKey: secrets.stripe.apiKey,
successPageUrl: `${domain}/order/thank-you`,
cancelPageUrl: `${domain}/order/error`,
// IMPORTANT: Keep track of all Stripe integrations' api versions (including webhooks)
apiVersion: '2020-08-27' as const,
webhook: {
// Default set in @golevelup/nestjs-stripe library
path: 'stripe/webhook',
secret: secrets.stripe.webhookSecret,
},
},
cookie: {
authIndicatorName: 'auth',
tokenName: 'token',
tokenExpiration: '7d',
},
auth: {
tokenKey: secrets.authTokenKey,
verificationTokenExpiration: '30m',
authTokenExpiration: '7d',
},
mongo: { connectionString: secrets.mongoConnectionString },
email: secrets.email,
serviceFee: {
stripeProductId: 'prod_KhDFjew4BDycpd',
stripePriceId: 'price_1K1oojFkgohp7fDw5cKL2wiy',
loclyCutPercent: 20,
},
rewards: {
referralUsd: 5,
refereeUsd: 5,
codeLength: 6,
},
host: {
payoutDelayDays: 19,
},
});

if (process.env.APP_ENV === 'dev') {
const sharedConfig = shared('http://localhost:3000');

return {
...sharedConfig,
mongo: {
...sharedConfig.mongo,
dbName: 'dev',
},
cookie: {
...sharedConfig.cookie,
cors: { secure: false, sameSite: 'lax' },
},
};
}

if (process.env.APP_ENV === 'prod') {
const sharedConfig = shared('https://aqueous-caverns-91110.herokuapp.com');

return {
...sharedConfig,
mongo: {
...sharedConfig.mongo,
dbName: 'prod',
},
cookie: {
...sharedConfig.cookie,
// Only 'SameSite=None; Secure' cookies are forwarded in third-party requests,
// which is necessary in production to allow the front-end on domain X (see main.ts :: enableCors config)
// to send request to server on domain Y:
// https://stackoverflow.com/a/46412839/6539857
// https://digiday.com/media/what-is-chrome-samesite/
cors: { secure: true, sameSite: 'none' },
},
};
}

throw new Error('No ENV passed');
})();

export type StripeConfig = {
apiKey: string;
apiVersion: '2020-08-27';
webhook: {
secret: string;
path: string;
};
successPageUrl: string;
cancelPageUrl: string;
};

export type MongoConfig = {
connectionString: string;
dbName: string;
};

export type CookieConfig = {
authIndicatorName: string;
tokenName: string;
tokenExpiration: string;
cors: {
secure: boolean;
sameSite: 'strict' | 'lax' | 'none';
};
};

export type AuthConfig = {
tokenKey: string;
verificationTokenExpiration: string;
authTokenExpiration: string;
};

export type AppConfig = {
domain: string;
nodeEnv: 'dev' | 'prod';
stripe: StripeConfig;
mongo: MongoConfig;
cookie: CookieConfig;
auth: AuthConfig;
email: EmailConfig;
};

export type EmailConfig = EtherealEmailConfig | MailchimpEmailConfig;
2 changes: 1 addition & 1 deletion src/AppModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
RawBodyMiddleware,
} from '@golevelup/nestjs-webhooks';

import config from '../main.configuration';
import config from '../app.configuration';

import { OrderModule } from './order/OrderModule';
import { CustomerModule } from './customer/CustomerModule';
Expand Down
2 changes: 1 addition & 1 deletion src/auth/AuthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ms from 'ms';
import { Body, Controller, Get, Post, Res } from '@nestjs/common';
import { CookieOptions, Response } from 'express';

import config from '../../main.configuration';
import config from '../../app.configuration';

import {
RequestAuthRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/auth/application/RequestAuth/RequestAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
INotificationService,
NotificationType,
} from '../../../infrastructure/notification/INotificationService';
import config from '../../../../main.configuration';
import config from '../../../../app.configuration';

/**
* Functionality for the first step in user auth – accepting user email, generating a verification token and sending it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

import config from '../../../../main.configuration';
import config from '../../../../app.configuration';

@Injectable()
/**
Expand Down
2 changes: 1 addition & 1 deletion src/auth/application/VerifyAuth/VerifyAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';

import config from '../../../../main.configuration';
import config from '../../../../app.configuration';

import { Token } from '../../entity/Token';
import { tokenToString } from '../utils';
Expand Down
2 changes: 1 addition & 1 deletion src/auth/infrastructure/AuthInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@nestjs/common';
import { Request, Response } from 'express';

import config from '../../../main.configuration';
import config from '../../../app.configuration';

import { UserType, Token } from '../entity/Token';
import { stringToToken } from '../application/utils';
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/email/EmailModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module, Provider } from '@nestjs/common';

import config from '../../../main.configuration';
import config from '../../../app.configuration';

import { MailchimpEmailService } from './MailchimpEmailService';
import { IEmailService } from './IEmailService';
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/email/EtherealPseudoEmailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { createTransport, Transporter } from 'nodemailer';
import SMTPTransport from 'nodemailer/lib/smtp-transport';

import config, { EtherealEmailConfig } from '../../../main.configuration';
import config, { EtherealEmailConfig } from '../../../app.configuration';

import { throwCustomException } from '../../common/error-handling';
import { EmailData, IEmailService } from './IEmailService';
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/email/MailchimpEmailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import e from 'express';
import { createTransport, Transporter } from 'nodemailer';
import SMTPTransport from 'nodemailer/lib/smtp-transport';

import config, { MailchimpEmailConfig } from '../../../main.configuration';
import config, { MailchimpEmailConfig } from '../../../app.configuration';

import { throwCustomException } from '../../common/error-handling';
import { EmailData, IEmailService } from './IEmailService';
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/notification/NotificationModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module, Provider } from '@nestjs/common';

import config from '../../../main.configuration';
import config from '../../../app.configuration';

import { EmailModule } from '../email/EmailModule';
import { INotificationService } from './INotificationService';
Expand Down
2 changes: 1 addition & 1 deletion src/order/application/ConfirmOrder/ConfirmOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Stripe from 'stripe';
import { HttpStatus, Injectable } from '@nestjs/common';
import { InjectStripeClient } from '@golevelup/nestjs-stripe';

import mainConfig from '../../../../main.configuration';
import mainConfig from '../../../../app.configuration';
import appConfig from '../../../../app.configuration';

import { ConfirmOrderResult, IConfirmOrder } from './IConfirmOrder';
Expand Down
2 changes: 1 addition & 1 deletion src/order/application/PayShipment/PayShipment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Stripe from 'stripe';
import { Injectable } from '@nestjs/common';
import { InjectStripeClient } from '@golevelup/nestjs-stripe';

import config from '../../../../main.configuration';
import config from '../../../../app.configuration';

import {
PayShipmentPayload,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Stripe from 'stripe';
import supertest, { SuperAgentTest, agent, Response } from 'supertest';
import { TestingModule } from '@nestjs/testing';

import config from '../../main.configuration';
import config from '../../app.configuration';

import { IRequestAuth } from '../../src/auth/application/RequestAuth/IRequestAuth';
import { UserType } from '../../src/auth/entity/Token';
Expand Down

0 comments on commit 60605dd

Please sign in to comment.