Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pentest 31462 #1586

Merged
merged 49 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
716b27a
added x-id-token header to getProfile request
anyoussefinia Dec 9, 2024
e3ba2ba
add backend check for allowed roles/terretories
anyoussefinia Dec 10, 2024
7aac43d
remove and reinstall node modules
anyoussefinia Dec 10, 2024
e458d5c
remove and reinstall node modules
anyoussefinia Dec 10, 2024
10b7634
fix lint error
anyoussefinia Dec 10, 2024
e956111
use previous version of jwt-decode 3.1.x
anyoussefinia Dec 10, 2024
61ad41b
modify token header to idToken
anyoussefinia Dec 10, 2024
5691dfe
modify token header to idToken
anyoussefinia Dec 10, 2024
135364b
attempt to modify getUser
anyoussefinia Dec 10, 2024
387a827
try adding idtoken to body
anyoussefinia Dec 10, 2024
68f61ae
change api method to post
anyoussefinia Dec 10, 2024
c4b9677
parse the user roles
anyoussefinia Dec 10, 2024
e0132f3
added getUserV2 api method
anyoussefinia Dec 11, 2024
d9904a0
remove unused imports
anyoussefinia Dec 11, 2024
4483eea
resassign type
anyoussefinia Dec 11, 2024
1dfe65e
revert table name
anyoussefinia Dec 11, 2024
300d660
add back serverless plugins
anyoussefinia Dec 11, 2024
67cffa8
add back serverless plugins
anyoussefinia Dec 11, 2024
d189f6d
modify import
anyoussefinia Dec 11, 2024
2ee3df6
add check for if no userRoles/initial login
anyoussefinia Dec 11, 2024
6caf24e
try double quoted string
anyoussefinia Dec 11, 2024
e475895
add back keyword custom
anyoussefinia Dec 12, 2024
a10c404
bring in other pentest branch to test batch function
anyoussefinia Dec 13, 2024
fd7b87a
sync package-lock
anyoussefinia Dec 13, 2024
5d8d67f
added one more condition to if check
anyoussefinia Dec 13, 2024
a0b2d5f
update if condition with one more check
anyoussefinia Dec 13, 2024
f7b3430
modify batch function with better logging
anyoussefinia Dec 13, 2024
852a8b8
wrap error with try catch for better logging
anyoussefinia Dec 13, 2024
e2b631a
fix property name to extract roles form decode auth token
anyoussefinia Feb 5, 2025
6f05cbb
check matching roles correct .status
anyoussefinia Feb 5, 2025
9b2e1b8
add logging for troubleshoot
anyoussefinia Feb 5, 2025
85b9745
add extra logging
anyoussefinia Feb 5, 2025
e4b8472
add logging
anyoussefinia Feb 5, 2025
9c8f767
parse JSON where needee
anyoussefinia Feb 5, 2025
780b70a
working commit, need to remove excessive logging
anyoussefinia Feb 5, 2025
287f422
remove excess logging
anyoussefinia Feb 6, 2025
fa3b49f
remove comment
anyoussefinia Feb 6, 2025
36fef7e
added token verificaiton service to check that JWT is valid
anyoussefinia Feb 8, 2025
21f2ea0
lint fix
anyoussefinia Feb 10, 2025
a3bfc82
added packages
anyoussefinia Feb 10, 2025
8d26a26
added packages
anyoussefinia Feb 10, 2025
f888507
name change to getUserProfileInfo, added check against cognito public…
anyoussefinia Feb 10, 2025
1d56a80
lint fix
anyoussefinia Feb 10, 2025
7a007a9
move enviornment variables in function scope
anyoussefinia Feb 10, 2025
bf64a97
lint fix
anyoussefinia Feb 10, 2025
884cfad
trying to fetch user pool id using serverless
anyoussefinia Feb 10, 2025
2229963
remove excess logging comments
anyoussefinia Feb 10, 2025
a6f0453
cleanup
anyoussefinia Feb 10, 2025
148279f
cleanup
anyoussefinia Feb 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/admin/handlers/addRolesToJWT.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const handler = async (event) => {
event.response.claimsOverrideDetails.claimsToAddOrOverride = event.response.claimsOverrideDetails.claimsToAddOrOverride || {};

// Example of adding roles dynamically from DynamoDB to the JWT claims
event.response.claimsOverrideDetails.claimsToAddOrOverride['custom:user_roles'] = JSON.stringify(roles); // Add user roles
event.response.claimsOverrideDetails.claimsToAddOrOverride["custom:user_roles"] = JSON.stringify(roles); // Add user roles
} catch(e) {
console.log("error updating id token claims", e)
}
Expand Down
12 changes: 9 additions & 3 deletions services/admin/handlers/batchUpdateCognitoUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ async function processCognitoUsers() {
try {
const externalUser = await getUser(userEmail);
let roles = [""];
if (externalUser.roleList) {
let roleList;
try{
roleList = externalUser.roleList;
}catch(error) {
noRolesCounter ++
console.log(userEmail + " has no roles");
}
if (roleList && roleList.length > 0 && roleList[0] != null) {
roles = externalUser.roleList.map(role => role.role);
hasRolesCounter ++;
} else {
noRolesCounter ++
console.log("user parsing error for user" + userEmail)
}

await updateUserAttribute(userPoolId, user.Username, roles);
} catch (error) {
console.error(`Error processing user ${userEmail}:`, error);
Expand Down
2 changes: 1 addition & 1 deletion services/app-api/getUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ export const main = handler(async (event) => {
userItem.validRoutes = getUserRoleObj(userItem.roleList).getAccesses();

return userItem;
});
});
108 changes: 108 additions & 0 deletions services/app-api/getUserV2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import handler from "./libs/handler-lib";
import jwt_decode from "jwt-decode";
import {getUser} from "./getUser";

import { getUserRoleObj } from "cmscommonlib";

/**
* returns the User Table entry who's id is this email
* @param {String} userEmail User to return
* @returns {Object} the User json object
*/


const allowedRoles = [
"cmsroleapprover",
"systemadmin",
"statesystemadmin",
"helpdesk",
"defaultcmsuser",
"cmsreviewer"
];

function checkMatchingRoles(arr1, arr2) {
console.log("check matching roles called")
// Iterate through each element in array1
for (let i = 0; i<arr1.length; i++) {
// Iterate through each element in array2
for (let j = 0; j<arr2.length; j++) {
// Check if both role and territory match
if ((arr1[i]).status === "active" && (arr2[j]).status === "active" && (arr1[i]).territory === (arr2[j]).territory) {
console.log("match found")
return true;
}
}
}
console.log("no match found")
return false; // Return false if no match is found
}

function checkAdminUser(arr) {
for (let i = 0; i < arr.length; i++) {
if (allowedRoles.includes(arr[i])) {
console.log("admin user");
return true;
}
}
console.log("not an admin user");
return false;
}

// Gets owns user data from User DynamoDB table
export const main = handler(async (event) => {
console.log("get user invoked")
const body = JSON.parse(event.body);
console.log("body: ", body)
const idToken = body.idToken;
console.log("Received idToken:", idToken);
if (!idToken) {
console.log("idToken header is missing");
return {
statusCode: 400,
body: JSON.stringify({ error: "idToken event body missing" }),
};
}
const decodedIdToken = jwt_decode(idToken);
console.log("decoded id token: ", decodedIdToken);
const idTokenEmail = decodedIdToken.email;
let userRoles = decodedIdToken["custom:user_roles"];
console.log("user roles: " + userRoles)

if(!userRoles) {
console.log("no roles for user")
const userItem = (await getUser(event.queryStringParameters.email)) ?? {};
userItem.validRoutes = getUserRoleObj(userItem.roleList).getAccesses();
return userItem;
}

try {
userRoles = JSON.parse(userRoles);
} catch (error) {
console.error('Error parsing user_roles:', error);
return {
statusCode: 400,
body: JSON.stringify({ error: "no user roles for user: ", idTokenEmail}),
};
}
console.log("id token email: ", idTokenEmail);
console.log("event query email: ",event.queryStringParameters.email )
if(checkAdminUser(userRoles) || idTokenEmail === event.queryStringParameters.email) {
const userItem = (await getUser(event.queryStringParameters.email)) ?? {};
userItem.validRoutes = getUserRoleObj(userItem.roleList).getAccesses();
return userItem;
} else {
const userItem = (await getUser(event.queryStringParameters.email)) ?? {};
const loggedInUserItem = await getUser(idTokenEmail);
const hasMatchingRoles = await checkMatchingRoles(userItem.roleList, loggedInUserItem.roleList);
if(!hasMatchingRoles) {
console.log("permission denied");
return {
statusCode: 400,
body: JSON.stringify({ error: "permission denied" }),
}
} else {
userItem.validRoutes = getUserRoleObj(userItem.roleList).getAccesses();
return userItem;
}
}
});
Loading
Loading