Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
corps-directory
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Container 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
fhurtado14
corps-directory
Merge requests
!18
Feature/jwt backend
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Feature/jwt backend
feature/jwt-backend
into
main
Overview
0
Commits
4
Pipelines
0
Changes
21
Merged
fhurtado14
requested to merge
feature/jwt-backend
into
main
4 months ago
Overview
0
Commits
4
Pipelines
0
Changes
21
Expand
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
2b3f7947
4 commits,
4 months ago
21 files
+
359
−
21273
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
21
Search (e.g. *.vue) (Ctrl+P)
backend/config/auth/authenticateJWT.js
+
140
−
1
Options
@@ -25,4 +25,143 @@ const authenticateJWT = (req, res, next) => {
});
};
module
.
exports
=
authenticateJWT
;
/*
Function to authenticate admin only functions
(create, delete, searchByName)
*/
const
authenticateAdminOnly
=
(
req
,
res
,
next
)
=>
{
// Check for bypass header - used in Postman
const
bypassToken
=
req
.
headers
[
"
x-dev-bypass
"
];
if
(
bypassToken
===
"
allow-dev-access
"
)
{
console
.
log
(
"
Bypassing authentication due to Postman bypass header.
"
);
req
.
user
=
{
role
:
"
admin
"
,
peopleId
:
1
,
};
return
next
();
}
// Get token from Authorization header
const
token
=
req
.
headers
.
authorization
?.
split
(
"
"
)[
1
];
// return early if no token provided
if
(
!
token
)
{
return
res
.
status
(
401
)
.
json
({
message
:
"
Access denied. No token provided.
"
});
}
try
{
// Verify the token
const
decoded
=
jwt
.
verify
(
token
,
JWT_SECRET
);
req
.
user
=
decoded
;
// Add the decoded token to the request object for later use
// extract the role from the token
const
role
=
decoded
.
role
;
if
(
!
role
)
{
return
res
.
status
(
403
).
json
({
message
:
"
Role information is missing.
"
});
}
// Handle role-based validation
if
(
role
===
"
admin
"
||
role
===
"
classChampion
"
||
role
===
"
orgLeader
"
)
{
// Admins pass automatically
req
.
user
=
decoded
;
return
next
();
}
else
{
// Other roles are not authenticated
return
res
.
status
(
403
)
.
json
({
message
:
"
User is not authorized to access this resource.
"
});
}
}
catch
(
err
)
{
return
res
.
status
(
403
).
json
({
message
:
"
Invalid or expired token.
"
});
}
};
/*
Function to authenticate functions that can be used by admin or
users themselves
(update, fullInfo)
*/
const
authenticateAdminAndPersonal
=
(
req
,
res
,
next
)
=>
{
// Check for bypass header - used in Postman
const
bypassToken
=
req
.
headers
[
"
x-dev-bypass
"
];
if
(
bypassToken
===
"
allow-dev-access
"
)
{
console
.
log
(
"
Bypassing authentication due to Postman bypass header.
"
);
req
.
user
=
{
role
:
"
admin
"
,
peopleId
:
1
,
};
return
next
();
}
// Get token from Authorization header
const
token
=
req
.
headers
.
authorization
?.
split
(
"
"
)[
1
];
// return early if no token provided
if
(
!
token
)
{
return
res
.
status
(
401
)
.
json
({
message
:
"
Access denied. No token provided.
"
});
}
try
{
// Verify the token
const
decoded
=
jwt
.
verify
(
token
,
JWT_SECRET
);
req
.
user
=
decoded
;
// Add the decoded token to the request object for later use
// extract the role from the token
const
role
=
decoded
.
role
;
const
peopleId
=
decoded
.
peopleId
;
const
reqPeopleId
=
req
.
params
;
console
.
log
(
"
Role:
"
,
role
);
console
.
log
(
"
PeopleId:
"
,
peopleId
);
console
.
log
(
"
param gotten from url:
"
,
reqPeopleId
);
if
(
!
role
)
{
return
res
.
status
(
403
).
json
({
message
:
"
Role information is missing.
"
});
}
// Handle role-based validation
if
(
role
===
"
admin
"
||
role
===
"
classChampion
"
||
role
===
"
orgLeader
"
)
{
// Admins pass automatically
req
.
user
=
decoded
;
console
.
log
(
"
admin passing
"
);
return
next
();
}
else
if
(
role
===
"
user
"
)
{
// need to verify the user is making the request on themselves
if
(
!
reqPeopleId
)
{
return
res
.
status
(
400
)
.
json
({
message
:
"
peopleId is required as a URL parameter.
"
});
}
if
(
reqPeopleId
===
peopleId
)
{
req
.
user
=
decoded
;
return
next
();
}
// condition is not met
return
res
.
status
(
403
)
.
json
({
message
:
"
User is not authorized to access this resource.
"
});
}
else
{
// Other roles are not authenticated
return
res
.
status
(
403
)
.
json
({
message
:
"
User is not authorized to access this resource.
"
});
}
}
catch
(
err
)
{
return
res
.
status
(
403
).
json
({
message
:
"
Invalid or expired token.
"
});
}
};
module
.
exports
=
{
authenticateJWT
,
authenticateAdminOnly
,
authenticateAdminAndPersonal
,
};
Loading