-
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. Expands the server's AWS IAM policy to allow access to a few required Cognito user pool API actions. The policy files are .tftpl.json instead of .json.tftpl to indicate that although templated they still remain valid JSON (at least for the time being).
- Loading branch information
Showing
13 changed files
with
357 additions
and
1 deletion.
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
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 @@ | ||
../variable-COGNITO_USER_POOL_ID.tf |
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,4 @@ | ||
variable "COGNITO_USER_POOL_ID" { | ||
type = string | ||
description = "Id of the Cognito user pool for this environment" | ||
} |
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 |
---|---|---|
|
@@ -46,4 +46,6 @@ module "iam" { | |
} | ||
|
||
env = "testing" | ||
|
||
COGNITO_USER_POOL_ID = module.cognito.COGNITO_USER_POOL_ID | ||
} |
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 { COGNITO_USER_POOL_ID } from "./config.js"; | ||
import { NotFound } from "./httpErrors.js"; | ||
|
||
|
||
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
Oops, something went wrong.