Skip to content

Commit d5a6bc2

Browse files
committed
bugfixes
1 parent 6fc619d commit d5a6bc2

File tree

13 files changed

+77
-96
lines changed

13 files changed

+77
-96
lines changed

Projects/BooksLibrary/src/api/controllers/user.controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { Authorizer } from 'auth/authorizer';
12
import express from 'express';
2-
import { Loader } from '../../startup/loader';
3-
import { UserService } from '../../services/user.service';
3+
import { UserService } from 'services/user.service';
4+
import { Loader } from 'startup/loader';
45

56
export class UserController {
67
//#region member variables and constructors

Projects/BooksLibrary/src/api/routes/user.routes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export const register = (app: express.Application): void => {
99
const authenticator = Loader.authenticator;
1010
const controller = new UserController();
1111

12-
router.post('/', authenticator.authenticateClient, controller.create);
13-
router.get('/', authenticator.authenticateClient, authenticator.authenticateUser, controller.search);
14-
router.get('/:id', authenticator.authenticateClient, authenticator.authenticateUser, controller.getById);
15-
router.delete('/:id', authenticator.authenticateClient, authenticator.authenticateUser, controller.delete);
12+
router.post('/', controller.create);
13+
router.get('/', authenticator.authenticateUser, controller.search);
14+
router.get('/:id', authenticator.authenticateUser, controller.getById);
15+
router.delete('/:id', authenticator.authenticateUser, controller.delete);
1616

1717
app.use('/api/v1/users', router);
1818
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"SystemIdentifier": "REAN HealthGuru",
3+
"Auth": {
4+
"Authentication": "Custom",
5+
"Authorization": "Custom"
6+
},
7+
"Database": {
8+
"Type": "SQL",
9+
"ORM": "Sequelize",
10+
"Flavour": "MySQL"
11+
},
12+
"FileStorage": {
13+
"Provider": "AWS-S3"
14+
},
15+
"Communication": {
16+
"SMS": {
17+
"Provider": "Twilio"
18+
},
19+
"Email": {
20+
"Provider": "SendGrid"
21+
},
22+
"InAppNotifications": {
23+
"Provider": "Firebase"
24+
}
25+
},
26+
"TemporaryFolders": {
27+
"Upload": "./tmp/resources/uploads/",
28+
"Download": "./tmp/resources/downloads/",
29+
"CleanupFolderBeforeMinutes": 10
30+
},
31+
"MaxUploadFileSize": 104857600
32+
}

Projects/BooksLibrary/src/auth/auth.injector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { ConfigurationManager } from 'config/configuration.manager';
12
import 'reflect-metadata';
2-
import { ConfigurationManager } from '../config/configuration.manager';
33
import { DependencyContainer } from 'tsyringe';
44
import { CustomAuthenticator } from './custom/custom.authenticator';
55
import { CustomAuthorizer } from './custom/custom.authorizer';

Projects/BooksLibrary/src/auth/authenticator.interface.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ import { AuthenticationResult } from '../domain.types/auth/auth.domain.types';
33

44
export interface IAuthenticator {
55
authenticateUser(request: express.Request): Promise<AuthenticationResult>;
6-
7-
authenticateClient(request: express.Request): Promise<AuthenticationResult>;
86
}

Projects/BooksLibrary/src/auth/authenticator.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,7 @@ export class Authenticator {
3131
}
3232
};
3333

