Skip to content

Commit

Permalink
Added getUser method
Browse files Browse the repository at this point in the history
  • Loading branch information
goraxan committed Mar 12, 2023
1 parent ecd14d5 commit 5288f7c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "levinriegner/craft-cognito-auth",
"description": "Enable authentication to Craft using AWS Cognito",
"type": "craft-plugin",
"version": "0.8.3",
"version": "0.9.0",
"keywords": [
"craft",
"cms",
Expand Down
25 changes: 18 additions & 7 deletions src/services/AWSCognitoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace levinriegner\craftcognitoauth\services;

use Craft;
use craft\base\Component;
use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
use Aws\CognitoIdentityProvider\Exception\CognitoIdentityProviderException;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Plain;
use levinriegner\craftcognitoauth\CraftJwtAuth;

class AWSCognitoService extends Component
Expand Down Expand Up @@ -332,16 +330,29 @@ public function resetPassword(string $code, string $password, string $email) : s
return '';
}

public function getEmail(?Token $token){
public function getUser($username) {
try {
return $this->client->adminGetUser([
'Username' => $username,
'UserPoolId' => $this->userpool_id
]);
} catch (\Exception $e) {
return $e->getMessage();
}

return null;
}

public function getEmail(?Plain $token){
if(!$token) return '';

return $token->getClaim('email','');
return $token->claims()->get('email','');
}

public function isAdmin(?Token $token){
public function isAdmin(?Plain $token){
if(!$token) return false;

$groups = $token->getClaim('cognito:groups',[]);
$groups = $token->claims()->get('cognito:groups',[]);
if($groups && in_array('admin', $groups)){
return true;
}
Expand Down

0 comments on commit 5288f7c

Please sign in to comment.