Skip to content

Commit b577376

Browse files
committed
fix authentication and logout
1 parent e2c3dc2 commit b577376

File tree

14 files changed

+27
-32
lines changed

14 files changed

+27
-32
lines changed

keys/private.pem.example

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
-----BEGIN RSA PRIVATE KEY-----
2-
MIICXAIBAAKBgQC0l9II745oaw+6EF+cZdBsvF4AAcKC+7AxUbcgUIH8hahApLwQ
3-
maeglUmnErumDu4aVEvHaWYvBBS2/Bc+mCGiadUegwahrUOX3g3FNQiNCWAdSBDM
4-
gwEctmlNQUh6OenceaBpQayIYIiOXgZRF28koy2dkiNp/gfqnClkBcArqwIDAQAB
5-
AoGAIW2U2EiXgKaIo7VSLV3/RZFciP3KZnp2M7rffeDJHtgSu9yCX0RB7gyUNJPY
6-
HjXXRxJhcIkG/B7yQqjJGLIKT3jdfFZh5TcJxbJI4+dPfcJCx4i+nXej25qYLalC
7-
ebgQs+xf3Qv5MAAGoLPqqee/MrBPyh31N85MqT0TqUnxcuECQQDvd39FIM6NiYEH
8-
u39S/wGnpQKU9qYlugNDId738nsC+53NtPeVIiM/87uctIvCFRiHblZ7ynyNhj7g
9-
uGtXX0QRAkEAwQ+/cpuoAxHBqhkqFgw5G0cgcx1lLre96s+Pg67HDFYCICNhDF08
10-
OajVp+daNMQ/0iwTU7qTbKfrTMIlk1R/+wJAKBkcaJjrvWuO/Zp54Y3t2wKYghUj
11-
ESIqta0QRviFfKRhcjahVomW2XiYq87XsDIUH2lsdeIcJR6bnMmEv+al4QJBAJd8
12-
o9xqBwthg3TVChECxkyBIxUwm5Rs2LjG5PWOzuD/G+vb1uq8veUAdaWqAz4kP2/K
13-
5d9tJwC7QgaY0KMqSsECQA+fDl+XOABmQFLITnjN31Zc6rArhgS/wFeO1MU/5oVL
14-
H0aH8xIM+eHPNnQrqct0aTmO2CnbbqoZlXoE4G6glVo=
2+
MIIBOgIBAAJBAJ6P+APtBxacEuI6n3PbdIDsLR2/uj/FVincMBYKBtpc3jBL/JNp
3+
qX10mmdkOpOv6Jh0vE314q9Zg88jSNjus9kCAwEAAQJAZ3W09IrSVzRbNfXeWPBW
4+
olB4V7LkSfvu7r1XOuor8ooi7cHyHAmaYu7LmcG41wE37BKkUG5+PTW3Q6qyIOqq
5+
IQIhANERd9yfuV57Tvv4eNHeIBPzpa2PUYCkOqYng9cfPR4dAiEAwigUJYUCeY6i
6+
SwlLcV+eFdGDd9n10iy3v9hXmyGUr+0CIDO8mObV9+9zoFYmZO+6gkGtt8A9iTPG
7+
cGURvkSMDHnZAiBy65QZLSRs3M8VCPhdr9H7ahqd6yYEdDGC3UPlb7f5dQIhAM6Q
8+
HzyFgXw46pPHHfiTH5bNt6Ms97plq1waZcwMtwfT
159
-----END RSA PRIVATE KEY-----

keys/public.pem.example

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-----BEGIN PUBLIC KEY-----
2-
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIyPdrgt0HmEwGblU18SddVY1JHky3wS
3-
/kCrsTT6wr6jOFMR6QTw1puBzposESpMXLj4thbM2Rhl0XVRiYUJONkCAwEAAQ==
4-
-----END PUBLIC KEY-----
1+
-----BEGIN RSA PUBLIC KEY-----
2+
MEgCQQCej/gD7QcWnBLiOp9z23SA7C0dv7o/xVYp3DAWCgbaXN4wS/yTaal9dJpn
3+
ZDqTr+iYdLxN9eKvWYPPI0jY7rPZAgMBAAE=
4+
-----END RSA PUBLIC KEY-----

src/auth/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export default {
77
}).unknown(true),
88
auth: Joi.object().keys({
99
'x-access-token': Joi.string().required().min(1),
10-
'x-user-id': JoiObjectId,
10+
'x-user-id': JoiObjectId(),
1111
}).unknown(true)
1212
};

src/helpers/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export enum ValidationSource {
1111
PARAM = 'params'
1212
}
1313