34-
public authenticateClient = async (
35-
request: express.Request,
36-
response: express.Response,
37-
next: express.NextFunction
38-
): Promise<boolean> => {
39-
try {
40-
const authResult = await this._authenticator.authenticateClient(request);
41-
if (authResult.Result === false) {
42-
ResponseHandler.failure(request, response, authResult.Message, authResult.HttpErrorCode);
43-
return false;
44-
}
45-
next();
46-
} catch (error) {
47-
Logger.instance().log(error.message);
48-
ResponseHandler.failure(request, response, 'Client authentication error: ' + error.message, 401);
49-
}
50-
};
51-
5234
public checkAuthentication = async (request: express.Request): Promise<boolean> => {
53-
const clientAuthResult = await this._authenticator.authenticateClient(request);
54-
if (clientAuthResult.Result === false) {
55-
throw new ApiError(401, 'Unauthorized access');
56-
}
5735
const userAuthResult = await this._authenticator.authenticateUser(request);
5836
if (userAuthResult.Result === false) {
5937
throw new ApiError(401, 'Unauthorized access');

Projects/BooksLibrary/src/auth/authorizer.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ export class Authorizer {
2222
return await this._authorizer.generateUserSessionToken(user);
2323
};
2424
}
25-
26-
////////////////////////////////////////////////////////////////////////

Projects/BooksLibrary/src/auth/custom/custom.authenticator.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@ import express from 'express';
22
import jwt from 'jsonwebtoken';
33
import { Logger } from '../../common/logger';
44
import { AuthenticationResult } from '../../domain.types/auth/auth.domain.types';
5-
import { CurrentClient } from '../../domain.types/miscellaneous/current.client';
6-
import { ApiClientService } from '../../services/api.client.service';
7-
import { Loader } from '../../startup/loader';
85
import { IAuthenticator } from '../authenticator.interface';
96

107
//////////////////////////////////////////////////////////////
118

129
export class CustomAuthenticator implements IAuthenticator {
13-
_clientService: ApiClientService = null;
14-
15-
constructor() {
16-
this._clientService = Loader.container.resolve(ApiClientService);
17-
}
18-
1910
public authenticateUser = async (request: express.Request): Promise<AuthenticationResult> => {
2011
try {
2112
var res: AuthenticationResult = {
@@ -57,43 +48,4 @@ export class CustomAuthenticator implements IAuthenticator {
5748
}
5849
return res;
5950
};
60-
61-
public authenticateClient = async (request: express.Request): Promise<AuthenticationResult> => {
62-
try {
63-
var res: AuthenticationResult = {
64-
Result: true,
65-
Message: 'Authenticated',
66-
HttpErrorCode: 200,
67-
};
68-
let apiKey: string = request.headers['x-api-key'] as string;
69-
if (!apiKey) {
70-
res = {
71-
Result: false,
72-
Message: 'Missing API key for the client',
73-
HttpErrorCode: 401,
74-
};
75-
return res;
76-
}
77-
apiKey = apiKey.trim();
78-
79-
const client: CurrentClient = await this._clientService.isApiKeyValid(apiKey);
80-
if (!client) {
81-
res = {
82-
Result: false,
83-
Message: 'Invalid API Key: Forebidden access',
84-
HttpErrorCode: 403,
85-
};
86-
return res;
87-
}
88-
request.currentClient = client;
89-
} catch (err) {
90-
Logger.instance().log(JSON.stringify(err, null, 2));
91-
res = {
92-
Result: false,
93-
Message: 'Error authenticating client',
94-
HttpErrorCode: 401,
95-
};
96-
}
97-
return res;
98-
};
9951
}

Projects/BooksLibrary/src/auth/custom/custom.authorizer.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import jwt from 'jsonwebtoken';
44
import { Logger } from '../../common/logger';
55
import { IAuthorizer } from '../authorizer.interface';
66
import { CurrentUser } from '../../domain.types/miscellaneous/current.user';
7-
import { RolePrivilegeService } from '../../services/role.privilege.service';
87
import { Loader } from '../../startup/loader';
98

109
//const execSync = require('child_process').execSync;
1110

1211
//////////////////////////////////////////////////////////////
1312

1413
export class CustomAuthorizer implements IAuthorizer {
15-
1614
_rolePrivilegeService: RolePrivilegeService = null;
1715

1816
constructor() {
@@ -31,7 +29,8 @@ export class CustomAuthorizer implements IAuthorizer {
3129
}
3230
const hasPrivilege = await this._rolePrivilegeService.hasPrivilegeForRole(
3331
currentUser.CurrentRoleId,
34-
context);
32+
context
33+
);
3534

3635
if (!hasPrivilege) {
3736
return false;
@@ -67,12 +66,10 @@ export class CustomAuthorizer implements IAuthorizer {
6766
};
6867

6968
private hasConsent = async (currentRoleId: number, context: string): Promise<boolean> => {
70-
7169
Logger.instance().log('Current role id: ' + currentRoleId);
7270
Logger.instance().log('Context: ' + context);
7371

7472
//for time being, return true always
7573
return true;
7674
};
77-
7875
}

Projects/BooksLibrary/src/config/configuration.manager.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
import { Configurations } from './configuration.types';
1+
import { AuthenticationType, AuthorizationType, Configurations } from './configuration.types';
2+
import * as configuration from 'app.config.json';
23

34
export class ConfigurationManager {
45
static _config: Configurations = null;
56

67
public static loadConfigurations = (): void => {
78
ConfigurationManager._config = {
8-
BaseUrl : process.env.BASE_URL,
9-
SystemIdentifier : '',
10-
MaxUploadFileSize : 0,
9+
BaseUrl: process.env.BASE_URL,
10+
SystemIdentifier: '',
11+
MaxUploadFileSize: 0,
12+
Auth: {
13+
Authentication: configuration.Auth.Authentication as AuthenticationType,
14+
Authorization: configuration.Auth.Authorization as AuthorizationType,
15+
},
1116
};
1217
};
1318

19+
public static Authentication = (): AuthenticationType => {
20+
return ConfigurationManager._config.Auth.Authentication;
21+
};
22+
23+
public static Authorization = (): AuthorizationType => {
24+
return ConfigurationManager._config.Auth.Authorization;
25+
};
26+
1427
public static MaxUploadFileSize = (): number => {
1528
return ConfigurationManager._config.MaxUploadFileSize;
1629
};

0 commit comments

Comments
 (0)