@@ -2,11 +2,12 @@ import { NextFunction, Request, Response, Router } from "express";
2
2
import Logger from "../../loaders/logger" ;
3
3
4
4
import CTFService from "../../services/CTF" ;
5
- import { UnauthorizedError } from "../../types/httperrors" ;
6
5
import { notImplemented } from "../../util" ;
6
+ import attachUser from "../../util/middleware/user" ;
7
7
8
8
export default ( ) : Router => {
9
9
const router = Router ( { mergeParams : true } ) ;
10
+ router . use ( attachUser ( ) ) ;
10
11
11
12
router . route ( "/" ) . get ( listCTFs ) . post ( createCTF ) . all ( notImplemented ) ;
12
13
router . route ( "/:ctfID" ) . get ( getCTF ) . all ( notImplemented ) ;
@@ -19,96 +20,60 @@ export default (): Router => {
19
20
const _CTFService = new CTFService ( ) ;
20
21
21
22
function createCTF ( req : Request , res : Response , next : NextFunction ) {
22
- if ( ! req . headers . authorization ) {
23
- next (
24
- new UnauthorizedError ( {
25
- errorMessage : "Missing authorization" ,
26
- errorCode : "error_unauthorized" ,
27
- } )
28
- ) ;
29
- }
30
-
31
- Logger . verbose ( `Creating new CTF for team ${ req . params . teamID } ` ) ;
23
+ Logger . verbose ( `Creating new CTF for team ${ req . team . _id } ` ) ;
32
24
Logger . debug ( JSON . stringify ( { ...req . body } ) ) ;
33
25
_CTFService
34
- . createCTF ( req . headers . authorization . slice ( 7 ) , req . params . teamID , req . body )
26
+ . createCTF ( req . user , req . team , req . body )
35
27
. then ( ( ctfDetails ) => {
36
28
res . status ( 201 ) . send ( ctfDetails ) ;
37
29
} )
38
30
. catch ( ( err ) => next ( err ) ) ;
39
31
}
40
32
41
33
function listCTFs ( req : Request , res : Response , next : NextFunction ) {
42
- if ( ! req . headers . authorization ) {
43
- next (
44
- new UnauthorizedError ( {
45
- errorMessage : "Missing authorization" ,
46
- errorCode : "error_unauthorized" ,
47
- } )
48
- ) ;
49
- }
50
-
51
- Logger . verbose ( `Getting list of CTFs for team ${ req . params . teamID } ` ) ;
34
+ Logger . verbose ( `Getting list of CTFs for team ${ req . team . _id } ` ) ;
52
35
_CTFService
53
- . listCTFs ( req . headers . authorization . slice ( 7 ) , req . params . teamID , req . body . includeArchived ?? undefined )
36
+ . listCTFs (
37
+ req . user ,
38
+ req . team ,
39
+ req . body . includeArchived ?? undefined
40
+ )
54
41
. then ( ( CTFs ) => {
55
42
res . status ( 200 ) . send ( CTFs ) ;
56
43
} )
57
44
. catch ( ( err ) => next ( err ) ) ;
58
45
}
59
46
60
47
function getCTF ( req : Request , res : Response , next : NextFunction ) {
61
- if ( ! req . headers . authorization ) {
62
- next (
63
- new UnauthorizedError ( {
64
- errorMessage : "Missing authorization" ,
65
- errorCode : "error_unauthorized" ,
66
- } )
67
- ) ;
68
- }
69
-
70
- Logger . verbose ( `Getting CTF with ID ${ req . params . ctfID } from team ${ req . params . teamID } ` ) ;
48
+ Logger . verbose (
49
+ `Getting CTF with ID ${ req . params . ctfID } from team ${ req . team . _id } `
50
+ ) ;
71
51
_CTFService
72
- . getCTF ( req . headers . authorization . slice ( 7 ) , req . params . teamID , req . params . ctfID )
52
+ . getCTF ( req . user , req . team , req . params . ctfID )
73
53
. then ( ( CTF ) => {
74
54
res . status ( 200 ) . send ( CTF ) ;
75
55
} )
76
56
. catch ( ( err ) => next ( err ) ) ;
77
57
}
78
58
79
-
80
59
function archiveCTF ( req : Request , res : Response , next : NextFunction ) {
81
- if ( ! req . headers . authorization ) {
82
- next (
83
- new UnauthorizedError ( {
84
- errorMessage : "Missing authorization" ,
85
- errorCode : "error_unauthorized" ,
86
- } )
87
- ) ;
88
- }
89
-
90
- Logger . verbose ( `Archiving CTF with ID ${ req . params . ctfID } in team ${ req . params . teamID } ` ) ;
60
+ Logger . verbose (
61
+ `Archiving CTF with ID ${ req . params . ctfID } in team ${ req . team . _id } `
62
+ ) ;
91
63
_CTFService
92
- . archiveCTF ( req . headers . authorization . slice ( 7 ) , req . params . teamID , req . params . ctfID )
64
+ . archiveCTF ( req . user , req . team , req . params . ctfID )
93
65
. then ( ( CTF ) => {
94
66
res . status ( 200 ) . send ( CTF ) ;
95
67
} )
96
68
. catch ( ( err ) => next ( err ) ) ;
97
69
}
98
70
99
71
function unarchiveCTF ( req : Request , res : Response , next : NextFunction ) {
100
- if ( ! req . headers . authorization ) {
101
- next (
102
- new UnauthorizedError ( {
103
- errorMessage : "Missing authorization" ,
104
- errorCode : "error_unauthorized" ,
105
- } )
106
- ) ;
107
- }
108
-
109
- Logger . verbose ( `Unarchiving CTF with ID ${ req . params . ctfID } in team ${ req . params . teamID } ` ) ;
72
+ Logger . verbose (
73
+ `Unarchiving CTF with ID ${ req . params . ctfID } in team ${ req . team . _id } `
74
+ ) ;
110
75
_CTFService
111
- . unarchiveCTF ( req . headers . authorization . slice ( 7 ) , req . params . teamID , req . params . ctfID )
76
+ . unarchiveCTF ( req . user , req . team , req . params . ctfID )
112
77
. then ( ( CTF ) => {
113
78
res . status ( 200 ) . send ( CTF ) ;
114
79
} )
0 commit comments