From d4010ed8e37fc614c958134420aaab28a15b162f Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Tue, 12 Dec 2023 16:31:52 +0200 Subject: [PATCH] TW-1192 Leave one login/password pair --- .env.dist | 6 ++-- package.json | 1 - src/config.ts | 12 +++---- src/index.ts | 4 +-- src/middlewares/basic-auth.middleware.ts | 19 +++------- src/routers/slise-heuristic-rules-router.ts | 9 +++-- src/routers/slise-rules-router.ts | 4 +-- src/utils/express-helpers.ts | 9 +++-- yarn.lock | 40 --------------------- 9 files changed, 22 insertions(+), 82 deletions(-) diff --git a/.env.dist b/.env.dist index 57b8115..5f4b337 100644 --- a/.env.dist +++ b/.env.dist @@ -8,7 +8,5 @@ ALICE_BOB_PRIVATE_KEY= THREE_ROUTE_API_URL= THREE_ROUTE_API_AUTH_TOKEN= REDIS_URL= -ADD_NOTIFICATION_USERNAME= -ADD_NOTIFICATION_PASSWORD= -MANAGE_ADS_USERNAME= -MANAGE_ADS_PASSWORD= +ADMIN_USERNAME= +ADMIN_PASSWORD= diff --git a/package.json b/package.json index 5f0cf0d..eb51313 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "express": "^4.18.2", "firebase-admin": "^10.0.2", "ioredis": "^5.3.2", - "joi": "^17.11.0", "lodash": "^4.17.21", "memoizee": "^0.4.15", "pino": "^6.11.2", diff --git a/src/config.ts b/src/config.ts index a0f7f21..7ed45ca 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,16 +5,14 @@ export const MIN_IOS_APP_VERSION = '1.10.445'; export const MIN_ANDROID_APP_VERSION = '1.10.445'; export const EnvVars = { - MOONPAY_SECRET_KEY: '', // getEnv('MOONPAY_SECRET_KEY'), - ALICE_BOB_PRIVATE_KEY: '', // getEnv('ALICE_BOB_PRIVATE_KEY'), - ALICE_BOB_PUBLIC_KEY: '', // getEnv('ALICE_BOB_PUBLIC_KEY'), + MOONPAY_SECRET_KEY: getEnv('MOONPAY_SECRET_KEY'), + ALICE_BOB_PRIVATE_KEY: getEnv('ALICE_BOB_PRIVATE_KEY'), + ALICE_BOB_PUBLIC_KEY: getEnv('ALICE_BOB_PUBLIC_KEY'), THREE_ROUTE_API_URL: getEnv('THREE_ROUTE_API_URL'), THREE_ROUTE_API_AUTH_TOKEN: getEnv('THREE_ROUTE_API_AUTH_TOKEN'), REDIS_URL: getEnv('REDIS_URL'), - ADD_NOTIFICATION_USERNAME: getEnv('ADD_NOTIFICATION_USERNAME'), - ADD_NOTIFICATION_PASSWORD: getEnv('ADD_NOTIFICATION_PASSWORD'), - MANAGE_ADS_USERNAME: getEnv('MANAGE_ADS_USERNAME'), - MANAGE_ADS_PASSWORD: getEnv('MANAGE_ADS_PASSWORD') + ADMIN_USERNAME: getEnv('ADMIN_USERNAME'), + ADMIN_PASSWORD: getEnv('ADMIN_PASSWORD') }; for (const name in EnvVars) { diff --git a/src/index.ts b/src/index.ts index 030074e..e232f90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ import swaggerUi from 'swagger-ui-express'; import { getAdvertisingInfo } from './advertising/advertising'; import { MIN_ANDROID_APP_VERSION, MIN_IOS_APP_VERSION } from './config'; import getDAppsStats from './getDAppsStats'; -import { basicAuth, BasicAuthRights } from './middlewares/basic-auth.middleware'; +import { basicAuth } from './middlewares/basic-auth.middleware'; import { Notification, PlatformType } from './notifications/notification.interface'; import { getImageFallback } from './notifications/utils/get-image-fallback.util'; import { getNotifications } from './notifications/utils/get-notifications.util'; @@ -118,7 +118,7 @@ app.get('/api/notifications', async (_req, res) => { } }); -app.post('/api/notifications', basicAuth(BasicAuthRights.AddNotification), async (req, res) => { +app.post('/api/notifications', basicAuth, async (req, res) => { try { const { mobile, diff --git a/src/middlewares/basic-auth.middleware.ts b/src/middlewares/basic-auth.middleware.ts index 28eb93a..deaca01 100644 --- a/src/middlewares/basic-auth.middleware.ts +++ b/src/middlewares/basic-auth.middleware.ts @@ -3,28 +3,17 @@ import { Request, Response, NextFunction } from 'express'; import { EnvVars } from '../config'; import { isDefined } from '../utils/helpers'; -export enum BasicAuthRights { - AddNotification = 'add-notification', - ManageAds = 'manage-ads' -} - const credentials = { - [BasicAuthRights.AddNotification]: { - username: EnvVars.ADD_NOTIFICATION_USERNAME, - password: EnvVars.ADD_NOTIFICATION_PASSWORD - }, - [BasicAuthRights.ManageAds]: { - username: EnvVars.MANAGE_ADS_USERNAME, - password: EnvVars.MANAGE_ADS_PASSWORD - } + username: EnvVars.ADMIN_USERNAME, + password: EnvVars.ADMIN_PASSWORD }; -export const basicAuth = (rights: BasicAuthRights) => (req: Request, res: Response, next: NextFunction) => { +export const basicAuth = (req: Request, res: Response, next: NextFunction) => { const base64EncodedCredentials = req.get('Authorization'); if (isDefined(base64EncodedCredentials)) { const [username, password] = Buffer.from(base64EncodedCredentials.split(' ')[1], 'base64').toString().split(':'); - const { username: correctUsername, password: correctPassword } = credentials[rights]; + const { username: correctUsername, password: correctPassword } = credentials; if (!(username === correctUsername && password === correctPassword)) { handleNotAuthenticated(res, next); diff --git a/src/routers/slise-heuristic-rules-router.ts b/src/routers/slise-heuristic-rules-router.ts index 43a350f..497f223 100644 --- a/src/routers/slise-heuristic-rules-router.ts +++ b/src/routers/slise-heuristic-rules-router.ts @@ -9,7 +9,7 @@ import { removeSliseHeuristicUrlRegexes, upsertSliseHeuristicSelectors } from '../advertising/slise'; -import { basicAuth, BasicAuthRights } from '../middlewares/basic-auth.middleware'; +import { basicAuth } from '../middlewares/basic-auth.middleware'; import { addObjectStorageMethodsToRouter, withBodyValidation, withExceptionHandler } from '../utils/express-helpers'; import { adTypesListSchema, regexStringListSchema, sliseSelectorsDictionarySchema } from '../utils/schemas'; @@ -93,7 +93,7 @@ sliseHeuristicRulesRouter }) ) .post( - basicAuth(BasicAuthRights.ManageAds), + basicAuth, withExceptionHandler( withBodyValidation(regexStringListSchema, async (req, res) => { const regexesAddedCount = await addSliseHeuristicUrlRegexes(req.body); @@ -103,7 +103,7 @@ sliseHeuristicRulesRouter ) ) .delete( - basicAuth(BasicAuthRights.ManageAds), + basicAuth, withExceptionHandler( withBodyValidation(regexStringListSchema, async (req, res) => { const regexesRemovedCount = await removeSliseHeuristicUrlRegexes(req.body); @@ -206,6 +206,5 @@ addObjectStorageMethodsToRouter( 'adType', sliseSelectorsDictionarySchema, adTypesListSchema, - removedEntriesCount => `${removedEntriesCount} ad types have been removed`, - BasicAuthRights.ManageAds + removedEntriesCount => `${removedEntriesCount} ad types have been removed` ); diff --git a/src/routers/slise-rules-router.ts b/src/routers/slise-rules-router.ts index 31d3b27..5b4a400 100644 --- a/src/routers/slise-rules-router.ts +++ b/src/routers/slise-rules-router.ts @@ -7,7 +7,6 @@ import { SliseAdContainerRule, upsertSliseAdContainerRules } from '../advertising/slise'; -import { BasicAuthRights } from '../middlewares/basic-auth.middleware'; import { addObjectStorageMethodsToRouter } from '../utils/express-helpers'; import { hostnamesListSchema, sliseAdContainerRulesDictionarySchema } from '../utils/schemas'; import { sliseHeuristicRulesRouter } from './slise-heuristic-rules-router'; @@ -216,6 +215,5 @@ addObjectStorageMethodsToRouter( 'domain', sliseAdContainerRulesDictionarySchema, hostnamesListSchema, - removedEntriesCount => `${removedEntriesCount} domains have been removed`, - BasicAuthRights.ManageAds + removedEntriesCount => `${removedEntriesCount} domains have been removed` ); diff --git a/src/utils/express-helpers.ts b/src/utils/express-helpers.ts index 0a73d13..3499acd 100644 --- a/src/utils/express-helpers.ts +++ b/src/utils/express-helpers.ts @@ -1,7 +1,7 @@ import { NextFunction, Request, RequestHandler, Response, Router } from 'express'; import { ArraySchema as IArraySchema, ObjectSchema as IObjectSchema, Schema, ValidationError } from 'yup'; -import { basicAuth, BasicAuthRights } from '../middlewares/basic-auth.middleware'; +import { basicAuth } from '../middlewares/basic-auth.middleware'; interface ObjectStorageMethods { getByKey: (key: string) => Promise; @@ -49,8 +49,7 @@ export const addObjectStorageMethodsToRouter = ( keyName: string, objectValidationSchema: IObjectSchema>, keysArrayValidationSchema: IArraySchema, - successfulRemovalMessage: (removedEntriesCount: number) => string, - modifyAuthRights: BasicAuthRights + successfulRemovalMessage: (removedEntriesCount: number) => string ) => { router.get( path === '/' ? `/:${keyName}` : `${path}/:${keyName}`, @@ -73,7 +72,7 @@ export const addObjectStorageMethodsToRouter = ( }) ) .post( - basicAuth(modifyAuthRights), + basicAuth, withExceptionHandler( withBodyValidation(objectValidationSchema, async (req, res) => { const validatedValues = req.body; @@ -85,7 +84,7 @@ export const addObjectStorageMethodsToRouter = ( ) ) .delete( - basicAuth(modifyAuthRights), + basicAuth, withExceptionHandler( withBodyValidation(keysArrayValidationSchema, async (req, res) => { const removedEntriesCount = await methods.removeValues(req.body); diff --git a/yarn.lock b/yarn.lock index 219e686..12090f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -236,18 +236,6 @@ resolved "https://registry.yarnpkg.com/@hapi/bourne/-/bourne-2.0.0.tgz#5bb2193eb685c0007540ca61d166d4e1edaf918d" integrity sha512-WEezM1FWztfbzqIUbsDzFRVMxSoLy3HugVcux6KDDtTqzPsLE8NDRHfXvev66aH1i2oOKKar3/XDjbvh/OUBdg== -"@hapi/hoek@^9.0.0": - version "9.3.0" - resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.3.0.tgz#8368869dcb735be2e7f5cb7647de78e167a251fb" - integrity sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ== - -"@hapi/topo@^5.0.0": - version "5.1.0" - resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012" - integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg== - dependencies: - "@hapi/hoek" "^9.0.0" - "@humanwhocodes/config-array@^0.5.0": version "0.5.0" resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.5.0.tgz#1407967d4c6eecd7388f83acf1eaf4d0c6e58ef9" @@ -369,23 +357,6 @@ resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570" integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA= -"@sideway/address@^4.1.3": - version "4.1.4" - resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.4.tgz#03dccebc6ea47fdc226f7d3d1ad512955d4783f0" - integrity sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw== - dependencies: - "@hapi/hoek" "^9.0.0" - -"@sideway/formula@^3.0.1": - version "3.0.1" - resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.1.tgz#80fcbcbaf7ce031e0ef2dd29b1bfc7c3f583611f" - integrity sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg== - -"@sideway/pinpoint@^2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df" - integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ== - "@stablelib/binary@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@stablelib/binary/-/binary-1.0.1.tgz#c5900b94368baf00f811da5bdb1610963dfddf7f" @@ -2822,17 +2793,6 @@ jmespath@^0.15.0: resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.15.0.tgz#a3f222a9aae9f966f5d27c796510e28091764217" integrity sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc= -joi@^17.11.0: - version "17.11.0" - resolved "https://registry.yarnpkg.com/joi/-/joi-17.11.0.tgz#aa9da753578ec7720e6f0ca2c7046996ed04fc1a" - integrity sha512-NgB+lZLNoqISVy1rZocE9PZI36bL/77ie924Ri43yEvi9GUUMPeyVIr8KdFTMUlby1p0PBYMk9spIxEUQYqrJQ== - dependencies: - "@hapi/hoek" "^9.0.0" - "@hapi/topo" "^5.0.0" - "@sideway/address" "^4.1.3" - "@sideway/formula" "^3.0.1" - "@sideway/pinpoint" "^2.0.0" - jose@^2.0.5: version "2.0.6" resolved "https://registry.yarnpkg.com/jose/-/jose-2.0.6.tgz#894ba19169af339d3911be933f913dd02fc57c7c"