UNPKG

@gravitywelluk/aws

Version:

Library of commonly used AWS wrapper functions to communicate with the AWS SDK

94 lines (93 loc) 4.56 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateCognitoUserGroup = void 0; const Joi = __importStar(require("joi")); const R = __importStar(require("ramda")); const validation_utils_1 = require("@gravitywelluk/validation-utils"); const cognito_configure_1 = require("./cognito-configure"); const aws_error_1 = require("../utils/aws-error"); const utils_1 = require("./utils"); /** * Updates the given Cognito user's groups, equivalent to a PUT request * * @param updateUserParams - The parameters required to create a Cognito user * @param awsCognitoConfigOverrides - Configuration option overrides */ const updateCognitoUserGroup = async (updateUserParams, awsCognitoConfigOverrides = {}) => { const cognito = (0, cognito_configure_1.cognitoConfigure)(awsCognitoConfigOverrides); const requestedGroups = updateUserParams.groups || []; const { error } = Joi.object({ userPoolId: Joi.string().required(), cognitoId: Joi.string().required(), groups: Joi.array().items(Joi.string().required()).required() }).validate(updateUserParams); // Error if there any Joi validation errors if (error) { throw new validation_utils_1.JoiError(error); } // Get all of the Cognito groups for the given user pool const allCognitoGroups = await (0, utils_1.cognitoListGroups)(cognito, { UserPoolId: updateUserParams.userPoolId }); // Validate that the given updateUserParams.groups match the allCognitoGroups const { error: joiCognitoGroupsError } = Joi.array().items(Joi.string().valid(...allCognitoGroups).required()).validate(updateUserParams.groups); // Error if there any Joi validation errors regarding the given groups now // we have sight of the groups that can be chosen (allCognitoGroups) if (joiCognitoGroupsError) { throw new validation_utils_1.JoiError(joiCognitoGroupsError); } try { // Get the user's assigned groups const cognitoUserGroupList = await cognito.adminListGroupsForUser({ UserPoolId: updateUserParams.userPoolId, Username: updateUserParams.cognitoId }).promise(); // Collate the group names const currentCognitoGroups = cognitoUserGroupList.Groups ? cognitoUserGroupList.Groups.map(group => group.GroupName).filter(groupName => typeof groupName === "string") : []; const toAdd = R.uniq(R.filter(groupName => !R.includes(groupName, currentCognitoGroups), requestedGroups)); const toRemove = R.uniq(R.filter(groupName => !R.includes(groupName, requestedGroups), currentCognitoGroups)); // Removed the user from the toRemove groups for (const GroupName of toRemove) { await cognito.adminRemoveUserFromGroup({ GroupName, Username: updateUserParams.cognitoId, UserPoolId: updateUserParams.userPoolId }).promise(); } // Add the user to the toAdd groups for (const GroupName of toAdd) { await cognito.adminAddUserToGroup({ GroupName, Username: updateUserParams.cognitoId, UserPoolId: updateUserParams.userPoolId }).promise(); } // Get and return the final user groups const { Groups: finalCognitoUserGroupList } = await cognito.adminListGroupsForUser({ UserPoolId: updateUserParams.userPoolId, Username: updateUserParams.cognitoId }).promise(); return finalCognitoUserGroupList; } catch (error) { throw new aws_error_1.AwsError(error); } }; exports.updateCognitoUserGroup = updateCognitoUserGroup;