14-
export const JoiObjectId = () => Joi.string().custom((value: string, helpers) => {
14+
export const JoiObjectId = () => Joi.string().required().custom((value: string, helpers) => {
1515
if (!Types.ObjectId.isValid(value))
1616
return helpers.error('any.invalid');
1717
return value;

src/routes/v1/access/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ router.post('/basic', validator(schema.userCredential),
2929
const tokens = await createTokens(user, accessTokenKey, refreshTokenKey);
3030

3131
new SuccessResponse('Login Success', {
32-
user: _.pick(user, ['name', 'email']),
32+
user: _.pick(user, ['_id', 'name', 'roles', 'profilePicUrl']),
3333
tokens: tokens
3434
}).send(res);
3535
}));

src/routes/v1/access/logout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const router = express.Router();
88

99
/*-------------------------------------------------------------------------*/
1010
// Below all APIs are private APIs protected for Access Token
11-
router.use('/', require('../../../auth/Authentication'));
11+
router.use('/', require('../../../auth/authentication'));
1212
/*-------------------------------------------------------------------------*/
1313

1414
router.delete('/',

src/routes/v1/access/schema.ts

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

src/routes/v1/access/signup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ router.post('/basic', validator(schema.signup),
3333

3434
const tokens = await createTokens(createdUser, keystore.primaryKey, keystore.secondaryKey);
3535
new SuccessResponse('Signup Successful', {
36-
user: _.pick(createdUser, ['name', 'email', 'roles', 'profilePicUrl']),
36+
user: _.pick(createdUser, ['_id', 'name', 'email', 'roles', 'profilePicUrl']),
3737
tokens: tokens,
3838
}).send(res);
3939
}));

src/routes/v1/blog/editor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const router = express.Router();
1515
/*-------------------------------------------------------------------------*/
1616
// Below all APIs are private APIs protected for Access Token and Editor's Role
1717
router.use('/',
18-
require('../../../auth/Authentication'),
18+
require('../../../auth/authentication'),
1919
(req: RoleRequest, res, next) => { req.currentRoleCode = RoleCode.EDITOR; next(); },
20-
require('../../../auth/Authorization'));
20+
require('../../../auth/authorization'));
2121
/*-------------------------------------------------------------------------*/
2222

2323
router.put('/publish/:id', validator(schema.blogId, ValidationSource.PARAM),

src/routes/v1/blog/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
url: Joi.string().required().uri()
77
}),
88
blogId: Joi.object().keys({
9-
id: JoiObjectId
9+
id: JoiObjectId()
1010
}),
1111
blogTag: Joi.object().keys({
1212
tag: Joi.string().required().min(1)
@@ -16,7 +16,7 @@ export default {
1616
pageItemCount: Joi.number().required().integer().min(1),
1717
}),
1818
authorId: Joi.object().keys({
19-
id: JoiObjectId
19+
id: JoiObjectId()
2020
}),
2121
blogCreate: Joi.object().keys({
2222
title: Joi.string().required().min(3).max(500),

src/routes/v1/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ app.use('/', require('../../auth/apikey'));
99

1010
app.use('/signup', require('./access/signup'));
1111
app.use('/login', require('./access/login'));
12+
app.use('/logout', require('./access/logout'));
1213
app.use('/token', require('./access/token'));
1314
app.use('/blogs', require('./blog/blogList'));
1415
app.use('/blog', require('./blog/blogDetail'));

src/routes/v1/profile/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JoiObjectId } from '../../../helpers/validator';
33

44
export default {
55
userId: Joi.object().keys({
6-
id: JoiObjectId
6+
id: JoiObjectId()
77
}),
88
profile: Joi.object().keys({
99
name: Joi.string().optional().min(1).max(200),

src/routes/v1/profile/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ router.get('/public/id/:id', validator(schema.userId, ValidationSource.PARAM),
2020

2121
/*-------------------------------------------------------------------------*/
2222
// Below all APIs are private APIs protected for Access Token
23-
router.use('/', require('../../../auth/Authentication'));
23+
router.use('/', require('../../../auth/authentication'));
2424
/*-------------------------------------------------------------------------*/
2525

2626
router.get('/my',

src/utils/JWT.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class JWT {
2424
}
2525

2626
public static async encode(payload: JwtPayload): Promise<string> {
27-
const cert = await JWT.readPrivateKey();
27+
const cert = await this.readPrivateKey();
2828
if (!cert)
2929
throw new InternalError('Token generation failure');
3030
// @ts-ignore
@@ -35,7 +35,7 @@ export default class JWT {
3535
* This method checks the token and returns the decoded data when token is valid in all respect
3636
*/
3737
public static async validate(token: string, validations: ValidationParams): Promise<JwtPayload> {
38-
const cert = await JWT.readPublicKey();
38+
const cert = await this.readPublicKey();
3939
try {
4040
// @ts-ignore
4141
return await promisify(verify)(token, cert, validations);
@@ -50,7 +50,7 @@ export default class JWT {
5050
* This method checks the token and returns the decoded data even when the token is expired
5151
*/
5252
public static async decode(token: string, validations: ValidationParams): Promise<JwtPayload> {
53-
const cert = await JWT.readPublicKey();
53+
const cert = await this.readPublicKey();
5454
try {
5555
// token is verified if it was encrypted by the private key
5656
// and if is still not expired then get the payload

0 commit comments

Comments
 (0)