Skip to content

Commit

Permalink
TW-1192 Leave one login/password pair
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Dec 12, 2023
1 parent 478d35d commit d4010ed
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 82 deletions.
6 changes: 2 additions & 4 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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=
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 5 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 4 additions & 15 deletions src/middlewares/basic-auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions src/routers/slise-heuristic-rules-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -93,7 +93,7 @@ sliseHeuristicRulesRouter
})
)
.post(
basicAuth(BasicAuthRights.ManageAds),
basicAuth,
withExceptionHandler(
withBodyValidation(regexStringListSchema, async (req, res) => {
const regexesAddedCount = await addSliseHeuristicUrlRegexes(req.body);
Expand All @@ -103,7 +103,7 @@ sliseHeuristicRulesRouter
)
)
.delete(
basicAuth(BasicAuthRights.ManageAds),
basicAuth,
withExceptionHandler(
withBodyValidation(regexStringListSchema, async (req, res) => {
const regexesRemovedCount = await removeSliseHeuristicUrlRegexes(req.body);
Expand Down Expand Up @@ -206,6 +206,5 @@ addObjectStorageMethodsToRouter<string[]>(
'adType',
sliseSelectorsDictionarySchema,
adTypesListSchema,
removedEntriesCount => `${removedEntriesCount} ad types have been removed`,
BasicAuthRights.ManageAds
removedEntriesCount => `${removedEntriesCount} ad types have been removed`
);
4 changes: 1 addition & 3 deletions src/routers/slise-rules-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -216,6 +215,5 @@ addObjectStorageMethodsToRouter<SliseAdContainerRule[]>(
'domain',
sliseAdContainerRulesDictionarySchema,
hostnamesListSchema,
removedEntriesCount => `${removedEntriesCount} domains have been removed`,
BasicAuthRights.ManageAds
removedEntriesCount => `${removedEntriesCount} domains have been removed`
);
9 changes: 4 additions & 5 deletions src/utils/express-helpers.ts
Original file line number Diff line number Diff line change
@@ -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<V> {
getByKey: (key: string) => Promise<V>;
Expand Down Expand Up @@ -49,8 +49,7 @@ export const addObjectStorageMethodsToRouter = <V>(
keyName: string,
objectValidationSchema: IObjectSchema<Record<string, V>>,
keysArrayValidationSchema: IArraySchema<string[], object>,
successfulRemovalMessage: (removedEntriesCount: number) => string,
modifyAuthRights: BasicAuthRights
successfulRemovalMessage: (removedEntriesCount: number) => string
) => {
router.get(
path === '/' ? `/:${keyName}` : `${path}/:${keyName}`,
Expand All @@ -73,7 +72,7 @@ export const addObjectStorageMethodsToRouter = <V>(
})
)
.post(
basicAuth(modifyAuthRights),
basicAuth,
withExceptionHandler(
withBodyValidation(objectValidationSchema, async (req, res) => {
const validatedValues = req.body;
Expand All @@ -85,7 +84,7 @@ export const addObjectStorageMethodsToRouter = <V>(
)
)
.delete(
basicAuth(modifyAuthRights),
basicAuth,
withExceptionHandler(
withBodyValidation(keysArrayValidationSchema, async (req, res) => {
const removedEntriesCount = await methods.removeValues(req.body);
Expand Down
40 changes: 0 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit d4010ed

Please sign in to comment.