Skip to content

Commit de70c29

Browse files
committed
Add support for getting all the files from the storage directory
Fix http protocol bug Change the permissions for defining a create and read permission for this endpoint Updating the documentation
1 parent 9603713 commit de70c29

File tree

8 files changed

+104
-72
lines changed

8 files changed

+104
-72
lines changed

.env

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
JWT_SECRET=2VRxS0s15nV2BnyVYcgBvKJwoaPeQdVXsaJylt96Jb9iypXOGylcTCTo8rS1E7Mk
2-
JWT_ACTIVE=false
3-
BASIC_ACTIVE=false
1+
JWT_SECRET=PPNBhcIhU0i7MWF2siMNl6nq5icNLV9ePeBfJfuqTkmOj867jY6yKqetV12J0kTt
2+
JWT_ACTIVE=true
3+
BASIC_ACTIVE=true
44
BASIC_USER=test
55
BASIC_SECRET=asdf

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.0] - 2020-07-29
8+
### Added
9+
- Added [GET] /browse to get all files available on the print server.
10+
- JWT: [GET] /browse requires "browse-read" as permission or a custom permission which can be addressed by setting the environment variable "PERMISSION_BROWSE_READ"
11+
12+
### Fixed
13+
- Fixed http-protocol bug where links where always http.
14+
15+
### Changed
16+
- JWT: [POST] /browse requires now the permission "browse-create" instead of "browse" or a custom permission which can be addressed by setting the environment variable "PERMISSION_BROWSE_CREATE"
17+
18+
719
## [0.1.2] - 2020-06-28
820
### Fixed
921
- Issue with decision if you want to use JWT or Basic Authentication

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ JWT_SECRET=2VRxS0s15nV2BnyVYcgBvKJwoaPeQdVXsaJylt96Jb9iypXOGylcTCTo8rS1E7Mk
2121
JWT_ISSUER=http://localhost:8000/api/auth/login
2222
APP_KEY=
2323
PORT=3000
24-
PERMISSION_BROWSE=print
24+
PERMISSION_BROWSE_CREATE=print
25+
PERMISSION_BROWSE_READ=print
2526
```
2627

2728
## Run the service
@@ -45,8 +46,8 @@ If you want to use the JWT Authentication the JWT must contain custom claims wit
4546
- roles (Array)
4647
- permissions (Array)
4748

48-
If the claim contains a "*" or the given *PERMISSIONS_PRINT* key it will proceed.
49-
49+
[POST]: If the claim contains a "*" or the given *PERMISSIONS_PRINT_CREATE* key it will proceed.
50+
[GET]: If the claim contains a "*" or the given *PERMISSIONS_PRINT_READ* key it will proceed.
5051
#### JWT_ACTIVE (env key)
5152

5253
Default: true

package-lock.json

Lines changed: 38 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
"test:e2e": "jest --config ./test/jest-e2e.json"
2323
},
2424
"dependencies": {
25-
"@nestjs/common": "^7.2.0",
25+
"@nestjs/common": "^7.4.1",
2626
"@nestjs/config": "^0.5.0",
27-
"@nestjs/core": "^7.2.0",
28-
"@nestjs/platform-express": "^7.2.0",
27+
"@nestjs/core": "^7.4.1",
28+
"@nestjs/platform-express": "^7.4.1",
2929
"@nestjs/typeorm": "^7.1.0",
3030
"axios": "^0.19.2",
3131
"class-transformer": "^0.2.3",
@@ -38,19 +38,19 @@
3838
"puppeteer": "^3.3.0",
3939
"reflect-metadata": "^0.1.13",
4040
"rimraf": "^3.0.2",
41-
"rxjs": "^6.5.4",
41+
"rxjs": "^6.6.0",
4242
"signed": "^1.0.3",
4343
"typeorm": "^0.2.25",
44-
"uuid": "^8.2.0"
44+
"uuid": "^8.3.0"
4545
},
4646
"devDependencies": {
4747
"@nestjs/cli": "^7.4.1",
4848
"@nestjs/schematics": "^7.0.0",
49-
"@nestjs/testing": "^7.2.0",
50-
"@types/express": "^4.17.3",
49+
"@nestjs/testing": "^7.4.1",
50+
"@types/express": "^4.17.7",
5151
"@types/jest": "25.2.3",
52-
"@types/node": "^13.9.1",
53-
"@types/supertest": "^2.0.8",
52+
"@types/node": "^13.13.15",
53+
"@types/supertest": "^2.0.10",
5454
"@typescript-eslint/eslint-plugin": "3.0.2",
5555
"@typescript-eslint/parser": "3.0.2",
5656
"eslint": "7.1.0",
@@ -64,7 +64,7 @@
6464
"ts-loader": "^6.2.1",
6565
"ts-node": "^8.6.2",
6666
"tsconfig-paths": "^3.9.0",
67-
"typescript": "^3.7.4",
67+
"typescript": "^3.9.7",
6868
"webpack-node-externals": "^1.7.2"
6969
},
7070
"jest": {

src/browser/browser.controller.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ export class BrowserController {
2424
});
2525
}
2626

27+
@Get("api/browse")
28+
@UseGuards(BrowseGuard)
29+
async files(@Param() params, @Req() req: Request, @Res() res: Response) {
30+
31+
let result = await fs.promises.readdir(`./storage/`);
32+
33+
result = result.filter(file => {
34+
if (file.substr(0,1) !== '.') {
35+
return file;
36+
}
37+
});
38+
39+
40+
result = result.map(file => {
41+
file = file.substr(0, file.length - 4);
42+
return this.signature.sign(`${req.protocol}://${req.headers.host}/api/browse/${file}`);
43+
})
44+
45+
res.status(HttpStatus.OK).json(result);
46+
47+
}
48+
2749
@Get("api/browse/:id")
2850
async file(@Param() params, @Req() req: Request, @Res() res: Response) {
2951

@@ -73,7 +95,7 @@ export class BrowserController {
7395
res.status(HttpStatus.OK).json(new PdfResult({
7496
statusCode: HttpStatus.OK,
7597
requestUrl: createSession.url,
76-
downloadUrl: resultUpload == false ? this.signature.sign(`http://${request.headers.host}/api/browse/${result.id}`) : null,
98+
downloadUrl: resultUpload == false ? this.signature.sign(`${request.protocol}://${request.headers.host}/api/browse/${result.id}`) : null,
7799
filename: result.id,
78100
uploaded: resultUpload,
79101
waited: createSession.postBackWait

src/configuration/configuration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default () => ({
1212
secret: process.env.BASIC_SECRET || null
1313
},
1414
permissions: {
15-
browse: process.env.PERMISSION_BROWSE || 'browse'
15+
browseCreate: process.env.PERMISSION_BROWSE_CREATE || 'browse-create',
16+
browseRead: process.env.PERMISSION_BROWSE_READ || 'browse-read'
1617
},
1718
browser: process.env.BROWSER_PATH || null
1819
});

src/guards/browse.guard.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const jwt = require('jsonwebtoken');
88
export class BrowseGuard implements CanActivate {
99

1010
constructor(@Inject('ConfigService') private configService: ConfigService) {
11-
11+
1212
}
1313

1414
canActivate(
@@ -19,9 +19,19 @@ export class BrowseGuard implements CanActivate {
1919
const jwtActive = this.configService.get<boolean>('jwt.active');
2020
const token = request.headers.authorization ? request.headers.authorization.replace('Bearer ', '') : '';
2121

22+
let permissionFromConfig: string = "";
23+
switch(request.method) {
24+
case 'GET':
25+
permissionFromConfig = "permissions.browseRead";
26+
break;
27+
case 'POST':
28+
permissionFromConfig = "permissions.browseCreate";
29+
break;
30+
}
31+
2232
if (jwtActive == true) {
2333
const data = jwt.decode(token);
24-
return data.permissions === undefined || data.permissions.indexOf(this.configService.get<string>('permissions.browse')) > -1 || data.permissions.indexOf('*') > -1;
34+
return data.permissions === undefined || data.permissions.indexOf(this.configService.get<string>(permissionFromConfig)) > -1 || data.permissions.indexOf('*') > -1;
2535
}
2636
else {
2737
return true;

0 commit comments

Comments
 (0)