-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RESTful API endpoints for managing Groups members
Read-write access for group owners, read-only access for all other members of a group, and no access for non-members.
- Loading branch information
Showing
6 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Cognito user pool (IdP) management. | ||
* | ||
* @module cognito | ||
*/ | ||
/* eslint-disable no-await-in-loop */ | ||
import { | ||
CognitoIdentityProviderClient, | ||
AdminAddUserToGroupCommand, | ||
AdminRemoveUserFromGroupCommand, | ||
paginateListUsersInGroup, | ||
UserNotFoundException, | ||
} from "@aws-sdk/client-cognito-identity-provider"; | ||
|
||
import { NotFound } from "./httpErrors.js"; | ||
|
||
|
||
const COGNITO_USER_POOL_ID = "us-east-1_Cg5rcTged"; | ||
const REGION = COGNITO_USER_POOL_ID.split("_")[0]; | ||
|
||
const cognito = new CognitoIdentityProviderClient({ region: REGION }); | ||
|
||
|
||
/** | ||
* Retrieve AWS Cognito users in a Cognito group. | ||
* | ||
* @param {string} name - Name of the AWS Cognito group | ||
* @yields {object} user, see <https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cognito-identity-provider/interfaces/usertype.html> | ||
*/ | ||
export async function* listUsersInGroup(name) { | ||
const paginator = paginateListUsersInGroup({client: cognito}, { | ||
UserPoolId: COGNITO_USER_POOL_ID, | ||
GroupName: name, | ||
}); | ||
|
||
for await (const page of paginator) { | ||
yield* page.Users; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Add an AWS Cognito user to a Cognito group. | ||
* | ||
* @param {string} username | ||
* @param {string} group - Name of the AWS Cognito group | ||
* @throws {NotFound} if username is unknown | ||
*/ | ||
export async function addUserToGroup(username, group) { | ||
try { | ||
await cognito.send(new AdminAddUserToGroupCommand({ | ||
UserPoolId: COGNITO_USER_POOL_ID, | ||
Username: username, | ||
GroupName: group, | ||
})); | ||
} catch (err) { | ||
if (err instanceof UserNotFoundException) { | ||
throw new NotFound(`unknown user: ${username}`); | ||
} | ||
throw err; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Remove an AWS Cognito user from a Cognito group. | ||
* | ||
* @param {string} username | ||
* @param {string} group - Name of the AWS Cognito group | ||
* @throws {NotFound} if username is unknown | ||
*/ | ||
export async function removeUserFromGroup(username, group) { | ||
try { | ||
await cognito.send(new AdminRemoveUserFromGroupCommand({ | ||
UserPoolId: COGNITO_USER_POOL_ID, | ||
Username: username, | ||
GroupName: group, | ||
})); | ||
} catch (err) { | ||
if (err instanceof UserNotFoundException) { | ||
throw new NotFound(`unknown user: ${username}`); | ||
} | ||
throw err; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Iterator utilities. | ||
* | ||
* @module utils.iterators | ||
*/ | ||
|
||
|
||
/** | ||
* Maps a function over any iterable, including async ones. | ||
* | ||
* If the iterable has a "map" property, it's called directly. | ||
* | ||
* @param {Object} it - [Iterable object]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol}, e.g. an array, Set, Map, generator, async generator, etc. | ||
* @param {Function} fn - Synchronous mapping function taking an element and returning a mapped value. | ||
* @returns {Array} | ||
*/ | ||
export async function map(it, fn) { | ||
if (it.map) return it.map(x => fn(x)); | ||
|
||
const array = []; | ||
for await (const x of it) { | ||
/* Apply fn() as we go to avoid keeping all of the untransformed and | ||
* potentially large elements in memory at once. | ||
*/ | ||
array.push(fn(x)); | ||
} | ||
return array; | ||
} | ||
|
||
|
||
/** | ||
* Consume an potentially-async iterable into an array. | ||
* | ||
* @param {Object} it - [Iterable object]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol}, e.g. an array, Set, Map, generator, async generator, etc. | ||
* @returns {Array} | ||
*/ | ||
export async function slurp(it) { | ||
return await map(it, x => x); | ||
} |