Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin user creation method #38

Closed
wants to merge 10 commits into from
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down Expand Up @@ -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
Expand All @@ -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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you format this like the other lines (indented and link your username)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this has bjoernHeneka's username, please change that to your own.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pmill Could you please give me access to push my branch? My this merge request can be ignored

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just need to push to your own branch on your fork and the PR will be updated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks & done :)

0.2.10 (21/10/2019)

* Added tests - [franjid](https://github.com/franjid)
Expand Down
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a space before and after the .? Same for the echo line above

}
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