Skip to content

Commit

Permalink
Mapped additional errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pmill committed Jan 27, 2018
1 parent 184b37a commit 35acc5d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 68 deletions.
175 changes: 108 additions & 67 deletions src/CognitoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Jose\Component\Signature\JWSVerifier;
use Jose\Component\Signature\Serializer\CompactSerializer;
use pmill\AwsCognito\Exception\ChallengeException;
use pmill\AwsCognito\Exception\CognitoResponseException;
use pmill\AwsCognito\Exception\TokenExpiryException;
use pmill\AwsCognito\Exception\TokenVerificationException;

Expand Down Expand Up @@ -82,13 +83,7 @@ public function authenticate($username, $password)

return $this->handleAuthenticateResponse($response->toArray());
} catch (CognitoIdentityProviderException $e) {
$errorClass = "pmill\\AwsCognito\\Exception\\" . $e->getAwsErrorCode();

if (class_exists($errorClass)) {
throw new $errorClass($e);
} else {
throw $e;
}
throw CognitoResponseException::createFromCognitoException($e);
}
}

Expand All @@ -103,14 +98,18 @@ public function authenticate($username, $password)
*/
public function respondToAuthChallenge($challengeName, array $challengeResponses, $session)
{
$response = $this->client->respondToAuthChallenge([
'ChallengeName' => $challengeName,
'ChallengeResponses' => $challengeResponses,
'ClientId' => $this->appClientId,
'Session' => $session,
]);
try {
$response = $this->client->respondToAuthChallenge([
'ChallengeName' => $challengeName,
'ChallengeResponses' => $challengeResponses,
'ClientId' => $this->appClientId,
'Session' => $session,
]);

return $this->handleAuthenticateResponse($response->toArray());
return $this->handleAuthenticateResponse($response->toArray());
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
Expand All @@ -137,65 +136,88 @@ public function respondToNewPasswordRequiredChallenge($username, $newPassword, $
/**
* @param string $username
* @param string $refreshToken
*
* @return array
* @return string
* @throws Exception
*/
public function refreshAuthentication($username, $refreshToken)
{
$response = $this->client->adminInitiateAuth([
'AuthFlow' => 'REFRESH_TOKEN_AUTH',
'AuthParameters' => [
'USERNAME' => $username,
'REFRESH_TOKEN' => $refreshToken,
'SECRET_HASH' => $this->cognitoSecretHash($username),
],
'ClientId' => $this->appClientId,
'UserPoolId' => $this->userPoolId,
])->toArray();
try {
$response = $this->client->adminInitiateAuth([
'AuthFlow' => 'REFRESH_TOKEN_AUTH',
'AuthParameters' => [
'USERNAME' => $username,
'REFRESH_TOKEN' => $refreshToken,
'SECRET_HASH' => $this->cognitoSecretHash($username),
],
'ClientId' => $this->appClientId,
'UserPoolId' => $this->userPoolId,
])->toArray();

return $response['AuthenticationResult'];
return $response['AuthenticationResult'];
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
* @param string $accessToken
* @param string $previousPassword
* @param string $proposedPassword
* @throws Exception
* @throws TokenExpiryException
* @throws TokenVerificationException
*/
public function changePassword($accessToken, $previousPassword, $proposedPassword)
{
$this->verifyAccessToken($accessToken);

$this->client->changePassword([
'AccessToken' => $accessToken,
'PreviousPassword' => $previousPassword,
'ProposedPassword' => $proposedPassword,
]);
try {
$this->client->changePassword([
'AccessToken' => $accessToken,
'PreviousPassword' => $previousPassword,
'ProposedPassword' => $proposedPassword,
]);
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
* @param string $confirmationCode
* @param string $username
* @throws Exception
*/
public function confirmUserRegistration($confirmationCode, $username)
{
$this->client->confirmSignUp([
'ClientId' => $this->appClientId,
'ConfirmationCode' => $confirmationCode,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
try {
$this->client->confirmSignUp([
'ClientId' => $this->appClientId,
'ConfirmationCode' => $confirmationCode,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
* @param string $accessToken
* @throws Exception
* @throws TokenExpiryException
* @throws TokenVerificationException
*/
public function deleteUser($accessToken)
{
$this->verifyAccessToken($accessToken);

$this->client->deleteUser([
'AccessToken' => $accessToken,
]);
try {
$this->client->deleteUser([
'AccessToken' => $accessToken,
]);
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
Expand Down Expand Up @@ -237,8 +259,8 @@ protected function downloadJwtWebKeys()
* @param string $username
* @param string $password
* @param array $attributes
*
* @return string
* @throws Exception
*/
public function registerUser($username, $password, array $attributes = [])
{
Expand All @@ -250,55 +272,74 @@ public function registerUser($username, $password, array $attributes = [])
];
}

$response = $this->client->signUp([
'ClientId' => $this->appClientId,
'Password' => $password,
'SecretHash' => $this->cognitoSecretHash($username),
'UserAttributes' => $userAttributes,
'Username' => $username,
]);
try {
$response = $this->client->signUp([
'ClientId' => $this->appClientId,
'Password' => $password,
'SecretHash' => $this->cognitoSecretHash($username),
'UserAttributes' => $userAttributes,
'Username' => $username,
]);

return $response['UserSub'];
return $response['UserSub'];
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
* @param string $confirmationCode
* @param string $username
* @param string $proposedPassword
* @throws Exception
*/
public function resetPassword($confirmationCode, $username, $proposedPassword)
{
$this->client->confirmForgotPassword([
'ClientId' => $this->appClientId,
'ConfirmationCode' => $confirmationCode,
'Password' => $proposedPassword,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
try {
$this->client->confirmForgotPassword([
'ClientId' => $this->appClientId,
'ConfirmationCode' => $confirmationCode,
'Password' => $proposedPassword,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
* @param string $username
* @throws Exception
*/
public function resendRegistrationConfirmationCode($username)
{
$this->client->resendConfirmationCode([
'ClientId' => $this->appClientId,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
try {
$this->client->resendConfirmationCode([
'ClientId' => $this->appClientId,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
* @param string $username
* @throws Exception
*/
public function sendForgottenPasswordRequest($username)
{
$this->client->forgotPassword([
'ClientId' => $this->appClientId,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
try {
$this->client->forgotPassword([
'ClientId' => $this->appClientId,
'SecretHash' => $this->cognitoSecretHash($username),
'Username' => $username,
]);
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}

/**
Expand Down
19 changes: 18 additions & 1 deletion src/Exception/CognitoResponseException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace pmill\AwsCognito\Exception;

use Aws\CognitoIdentityProvider\Exception\CognitoIdentityProviderException;
use Exception;
use Throwable;

class CognitoResponseException extends \Exception
class CognitoResponseException extends Exception
{
/**
* CognitoResponseException constructor.
Expand All @@ -13,4 +15,19 @@ public function __construct(Throwable $previous = null)
{
parent::__construct(get_class(), 0, $previous);
}

/**
* @param CognitoIdentityProviderException $e
* @return Exception
*/
public static function createFromCognitoException(CognitoIdentityProviderException $e)
{
$errorClass = "pmill\\AwsCognito\\Exception\\" . $e->getAwsErrorCode();

if (class_exists($errorClass)) {
return new $errorClass($e);
}

return $e;
}
}

0 comments on commit 35acc5d

Please sign in to comment.