Skip to content

Commit

Permalink
Merge pull request #36 from bjoernHeneka/getUserByToken
Browse files Browse the repository at this point in the history
Adds method to retrieve User by an accessToken
  • Loading branch information
pmill authored Jan 6, 2020
2 parents 60e4f95 + ff177fa commit a3586da
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
18 changes: 18 additions & 0 deletions examples/getUserByToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/** @var \pmill\AwsCognito\CognitoClient $client */
$client = require(__DIR__ . '/bootstrap.php');

$username = '[email protected]';
$password = 'S3cr3T';

$authenticationResponse = $client->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();
}
29 changes: 25 additions & 4 deletions src/CognitoClient.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace pmill\AwsCognito;

use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
Expand Down Expand Up @@ -201,6 +202,25 @@ public function confirmUserRegistration($confirmationCode, $username)
}
}

/**
* @param string $accessToken
* @throws Exception
* @throws TokenExpiryException
* @throws TokenVerificationException
* @return AwsResult
*/
public function getUserByToken($accessToken)
{
try {
$response = $this->client->getUser([
'AccessToken' => $accessToken,
]);
return $response;
} catch (Exception $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/*
* @param string $username
* @return AwsResult
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a3586da

Please sign in to comment.