Skip to content

Commit 554c3ec

Browse files
committed
fix apikey validation
1 parent 1186386 commit 554c3ec

File tree

10 files changed

+29
-15
lines changed

10 files changed

+29
-15
lines changed

addons/instruction.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# The resouces provided in this directory can be used to setup the project

addons/mongodb/api_keys.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"_id":{"$oid":"5e7b8e40d347fc2407c564a9"},"metadata":"To be used by the xyz vendor","key":"GCMUDiuY5a7WvyUNt9n3QztToSHzK7Uj","version":{"$numberInt":"1"},"status":true,"createdAt":{"$date":{"$numberLong":"1585141250000"}},"updatedAt":{"$date":{"$numberLong":"1585141250000"}}}

addons/mongodb/roles.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{"_id":{"$oid":"5e7b8acad7aded2407e078d7"},"code":"LEARNER","status":true,"createdAt":{"$date":{"$numberLong":"1585161000000"}},"updatedAt":{"$date":{"$numberLong":"1585161000000"}}}
2+
{"_id":{"$oid":"5e7b8c22d347fc2407c564a6"},"code":"WRITER","status":true,"createdAt":{"$date":{"$numberLong":"1585161000000"}},"updatedAt":{"$date":{"$numberLong":"1585161000000"}}}
3+
{"_id":{"$oid":"5e7b8c2ad347fc2407c564a7"},"code":"EDITOR","status":true,"createdAt":{"$date":{"$numberLong":"1585161000000"}},"updatedAt":{"$date":{"$numberLong":"1585161000000"}}}
4+
{"_id":{"$oid":"5e7b8c32d347fc2407c564a8"},"code":"ADMIN","status":true,"createdAt":{"$date":{"$numberLong":"1585161000000"}},"updatedAt":{"$date":{"$numberLong":"1585161000000"}}}

src/auth/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { JoiObjectId } from '../helpers/validator';
44
export default {
55
apiKey: Joi.object().keys({
66
'x-api-key': Joi.string().required().min(1)
7-
}),
7+
}).unknown(true),
88
auth: Joi.object().keys({
99
'x-access-token': Joi.string().required().min(1),
1010
'x-user-id': JoiObjectId,
11-
})
11+
}).unknown(true)
1212
};

src/database/repository/ApiKeyRepo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import ApiKey, { IApiKey } from '../model/ApiKey';
33
export default class ApiRepository {
44

55
public static async findByKey(key: string): Promise<IApiKey> {
6-
return ApiKey.findOne({ apiKey: key, status: true }).lean<IApiKey>().exec();
6+
return ApiKey.findOne({ key: key, status: true }).lean<IApiKey>().exec();
77
}
88
}

src/helpers/validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ export const JoiObjectId = () => Joi.string().custom((value: string, helpers) =>
1818
}, 'Object Id Validation');
1919

2020

21-
export default (schema: Joi.Schema, source: ValidationSource = ValidationSource.BODY) =>
21+
export default (schema: Joi.ObjectSchema, source: ValidationSource = ValidationSource.BODY) =>
2222
(req: Request, res: Response, next: NextFunction) => {
2323
try {
2424
const { error } = schema.validate(req[source]);
2525

26-
if (error === null) return next();
26+
if (!error) return next();
2727

2828
const { details } = error;
2929
const message = details.map(i => i.message.replace(/['"]+/g, '')).join(',');

src/routes/v1/access/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default {
1212
auth: Joi.object().keys({
1313
'x-access-token': Joi.string().required().min(1),
1414
'x-user-id': JoiObjectId,
15-
}),
15+
}).unknown(true),
1616
signup: Joi.object().keys({
1717
name: Joi.string().required().min(3),
1818
email: Joi.string().required().email(),

src/routes/v1/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const app = express();
44

55
/*-------------------------------------------------------------------------*/
66
// Below all APIs are public APIs protected by api-key
7-
app.use('/', require('../../auth/apiValidation'));
7+
app.use('/', require('../../auth/apikey'));
88
/*-------------------------------------------------------------------------*/
99

1010
app.use('/signup', require('./access/signup'));

src/server.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import express, { Request, Response, NextFunction, Errback } from 'express';
1+
import express, { Request, Response, NextFunction } from 'express';
22
import Logger from './utils/Logger';
33
import bodyParser from 'body-parser';
44
import http from 'http';
@@ -19,14 +19,17 @@ http.createServer(app).listen(port, () => { Logger.info(`server running on port
1919
app.use('/v1', require('./routes/v1'));
2020

2121
// catch 404 and forward to error handler
22-
app.use((req: Request, res: Response, next: NextFunction) => next(new NotFoundError()));
22+
app.use((req, res, next) => next(new NotFoundError()));
2323

2424
// Middleware Error Handler
25-
app.use((err: Errback, req: Request, res: Response, next: NextFunction) => {
25+
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
2626
if (err instanceof ApiError) {
2727
ApiError.handle(err, res);
2828
} else {
29-
if (environment === 'development') return res.status(500).json(err);
29+
if (environment === 'development') {
30+
Logger.error(err);
31+
return res.status(500).send(err.message);
32+
}
3033
ApiError.handle(new InternalError(), res);
3134
}
3235
});

src/utils/logger.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if (!fs.existsSync(dir)) {
1313
fs.mkdirSync(dir);
1414
}
1515

16-
const logLevel = environment === 'development' ? 'debug' : 'info';
16+
const logLevel = environment === 'development' ? 'debug' : 'warn';
1717

1818
const options = {
1919
file: {
@@ -26,17 +26,22 @@ const options = {
2626
humanReadableUnhandledException: true,
2727
prettyPrint: true,
2828
json: true,
29-
maxsize: 5242880, // 5MB
29+
maxSize: '20m',
3030
colorize: true,
31+
maxFiles: '14d'
3132
}
3233
};
3334

3435
export default createLogger({
3536
transports: [
3637
new transports.Console({
3738
level: logLevel,
38-
format: format.combine(format.colorize(), format.simple())
39-
})
39+
format: format.combine(
40+
format.colorize(),
41+
format.metadata(),
42+
format.errors({ stack: true }),
43+
format.prettyPrint())
44+
}),
4045
],
4146
exceptionHandlers: [
4247
new DailyRotateFile(options.file),

0 commit comments

Comments
 (0)