Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoBackendAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dojo Project (HES-SO)
Projects
Backend
DojoBackendAPI
Commits
9354feb9
Commit
9354feb9
authored
2 years ago
by
michael.minelli
Browse files
Options
Downloads
Patches
Plain Diff
Add a middleware for security verifications
parent
920c5574
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ExpressAPI/src/middlewares/SecurityMiddleware.ts
+51
-0
51 additions, 0 deletions
ExpressAPI/src/middlewares/SecurityMiddleware.ts
ExpressAPI/src/types/SecurityCheckType.ts
+5
-0
5 additions, 0 deletions
ExpressAPI/src/types/SecurityCheckType.ts
with
56 additions
and
0 deletions
ExpressAPI/src/middlewares/SecurityMiddleware.ts
0 → 100644
+
51
−
0
View file @
9354feb9
import
express
from
'
express
'
;
import
{
StatusCodes
}
from
'
http-status-codes
'
;
import
SecurityCheckType
from
'
../types/SecurityCheckType
'
;
import
logger
from
'
../shared/logging/WinstonLogger
'
;
import
ApiRequest
from
'
../models/ApiRequest
'
;
class
SecurityMiddleware
{
private
static
_instance
:
SecurityMiddleware
;
private
constructor
()
{
}
public
static
get
instance
():
SecurityMiddleware
{
if
(
!
SecurityMiddleware
.
_instance
)
{
SecurityMiddleware
.
_instance
=
new
SecurityMiddleware
();
}
return
SecurityMiddleware
.
_instance
;
}
//Check if at least ONE rule match. It's NOT an AND but it's a OR function. For IsJuryUnlock, IsStudentUnlock and IsScheduleUnlock it's cumulative
check
(...
checkTypes
:
Array
<
SecurityCheckType
>
):
(
req
:
ApiRequest
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
)
=>
void
{
return
async
(
req
:
ApiRequest
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
)
=>
{
let
isAllowed
=
checkTypes
.
length
===
0
;
if
(
!
isAllowed
)
{
for
(
let
checkType
of
checkTypes
)
{
try
{
switch
(
checkType
)
{
default
:
isAllowed
=
isAllowed
||
false
;
break
;
}
}
catch
(
e
)
{
logger
.
error
(
'
Security check failed !!! =>
'
+
e
);
isAllowed
=
isAllowed
||
false
;
}
}
}
if
(
!
isAllowed
)
{
return
req
.
session
.
sendResponse
(
res
,
StatusCodes
.
FORBIDDEN
);
}
return
next
();
};
}
}
export
default
SecurityMiddleware
.
instance
;
This diff is collapsed.
Click to expand it.
ExpressAPI/src/types/SecurityCheckType.ts
0 → 100644
+
5
−
0
View file @
9354feb9
enum
SecurityCheckType
{
}
export
default
SecurityCheckType
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment