From 0ffaee5f3c02f8d8c2928f5b9b7e397038ad6557 Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Sat, 22 Feb 2020 01:24:30 +0530 Subject: [PATCH 01/15] Added Exception for invalidParameter & NotAuthorized --- src/Exception/InvalidParameterException.php | 7 +++++++ src/Exception/NotAuthorizedException.php | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 src/Exception/InvalidParameterException.php create mode 100644 src/Exception/NotAuthorizedException.php diff --git a/src/Exception/InvalidParameterException.php b/src/Exception/InvalidParameterException.php new file mode 100644 index 0000000..3f7fc80 --- /dev/null +++ b/src/Exception/InvalidParameterException.php @@ -0,0 +1,7 @@ + Date: Sat, 22 Feb 2020 02:52:16 +0530 Subject: [PATCH 02/15] Added InvalidParameter & NotAuthorized exceptions --- src/Exception/InvalidParameterException.php | 7 +++++++ src/Exception/NotAuthorizedException.php | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 src/Exception/InvalidParameterException.php create mode 100644 src/Exception/NotAuthorizedException.php diff --git a/src/Exception/InvalidParameterException.php b/src/Exception/InvalidParameterException.php new file mode 100644 index 0000000..3f7fc80 --- /dev/null +++ b/src/Exception/InvalidParameterException.php @@ -0,0 +1,7 @@ + Date: Sat, 22 Feb 2020 02:57:55 +0530 Subject: [PATCH 03/15] Added method to create a user by admin and set the status to CONFIRMED --- README.md | 4 ++++ src/CognitoClient.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/README.md b/README.md index ab1e162..dde54d4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,10 @@ before running them. Version History --------------- +0.2.11 (22/02/2020) + +* Added method to create a user by admin and set the status to CONFIRMED - [abhi36](https://github.com/abhi36) + 0.2.11 (06/01/2020) Added method to get a user by an access token - bjoernHeneka diff --git a/src/CognitoClient.php b/src/CognitoClient.php index ae9c435..58327fd 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -259,6 +259,48 @@ public function deleteUser($accessToken) } } + /** + * @param string $username + * @param string $password + * @param array $userAttributes + * @param boolean $confirmSignup + * @return object + * @throws Exception + */ + public function adminCreateUser($username, $password, $attributes = [], $confirmSignup = true) + { + try { + $registeredUser = $this->client->adminCreateUser([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username, + 'TemporaryPassword' => $password, + 'UserAttributes' => $attributes, + 'MessageAction' => "SUPPRESS", + 'DesiredDeliveryMediums' => ["EMAIL"] + ]); + + /** + * Auto confirm added user if confirm signup is set to true + */ + if($confirmSignup){ + $respAuthenticate = []; + try { + $respAuthenticate = $this->authenticate($username, $password); + } catch (ChallengeException $e) { + if ($e->getChallengeName() === self::CHALLENGE_NEW_PASSWORD_REQUIRED) { + $respAuthenticate = $this->respondToNewPasswordRequiredChallenge($username, $password, $e->getSession()); + } + } + return $respAuthenticate; + } + + return $registeredUser; + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + + /** * @param string $username * @throws Exception From 7ad0bd22dd2a66f0063d338e12bb22d4497ab41d Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Sat, 22 Feb 2020 03:45:41 +0530 Subject: [PATCH 04/15] Create group if the same is missing while trying to add a user to the group --- src/CognitoClient.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index d319213..1ed1fac 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -332,7 +332,18 @@ public function addUserToGroup($username, $groupName) "GroupName" => $groupName ]); } catch (CognitoIdentityProviderException $e) { - throw CognitoResponseException::createFromCognitoException($e); + if($e->getAwsErrorCode() == "ResourceNotFoundException"){ + try{ + $this->client->createGroup([ + "GroupName" => $groupName, + "UserPoolId" => $this->userPoolId + ]); + $this->addUserToGroup($username, $groupName); + } catch( AwsException $ae ){} + + }else{ + throw CognitoResponseException::createFromCognitoException($e); + } } } From e48c3a4274beaf057aec7c3ff17ec58ff947cc4d Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Thu, 27 Feb 2020 23:24:16 +0530 Subject: [PATCH 05/15] Rebasing repo From 72a169e66c22c2c190bae1a09f4d9f0901145666 Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Thu, 27 Feb 2020 23:27:34 +0530 Subject: [PATCH 06/15] Rebasing From 53f9bc5395e672a76221da6adc2aa660032206da Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Thu, 27 Feb 2020 23:34:39 +0530 Subject: [PATCH 07/15] Removing non-required exception classes --- src/Exception/InvalidParameterException.php | 7 ------- src/Exception/NotAuthorizedException.php | 7 ------- 2 files changed, 14 deletions(-) delete mode 100644 src/Exception/InvalidParameterException.php delete mode 100644 src/Exception/NotAuthorizedException.php diff --git a/src/Exception/InvalidParameterException.php b/src/Exception/InvalidParameterException.php deleted file mode 100644 index 3f7fc80..0000000 --- a/src/Exception/InvalidParameterException.php +++ /dev/null @@ -1,7 +0,0 @@ - Date: Thu, 27 Feb 2020 23:58:47 +0530 Subject: [PATCH 08/15] Separated user cofirmation from user creation --- src/CognitoClient.php | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index 1ed1fac..04bdd4e 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -267,7 +267,7 @@ public function deleteUser($accessToken) * @return object * @throws Exception */ - public function adminCreateUser($username, $password, $attributes = [], $confirmSignup = true) + public function adminCreateUser($username, $password, $attributes = []) { try { $registeredUser = $this->client->adminCreateUser([ @@ -279,21 +279,6 @@ public function adminCreateUser($username, $password, $attributes = [], $confirm 'DesiredDeliveryMediums' => ["EMAIL"] ]); - /** - * Auto confirm added user if confirm signup is set to true - */ - if($confirmSignup){ - $respAuthenticate = []; - try { - $respAuthenticate = $this->authenticate($username, $password); - } catch (ChallengeException $e) { - if ($e->getChallengeName() === self::CHALLENGE_NEW_PASSWORD_REQUIRED) { - $respAuthenticate = $this->respondToNewPasswordRequiredChallenge($username, $password, $e->getSession()); - } - } - return $respAuthenticate; - } - return $registeredUser; } catch (CognitoIdentityProviderException $e) { throw CognitoResponseException::createFromCognitoException($e); @@ -301,6 +286,26 @@ public function adminCreateUser($username, $password, $attributes = [], $confirm } + /** + * Set a admin added user as CONFIRMED. It sets the parameter password as final + * @param string $username + * @param string $password + * @return object + * @throws Exception + */ + public function adminCofirmAddedUser($username, $password){ + $respAuthenticate = []; + try { + $respAuthenticate = $this->authenticate($username, $password); + } catch (ChallengeException $e) { + if ($e->getChallengeName() === self::CHALLENGE_NEW_PASSWORD_REQUIRED) { + $respAuthenticate = $this->respondToNewPasswordRequiredChallenge($username, $password, $e->getSession()); + } + } + return $respAuthenticate; + } + + /** * @param string $username * @throws Exception From f0ccae2e7cc154b9b5f114179cdc72225288be23 Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Fri, 28 Feb 2020 03:57:32 +0530 Subject: [PATCH 09/15] Added admin method to create user group --- README.md | 8 ++++---- src/CognitoClient.php | 34 ++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index dde54d4..4fbd1d6 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,13 @@ before running them. Version History --------------- -0.2.11 (22/02/2020) +0.2.11 (28/02/2020) -* Added method to create a user by admin and set the status to CONFIRMED - [abhi36](https://github.com/abhi36) +* Added method to create a user group - [abhi36](https://github.com/abhi36) -0.2.11 (06/01/2020) +0.2.11 (22/02/2020) -Added method to get a user by an access token - bjoernHeneka +* Added method to create a user by admin and set the status to CONFIRMED - [abhi36](https://github.com/abhi36) 0.2.10 (21/10/2019) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index 04bdd4e..bbca887 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -235,7 +235,7 @@ public function getUser($username) 'UserPoolId' => $this->userPoolId, ]); return $response; - } catch (Exception $e) { + } catch (CognitoIdentityProviderException $e) { throw CognitoResponseException::createFromCognitoException($e); } } @@ -293,7 +293,7 @@ public function adminCreateUser($username, $password, $attributes = []) * @return object * @throws Exception */ - public function adminCofirmAddedUser($username, $password){ + public function adminConfirmAddedUser($username, $password){ $respAuthenticate = []; try { $respAuthenticate = $this->authenticate($username, $password); @@ -337,18 +337,24 @@ public function addUserToGroup($username, $groupName) "GroupName" => $groupName ]); } catch (CognitoIdentityProviderException $e) { - if($e->getAwsErrorCode() == "ResourceNotFoundException"){ - try{ - $this->client->createGroup([ - "GroupName" => $groupName, - "UserPoolId" => $this->userPoolId - ]); - $this->addUserToGroup($username, $groupName); - } catch( AwsException $ae ){} - - }else{ - throw CognitoResponseException::createFromCognitoException($e); - } + throw CognitoResponseException::createFromCognitoException($e); + } + } + + /** + * Create a group for users + * @param string $username + * @param string $groupName + * @throws Exception + */ + public function createUserGroup($groupName){ + try{ + $this->client->createGroup([ + "GroupName" => $groupName, + "UserPoolId" => $this->userPoolId + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); } } From 4fd8a2532bb2c5fd136984d8a494e9169ba25e43 Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Fri, 28 Feb 2020 15:04:58 +0530 Subject: [PATCH 10/15] Updated user attributes acceptance format for user creation --- src/CognitoClient.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index bbca887..f9e67b9 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -259,6 +259,37 @@ public function deleteUser($accessToken) } } + + /** + * Disable a user + * @param string $username + */ + public function adminEnableUser($username){ + try { + $this->client->adminEnableUser([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + + /** + * Disable a user + * @param string $username + */ + public function adminDisableUser($username){ + try { + $this->client->adminDisableUser([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + /** * @param string $username * @param string $password @@ -269,12 +300,13 @@ public function deleteUser($accessToken) */ public function adminCreateUser($username, $password, $attributes = []) { + $userAttributes = $this->buildAttributesArray($attributes); try { $registeredUser = $this->client->adminCreateUser([ 'UserPoolId' => $this->userPoolId, 'Username' => $username, 'TemporaryPassword' => $password, - 'UserAttributes' => $attributes, + 'UserAttributes' => $userAttributes, 'MessageAction' => "SUPPRESS", 'DesiredDeliveryMediums' => ["EMAIL"] ]); From 89b72333ac78a49bd86f61002f31bb1303e6f954 Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Fri, 28 Feb 2020 19:57:55 +0530 Subject: [PATCH 11/15] Making requested changes from fixes #39 --- README.md | 1 + src/CognitoClient.php | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4fbd1d6..b23cf9d 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Version History * Added method to create a user by admin and set the status to CONFIRMED - [abhi36](https://github.com/abhi36) + 0.2.10 (21/10/2019) * Added tests - [franjid](https://github.com/franjid) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index f9e67b9..ab4d2b5 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -264,9 +264,10 @@ public function deleteUser($accessToken) * Disable a user * @param string $username */ - public function adminEnableUser($username){ + public function adminEnableUser($username) + { try { - $this->client->adminEnableUser([ + return $this->client->adminEnableUser([ 'UserPoolId' => $this->userPoolId, 'Username' => $username ]); @@ -279,9 +280,10 @@ public function adminEnableUser($username){ * Disable a user * @param string $username */ - public function adminDisableUser($username){ + public function adminDisableUser($username) + { try { - $this->client->adminDisableUser([ + return $this->client->adminDisableUser([ 'UserPoolId' => $this->userPoolId, 'Username' => $username ]); @@ -325,7 +327,8 @@ public function adminCreateUser($username, $password, $attributes = []) * @return object * @throws Exception */ - public function adminConfirmAddedUser($username, $password){ + public function adminConfirmAddedUser($username, $password) + { $respAuthenticate = []; try { $respAuthenticate = $this->authenticate($username, $password); @@ -379,7 +382,8 @@ public function addUserToGroup($username, $groupName) * @param string $groupName * @throws Exception */ - public function createUserGroup($groupName){ + public function createGroup($groupName) + { try{ $this->client->createGroup([ "GroupName" => $groupName, From 2e27bac66f03e3cbb5a2147bf6c1fcde6af3e789 Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Thu, 5 Mar 2020 04:01:46 +0530 Subject: [PATCH 12/15] Removed admin confirmation method for admin added users --- src/CognitoClient.php | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index ab4d2b5..09c4877 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -319,28 +319,6 @@ public function adminCreateUser($username, $password, $attributes = []) } } - - /** - * Set a admin added user as CONFIRMED. It sets the parameter password as final - * @param string $username - * @param string $password - * @return object - * @throws Exception - */ - public function adminConfirmAddedUser($username, $password) - { - $respAuthenticate = []; - try { - $respAuthenticate = $this->authenticate($username, $password); - } catch (ChallengeException $e) { - if ($e->getChallengeName() === self::CHALLENGE_NEW_PASSWORD_REQUIRED) { - $respAuthenticate = $this->respondToNewPasswordRequiredChallenge($username, $password, $e->getSession()); - } - } - return $respAuthenticate; - } - - /** * @param string $username * @throws Exception From 6274d88ac6f99385d311a8301d8fb1f1e6c93c63 Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Sat, 7 Mar 2020 23:54:14 +0530 Subject: [PATCH 13/15] Fixing code formatting --- src/CognitoClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index 09c4877..43ac844 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -362,8 +362,8 @@ public function addUserToGroup($username, $groupName) */ public function createGroup($groupName) { - try{ - $this->client->createGroup([ + try { + return $this->client->createGroup([ "GroupName" => $groupName, "UserPoolId" => $this->userPoolId ]); From 4856d00c4bdbc3945ede22faf0092713729ccfff Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Wed, 18 Mar 2020 21:08:54 +0530 Subject: [PATCH 14/15] Adding method to chage a user password --- README.md | 9 --------- src/CognitoClient.php | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b23cf9d..53d4a9c 100644 --- a/README.md +++ b/README.md @@ -43,15 +43,6 @@ before running them. Version History --------------- -0.2.11 (28/02/2020) - -* Added method to create a user group - [abhi36](https://github.com/abhi36) - -0.2.11 (22/02/2020) - -* Added method to create a user by admin and set the status to CONFIRMED - [abhi36](https://github.com/abhi36) - - 0.2.10 (21/10/2019) * Added tests - [franjid](https://github.com/franjid) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index 43ac844..8ef2395 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -292,6 +292,30 @@ public function adminDisableUser($username) } } + + /** + * @param string $username + * @param string $password + * @param boolean $permanent, decide whether to set this password as parmanent + * + * @return empty body + * @throws Exception + */ + public function adminSetUserPassword($username, $password, $permanent = 0) + { + $permanent = filter_var($permanent, FILTER_VALIDATE_BOOLEAN); + try { + return $this->client->AdminSetUserPassword([ + "Password" => $password, + "Permanent" => $permanent, + "Username" => $username, + "UserPoolId" => $this->userPoolId + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + /** * @param string $username * @param string $password From 11eccee84b3f253b68a5d1bccbba9a1bf888e13d Mon Sep 17 00:00:00 2001 From: Abhijeet K Date: Fri, 3 Apr 2020 14:14:41 +0530 Subject: [PATCH 15/15] Method to remove a user from a group --- src/CognitoClient.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index 8ef2395..8823257 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -360,6 +360,24 @@ public function adminDeleteUser($username) } } + /** + * @param string $username + * @param string $groupName + * @throws Exception + */ + public function removeUserFromGroup($username, $groupName) + { + try { + $this->client->adminRemoveUserFromGroup([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username, + "GroupName" => $groupName + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + /** * @param string $username * @param string $groupName