From cff7c511a101fe42589011450d9a8dff8f8abcdd Mon Sep 17 00:00:00 2001 From: Eric Kitaif Date: Wed, 24 Jul 2019 13:12:28 -0700 Subject: [PATCH] Added MongoDB Authentication Options --- package-lock.json | 218 +++++++++++++++-------------------- package.json | 10 +- src/app.module.ts | 23 ++-- src/config/config.service.ts | 45 +++++--- 4 files changed, 143 insertions(+), 153 deletions(-) diff --git a/package-lock.json b/package-lock.json index e40c53e..11e6817 100644 --- a/package-lock.json +++ b/package-lock.json @@ -506,6 +506,11 @@ "uuid": "3.3.2" }, "dependencies": { + "@types/graphql": { + "version": "14.2.2", + "resolved": "https://registry.npmjs.org/@types/graphql/-/graphql-14.2.2.tgz", + "integrity": "sha512-okXbUmdZFMO3AYBEJCcpJFPFDkKmIiZZBqWD5TmPtAv+GHfjD2qLZEI0PvZ8IWMU4ozoK2HV2lDxWjw4LbVlnw==" + }, "lodash": { "version": "4.17.14", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.14.tgz", @@ -778,9 +783,10 @@ } }, "@types/graphql": { - "version": "14.2.2", - "resolved": "https://registry.npmjs.org/@types/graphql/-/graphql-14.2.2.tgz", - "integrity": "sha512-okXbUmdZFMO3AYBEJCcpJFPFDkKmIiZZBqWD5TmPtAv+GHfjD2qLZEI0PvZ8IWMU4ozoK2HV2lDxWjw4LbVlnw==" + "version": "14.2.3", + "resolved": "https://registry.npmjs.org/@types/graphql/-/graphql-14.2.3.tgz", + "integrity": "sha512-UoCovaxbJIxagCvVfalfK7YaNhmxj3BQFRQ2RHQKLiu+9wNXhJnlbspsLHt/YQM99IaLUUFJNzCwzc6W0ypMeQ==", + "dev": true }, "@types/istanbul-lib-coverage": { "version": "2.0.1", @@ -858,9 +864,9 @@ "integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==" }, "@types/mongodb": { - "version": "3.1.28", - "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.1.28.tgz", - "integrity": "sha512-tG+QqJ/hir2p0069ee28t2O9tlGRJKDq1WFZC2QYMlU47LGdldLL8tepfTq6aFLvP58OpwSoxaJ/qjW93ob1NQ==", + "version": "3.1.30", + "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.1.30.tgz", + "integrity": "sha512-o8NLZRp+zEILEMj628cCTT2bSWH/Zst+vjSNgaSoKwGeHqJXiGPBSHb0RuidPrllBEyQ+uSW/qNPv0b5mzh+SA==", "dev": true, "requires": { "@types/bson": "*", @@ -883,9 +889,9 @@ "integrity": "sha512-aX+gFgA5GHcDi89KG5keey2zf0WfZk/HAQotEamsK2kbey+8yGKcson0hbK8E+v0NArlCJQCqMP161YhV6ZXLg==" }, "@types/nodemailer": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.2.0.tgz", - "integrity": "sha512-WGGEk/BGRLuYF3gyoTwbtKg5tCexZzb5lkTsis2k7GkAzlg4x2299/SC6Ssdj3X/5TzT1BHVc8zcFg/7KSzBLw==", + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.2.1.tgz", + "integrity": "sha512-6f46rxxaFwyOW39psPoQiM7jHjL7apDRNT5WPHIuv+TZFv+7sBGSI9J7blIC3/NWff4O9/VSzgoQtO6aPLUdvQ==", "dev": true, "requires": { "@types/node": "*" @@ -5820,9 +5826,9 @@ } }, "mongoose": { - "version": "5.6.5", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.6.5.tgz", - "integrity": "sha512-c8bIo8mxbf1ybwo9jgPKcJRICQBlIMKwDWt2A+M7h0AutroQ5EqzRAYOK1vrHwwwq00EcJyVwjVBW2wv8E9Wfw==", + "version": "5.6.6", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.6.6.tgz", + "integrity": "sha512-5uecJSyl2TwbGM9vJteP4C54zsQL6qllq1qe/JPGO3oqIWcK/PnzCL91E0gfPH5VVpvWGX+6PafNYmU3NK8S7w==", "requires": { "async": "2.6.2", "bson": "~1.1.1", @@ -6838,9 +6844,9 @@ "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==" }, "npm": { - "version": "6.10.1", - "resolved": "https://registry.npmjs.org/npm/-/npm-6.10.1.tgz", - "integrity": "sha512-ejR83c5aPTip5hPhziypqkJu06vb5tDIugCXx1c5+04RbMjtZeMA6BfsuGnV9EBdEwzKoaHkQ9sJWQAq+LjHYw==", + "version": "6.10.2", + "resolved": "https://registry.npmjs.org/npm/-/npm-6.10.2.tgz", + "integrity": "sha512-B6n5dgmsl1fpBYhor2OTEn9Md0r63/FpQocDn4WNT4gIQRQZLql9g+mk8s3j9qZD370kUTzwDaOBSmDdNGK3NA==", "requires": { "JSONStream": "^1.3.5", "abbrev": "~1.1.1", @@ -6851,7 +6857,7 @@ "bin-links": "^1.1.2", "bluebird": "^3.5.5", "byte-size": "^5.0.1", - "cacache": "^11.3.3", + "cacache": "^12.0.2", "call-limit": "^1.1.1", "chownr": "^1.1.2", "ci-info": "^2.0.0", @@ -6876,6 +6882,7 @@ "hosted-git-info": "^2.7.1", "iferr": "^1.0.2", "imurmurhash": "*", + "infer-owner": "^1.0.4", "inflight": "~1.0.6", "inherits": "^2.0.4", "ini": "^1.3.5", @@ -6884,12 +6891,12 @@ "json-parse-better-errors": "^1.0.2", "lazy-property": "~1.0.0", "libcipm": "^4.0.0", - "libnpm": "^3.0.0", - "libnpmaccess": "*", - "libnpmhook": "^5.0.2", - "libnpmorg": "*", - "libnpmsearch": "^2.0.1", - "libnpmteam": "*", + "libnpm": "^3.0.1", + "libnpmaccess": "^3.0.2", + "libnpmhook": "^5.0.3", + "libnpmorg": "^1.0.1", + "libnpmsearch": "^2.0.2", + "libnpmteam": "^1.0.2", "libnpx": "^10.2.0", "lock-verify": "^2.1.0", "lockfile": "^1.0.4", @@ -6909,24 +6916,24 @@ "mississippi": "^3.0.0", "mkdirp": "~0.5.1", "move-concurrently": "^1.0.1", - "node-gyp": "^5.0.2", + "node-gyp": "^5.0.3", "nopt": "~4.0.1", "normalize-package-data": "^2.5.0", "npm-audit-report": "^1.3.2", "npm-cache-filename": "~1.0.2", "npm-install-checks": "~3.0.0", - "npm-lifecycle": "^3.0.0", + "npm-lifecycle": "^3.1.2", "npm-package-arg": "^6.1.0", "npm-packlist": "^1.4.4", "npm-pick-manifest": "^2.2.3", - "npm-profile": "*", - "npm-registry-fetch": "^3.9.1", + "npm-profile": "^4.0.2", + "npm-registry-fetch": "^4.0.0", "npm-user-validate": "~1.0.0", "npmlog": "~4.1.2", "once": "~1.4.0", "opener": "^1.5.1", "osenv": "^0.1.5", - "pacote": "^9.5.1", + "pacote": "^9.5.4", "path-is-inside": "~1.0.2", "promise-inflight": "~1.0.1", "qrcode-terminal": "^0.12.0", @@ -6979,14 +6986,14 @@ "bundled": true }, "agent-base": { - "version": "4.2.1", + "version": "4.3.0", "bundled": true, "requires": { "es6-promisify": "^5.0.0" } }, "agentkeepalive": { - "version": "3.4.1", + "version": "3.5.2", "bundled": true, "requires": { "humanize-ms": "^1.2.1" @@ -7158,7 +7165,7 @@ "bundled": true }, "cacache": { - "version": "11.3.3", + "version": "12.0.2", "bundled": true, "requires": { "bluebird": "^3.5.5", @@ -7166,6 +7173,7 @@ "figgy-pudding": "^3.5.1", "glob": "^7.1.4", "graceful-fs": "^4.1.15", + "infer-owner": "^1.0.3", "lru-cache": "^5.1.1", "mississippi": "^3.0.0", "mkdirp": "^0.5.1", @@ -7175,20 +7183,6 @@ "ssri": "^6.0.1", "unique-filename": "^1.1.1", "y18n": "^4.0.0" - }, - "dependencies": { - "glob": { - "version": "7.1.4", - "bundled": true, - "requires": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - } - } } }, "call-limit": { @@ -7633,7 +7627,7 @@ } }, "es6-promise": { - "version": "4.2.6", + "version": "4.2.8", "bundled": true }, "es6-promisify": { @@ -8008,10 +8002,10 @@ } }, "https-proxy-agent": { - "version": "2.2.1", + "version": "2.2.2", "bundled": true, "requires": { - "agent-base": "^4.1.0", + "agent-base": "^4.3.0", "debug": "^3.1.0" } }, @@ -8048,6 +8042,10 @@ "version": "0.1.4", "bundled": true }, + "infer-owner": { + "version": "1.0.4", + "bundled": true + }, "inflight": { "version": "1.0.6", "bundled": true, @@ -8265,45 +8263,39 @@ } }, "libnpm": { - "version": "3.0.0", + "version": "3.0.1", "bundled": true, "requires": { "bin-links": "^1.1.2", "bluebird": "^3.5.3", "find-npm-prefix": "^1.0.2", - "libnpmaccess": "^3.0.1", + "libnpmaccess": "^3.0.2", "libnpmconfig": "^1.2.1", - "libnpmhook": "^5.0.2", - "libnpmorg": "^1.0.0", - "libnpmpublish": "^1.1.0", - "libnpmsearch": "^2.0.0", - "libnpmteam": "^1.0.1", + "libnpmhook": "^5.0.3", + "libnpmorg": "^1.0.1", + "libnpmpublish": "^1.1.2", + "libnpmsearch": "^2.0.2", + "libnpmteam": "^1.0.2", "lock-verify": "^2.0.2", "npm-lifecycle": "^3.0.0", "npm-logical-tree": "^1.2.1", "npm-package-arg": "^6.1.0", - "npm-profile": "^4.0.1", - "npm-registry-fetch": "^3.8.0", + "npm-profile": "^4.0.2", + "npm-registry-fetch": "^4.0.0", "npmlog": "^4.1.2", - "pacote": "^9.2.3", + "pacote": "^9.5.3", "read-package-json": "^2.0.13", "stringify-package": "^1.0.0" } }, "libnpmaccess": { - "version": "3.0.1", + "version": "3.0.2", "bundled": true, "requires": { "aproba": "^2.0.0", "get-stream": "^4.0.0", "npm-package-arg": "^6.1.0", - "npm-registry-fetch": "^3.8.0" - }, - "dependencies": { - "aproba": { - "version": "2.0.0", - "bundled": true - } + "npm-registry-fetch": "^4.0.0" } }, "libnpmconfig": { @@ -8351,33 +8343,27 @@ } }, "libnpmhook": { - "version": "5.0.2", + "version": "5.0.3", "bundled": true, "requires": { "aproba": "^2.0.0", "figgy-pudding": "^3.4.1", "get-stream": "^4.0.0", - "npm-registry-fetch": "^3.8.0" + "npm-registry-fetch": "^4.0.0" } }, "libnpmorg": { - "version": "1.0.0", + "version": "1.0.1", "bundled": true, "requires": { "aproba": "^2.0.0", "figgy-pudding": "^3.4.1", "get-stream": "^4.0.0", - "npm-registry-fetch": "^3.8.0" - }, - "dependencies": { - "aproba": { - "version": "2.0.0", - "bundled": true - } + "npm-registry-fetch": "^4.0.0" } }, "libnpmpublish": { - "version": "1.1.1", + "version": "1.1.2", "bundled": true, "requires": { "aproba": "^2.0.0", @@ -8386,34 +8372,28 @@ "lodash.clonedeep": "^4.5.0", "normalize-package-data": "^2.4.0", "npm-package-arg": "^6.1.0", - "npm-registry-fetch": "^3.8.0", + "npm-registry-fetch": "^4.0.0", "semver": "^5.5.1", "ssri": "^6.0.1" } }, "libnpmsearch": { - "version": "2.0.1", + "version": "2.0.2", "bundled": true, "requires": { "figgy-pudding": "^3.5.1", "get-stream": "^4.0.0", - "npm-registry-fetch": "^3.8.0" + "npm-registry-fetch": "^4.0.0" } }, "libnpmteam": { - "version": "1.0.1", + "version": "1.0.2", "bundled": true, "requires": { "aproba": "^2.0.0", "figgy-pudding": "^3.4.1", "get-stream": "^4.0.0", - "npm-registry-fetch": "^3.8.0" - }, - "dependencies": { - "aproba": { - "version": "2.0.0", - "bundled": true - } + "npm-registry-fetch": "^4.0.0" } }, "libnpx": { @@ -8531,11 +8511,11 @@ } }, "make-fetch-happen": { - "version": "4.0.2", + "version": "5.0.0", "bundled": true, "requires": { "agentkeepalive": "^3.4.1", - "cacache": "^11.3.3", + "cacache": "^12.0.0", "http-cache-semantics": "^3.8.1", "http-proxy-agent": "^2.1.0", "https-proxy-agent": "^2.2.1", @@ -8664,7 +8644,7 @@ } }, "node-gyp": { - "version": "5.0.2", + "version": "5.0.3", "bundled": true, "requires": { "env-paths": "^1.0.0", @@ -8744,7 +8724,7 @@ } }, "npm-lifecycle": { - "version": "3.0.0", + "version": "3.1.2", "bundled": true, "requires": { "byline": "^5.0.0", @@ -8789,43 +8769,24 @@ } }, "npm-profile": { - "version": "4.0.1", + "version": "4.0.2", "bundled": true, "requires": { "aproba": "^1.1.2 || 2", "figgy-pudding": "^3.4.1", - "npm-registry-fetch": "^3.8.0" + "npm-registry-fetch": "^4.0.0" } }, "npm-registry-fetch": { - "version": "3.9.1", + "version": "4.0.0", "bundled": true, "requires": { "JSONStream": "^1.3.4", "bluebird": "^3.5.1", "figgy-pudding": "^3.4.1", "lru-cache": "^5.1.1", - "make-fetch-happen": "^4.0.2", + "make-fetch-happen": "^5.0.0", "npm-package-arg": "^6.1.0" - }, - "dependencies": { - "make-fetch-happen": { - "version": "4.0.2", - "bundled": true, - "requires": { - "agentkeepalive": "^3.4.1", - "cacache": "^11.3.3", - "http-cache-semantics": "^3.8.1", - "http-proxy-agent": "^2.1.0", - "https-proxy-agent": "^2.2.1", - "lru-cache": "^5.1.1", - "mississippi": "^3.0.0", - "node-fetch-npm": "^2.0.2", - "promise-retry": "^1.1.1", - "socks-proxy-agent": "^4.0.0", - "ssri": "^6.0.0" - } - } } }, "npm-run-path": { @@ -8942,16 +8903,16 @@ } }, "pacote": { - "version": "9.5.1", + "version": "9.5.4", "bundled": true, "requires": { "bluebird": "^3.5.3", - "cacache": "^11.3.2", + "cacache": "^12.0.0", "figgy-pudding": "^3.5.1", "get-stream": "^4.1.0", "glob": "^7.1.3", "lru-cache": "^5.1.1", - "make-fetch-happen": "^4.0.1", + "make-fetch-happen": "^5.0.0", "minimatch": "^3.0.4", "minipass": "^2.3.5", "mississippi": "^3.0.0", @@ -8960,7 +8921,7 @@ "npm-package-arg": "^6.1.0", "npm-packlist": "^1.1.12", "npm-pick-manifest": "^2.2.3", - "npm-registry-fetch": "^3.8.0", + "npm-registry-fetch": "^4.0.0", "osenv": "^0.1.5", "promise-inflight": "^1.0.1", "promise-retry": "^1.1.1", @@ -9364,23 +9325,32 @@ "bundled": true }, "smart-buffer": { - "version": "4.0.1", + "version": "4.0.2", "bundled": true }, "socks": { - "version": "2.2.0", + "version": "2.3.2", "bundled": true, "requires": { "ip": "^1.1.5", - "smart-buffer": "^4.0.1" + "smart-buffer": "4.0.2" } }, "socks-proxy-agent": { - "version": "4.0.1", + "version": "4.0.2", "bundled": true, "requires": { - "agent-base": "~4.2.0", - "socks": "~2.2.0" + "agent-base": "~4.2.1", + "socks": "~2.3.2" + }, + "dependencies": { + "agent-base": { + "version": "4.2.1", + "bundled": true, + "requires": { + "es6-promisify": "^5.0.0" + } + } } }, "sorted-object": { diff --git a/package.json b/package.json index de9d654..8126c9f 100644 --- a/package.json +++ b/package.json @@ -34,9 +34,9 @@ "graphql": "^14.4.2", "graphql-tools": "^4.0.5", "joi": "^14.3.1", - "mongoose": "^5.6.5", + "mongoose": "^5.6.6", "nodemailer": "^6.3.0", - "npm": "^6.10.1", + "npm": "^6.10.2", "passport": "^0.4.0", "passport-jwt": "^4.0.0", "reflect-metadata": "^0.1.13", @@ -49,13 +49,13 @@ "@types/bcrypt": "^3.0.0", "@types/dotenv": "^6.1.1", "@types/express": "^4.17.0", - "@types/graphql": "^14.2.2", + "@types/graphql": "^14.2.3", "@types/jest": "^24.0.15", "@types/joi": "^14.3.3", - "@types/mongodb": "^3.1.28", + "@types/mongodb": "^3.1.30", "@types/mongoose": "^5.5.9", "@types/node": "^12.6.8", - "@types/nodemailer": "^6.2.0", + "@types/nodemailer": "^6.2.1", "@types/passport-jwt": "^3.0.1", "@types/supertest": "^2.0.8", "jest": "^24.8.0", diff --git a/src/app.module.ts b/src/app.module.ts index ea3eb22..e4636aa 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; -import { MongooseModule } from '@nestjs/mongoose'; +import { MongooseModule, MongooseModuleOptions } from '@nestjs/mongoose'; import { UsersModule } from './users/users.module'; import { AuthModule } from './auth/auth.module'; import { join } from 'path'; @@ -11,12 +11,21 @@ import { ConfigService } from './config/config.service'; imports: [ MongooseModule.forRootAsync({ imports: [ConfigModule], - useFactory: async (configService: ConfigService) => ({ - uri: configService.mongoUri, - useNewUrlParser: true, - useCreateIndex: true, - useFindAndModify: false, - }), + useFactory: async (configService: ConfigService) => { + const options: MongooseModuleOptions = { + uri: configService.mongoUri, + useNewUrlParser: true, + useCreateIndex: true, + useFindAndModify: false, + }; + + if (configService.mongoAuthEnabled) { + options.user = configService.mongoUser; + options.pass = configService.mongoPassword; + } + + return options; + }, inject: [ConfigService], }), GraphQLModule.forRoot({ diff --git a/src/config/config.service.ts b/src/config/config.service.ts index 39c79ad..2604cf4 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -7,16 +7,6 @@ export interface EnvConfig { [key: string]: string; } -type options = - | 'MONGO_URI' - | 'JWT_SECRET' - | 'JWT_EXPIRES_IN' - | 'EMAIL_SERVICE' - | 'EMAIL_USERNAME' - | 'EMAIL_PASSWORD' - | 'EMAIL_FROM' - | 'TEST_EMAIL_TO'; - @Injectable() export class ConfigService { private readonly envConfig: EnvConfig; @@ -36,6 +26,16 @@ export class ConfigService { private validateInput(envConfig: EnvConfig): EnvConfig { const envVarsSchema: Joi.ObjectSchema = Joi.object({ MONGO_URI: Joi.string().required(), + MONGO_AUTH_ENABLED: Joi.boolean().default(false), + MONGO_USER: Joi.string().when('MONGO_AUTH_ENABLED', { + is: true, + then: Joi.required(), + }), + MONGO_PASSWORD: Joi.string().when('MONGO_AUTH_ENABLED', { + is: true, + then: Joi.required(), + }), + IMAGES_URL: Joi.string().default('http://localhost:3000/images/'), JWT_SECRET: Joi.string().required(), JWT_EXPIRES_IN: Joi.number(), EMAIL_ENABLED: Joi.boolean().default(false), @@ -58,14 +58,9 @@ export class ConfigService { TEST_EMAIL_TO: Joi.string(), }); - const { error, value: validatedEnvConfig } = Joi.validate( - envConfig, - envVarsSchema, - ); + const { error, value: validatedEnvConfig } = Joi.validate(envConfig, envVarsSchema); if (error) { - throw new Error( - `Config validation error in your env file: ${error.message}`, - ); + throw new Error(`Config validation error in your env file: ${error.message}`); } return validatedEnvConfig; } @@ -85,6 +80,10 @@ export class ConfigService { return this.envConfig.JWT_SECRET; } + get imagesUrl(): string { + return this.envConfig.IMAGES_URL; + } + get emailService(): string | undefined { return this.envConfig.EMAIL_SERVICE; } @@ -105,7 +104,19 @@ export class ConfigService { return this.envConfig.TEST_EMAIL_TO; } + get mongoUser(): string | undefined { + return this.envConfig.MONGO_USER; + } + + get mongoPassword(): string | undefined { + return this.envConfig.MONGO_PASSWORD; + } + get emailEnabled(): boolean { return Boolean(this.envConfig.EMAIL_ENABLED).valueOf(); } + + get mongoAuthEnabled(): boolean { + return Boolean(this.envConfig.MONGO_AUTH_ENABLED).valueOf(); + } }