diff --git a/README.md b/README.md index 9bd89e2..ab1e162 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ pmill/aws-cognito ================= ![Downloads](https://poser.pugx.org/pmill/aws-cognito/downloads) +[![Build Status](https://travis-ci.com/pmill/aws-cognito.svg?branch=master)](https://travis-ci.com/pmill/aws-cognito) Introduction ------------ @@ -29,7 +30,7 @@ curl -sS https://getcomposer.org/installer | php Next, run the Composer command to install the latest version: ```bash -composer.phar require pmill/aws-cognito +php composer.phar require pmill/aws-cognito ``` Usage @@ -42,6 +43,10 @@ before running them. Version History --------------- +0.2.11 (06/01/2020) + +Added method to get a user by an access token - bjoernHeneka + 0.2.10 (21/10/2019) * Added tests - [franjid](https://github.com/franjid) diff --git a/examples/getUserByToken.php b/examples/getUserByToken.php new file mode 100644 index 0000000..f8b7b49 --- /dev/null +++ b/examples/getUserByToken.php @@ -0,0 +1,18 @@ +authenticate($username, $password); +$accessToken = $authenticationResponse['AccessToken']; + +try { + $user = $client->getUserByToken($accessToken); + echo $user['Username'].PHP_EOL; + var_dump($user['UserAttributes']); +} catch (Exception $e) { + echo "An error occurred: ".$e->getMessage(); +} diff --git a/src/CognitoClient.php b/src/CognitoClient.php index c94c752..aa59cbd 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -1,4 +1,5 @@ client->getUser([ + 'AccessToken' => $accessToken, + ]); + return $response; + } catch (Exception $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + /* * @param string $username * @return AwsResult @@ -261,7 +281,8 @@ public function adminDeleteUser($username) * @param string $groupName * @throws Exception */ - public function addUserToGroup($username, $groupName) { + public function addUserToGroup($username, $groupName) + { try { $this->client->adminAddUserToGroup([ 'UserPoolId' => $this->userPoolId, @@ -480,10 +501,10 @@ public function decodeAccessToken($accessToken) * * @param string $accessToken * - * @throws TokenExpiryException + * @return string * @throws TokenVerificationException * - * @return string + * @throws TokenExpiryException */ public function verifyAccessToken($accessToken) { @@ -494,7 +515,7 @@ public function verifyAccessToken($accessToken) throw new TokenVerificationException('invalid iss'); } - if ($jwtPayload['token_use'] !== 'access') { + if ( !in_array($jwtPayload['token_use'], ['id','access']) ) { throw new TokenVerificationException('invalid token_use'); } @@ -502,7 +523,7 @@ public function verifyAccessToken($accessToken) throw new TokenExpiryException('invalid exp'); } - return $jwtPayload['username']; + return $jwtPayload['username'] ?? $jwtPayload['cognito:username']; } /** @@ -526,7 +547,7 @@ public function getGroupsForUsername($username) try { return $this->client->adminListGroupsForUser([ 'UserPoolId' => $this->userPoolId, - 'Username' => $username + 'Username' => $username ]); } catch (Exception $e) { throw CognitoResponseException::createFromCognitoException($e);