Skip to content

Commit cea02af

Browse files
committed
Refactor middleware for readability purpose.
Update the readme for this repository
1 parent f623fb3 commit cea02af

File tree

2 files changed

+84
-50
lines changed

2 files changed

+84
-50
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# PRINT API based on Google Chrome
22

3+
[![Maintainability](https://api.codeclimate.com/v1/badges/c1f89cb8d7f346b1d4c4/maintainability)](https://codeclimate.com/github/AMBERSIVE/print-api/maintainability)
4+
35
This repository provides a simple api wrapper for puppeteer (the nodejs way to interact with google chrome headless). It also provides some functions to include it into a given application landscape (like creating the pdf and sending it back to a given endpoint of choice).
46

5-
The [CHANGELOG](CHANGELOG.md) gives you information
7+
The [CHANGELOG](CHANGELOG.md) gives you information about the latest changes.
68

79
## Installation
810

src/middlewares/auth.middleware.ts

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,91 @@ export class AuthMiddleware implements NestMiddleware {
1313

1414
async use(req: any, res: any, next: () => void) {
1515

16-
const jwtActive = this.configService.get<boolean>('jwt.active');
17-
const basicAuthActive = this.configService.get<boolean>('basicAuth.active');
18-
19-
if (jwtActive == true) {
20-
21-
// Authentication via JWT
22-
let token = req.headers.authorization ? req.headers.authorization.replace('Bearer ', '') : '';
23-
let secret = this.configService.get<string>('jwt.secret');
24-
25-
let verify = jwt.verify(token, secret, (err, decoded) => {
26-
if (err) {
27-
switch(err.name){
28-
case 'JsonWebTokenError':
29-
throw new HttpException({
30-
status: HttpStatus.UNAUTHORIZED,
31-
error: `[${err.name}]: ${err.message}`,
32-
message: err.message
33-
}, HttpStatus.UNAUTHORIZED);
34-
break;
35-
}
36-
}
37-
});
38-
}
39-
else if (basicAuthActive == true) {
40-
41-
// Basic authentiation
42-
let token = req.headers.authorization;
43-
let message;
44-
45-
if (token === undefined || token.indexOf('Basic ') !== 0) {
46-
message = 'invalid authentication.';
47-
throw new HttpException({
48-
status: HttpStatus.UNAUTHORIZED,
49-
error: `[BASIC AUTH]: ${message}`,
50-
message: message
51-
}, HttpStatus.UNAUTHORIZED);
52-
}
16+
const jwtActive:boolean = this.configService.get<string>('jwt.active') == "true";
17+
const basicAuthActive:boolean = this.configService.get<string>('basicAuth.active') == "true";
5318

54-
const base64Credentials = token.split(' ')[1];
55-
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
56-
const [username, password] = credentials.split(':');
57-
58-
if (this.configService.get<string>('basicAuth.user') != username || this.configService.get<string>('basicAuth.secret') != password) {
59-
message = 'invalid credentials.';
60-
throw new HttpException({
61-
status: HttpStatus.UNAUTHORIZED,
62-
error: `[BASIC AUTH]: ${message}`,
63-
message: message
64-
}, HttpStatus.UNAUTHORIZED);
65-
}
19+
let allowed:boolean = true;
20+
21+
if (jwtActive === true) {
22+
allowed = await this.checkJwt(req);
23+
}
24+
else if (basicAuthActive === true) {
25+
allowed = await this.checkBasicAuth(req);
26+
}
6627

28+
if (allowed == false) {
29+
throw new HttpException({
30+
status: HttpStatus.UNAUTHORIZED
31+
}, HttpStatus.UNAUTHORIZED);
6732
}
6833

6934
next();
7035
}
36+
37+
/**
38+
* Check if the validation with JWT works
39+
* @param req
40+
*/
41+
protected checkJwt(req: any):boolean {
42+
43+
// Authentication via JWT
44+
let token = req.headers.authorization ? req.headers.authorization.replace('Bearer ', '') : '';
45+
let secret = this.configService.get<string>('jwt.secret');
46+
47+
let verify = jwt.verify(token, secret, (err, decoded) => {
48+
if (err) {
49+
switch(err.name){
50+
case 'JsonWebTokenError':
51+
throw new HttpException({
52+
status: HttpStatus.UNAUTHORIZED,
53+
error: `[${err.name}]: ${err.message}`,
54+
message: err.message
55+
}, HttpStatus.UNAUTHORIZED);
56+
break;
57+
}
58+
}
59+
return true;
60+
});
61+
62+
return (verify == true);
63+
64+
}
65+
66+
/**
67+
* Check basic authentication
68+
* @param req
69+
*/
70+
protected checkBasicAuth(req:any):boolean {
71+
72+
// Basic authentiation
73+
let token:string = req.headers.authorization;
74+
let message:string;
75+
76+
if (token === undefined || token.indexOf('Basic ') !== 0) {
77+
message = 'invalid authentication.';
78+
throw new HttpException({
79+
status: HttpStatus.UNAUTHORIZED,
80+
error: `[BASIC AUTH]: ${message}`,
81+
message: message
82+
}, HttpStatus.UNAUTHORIZED);
83+
}
84+
85+
const base64Credentials = token.split(' ')[1];
86+
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
87+
const [username, password] = credentials.split(':');
88+
89+
if (this.configService.get<string>('basicAuth.user') != username || this.configService.get<string>('basicAuth.secret') != password) {
90+
message = 'invalid credentials.';
91+
throw new HttpException({
92+
status: HttpStatus.UNAUTHORIZED,
93+
error: `[BASIC AUTH]: ${message}`,
94+
message: message
95+
}, HttpStatus.UNAUTHORIZED);
96+
return false;
97+
}
98+
99+
return true;
100+
101+
}
102+
71103
}

0 commit comments

Comments
 (0)