From 85df8b36b487c3a41cb318321ab954d5618b216d Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Fri, 12 Jul 2024 18:57:30 +1000 Subject: [PATCH] Add Amazon Cognito provider --- .../amazoncognito/provider/AmazonCognito.php | 221 ++++++++++++++++ .../provider/AmazonCognitoUser.php | 242 ++++++++++++++++++ src/helpers/Provider.php | 3 + src/providers/AmazonCognito.php | 22 ++ 4 files changed, 488 insertions(+) create mode 100644 src/clients/amazoncognito/provider/AmazonCognito.php create mode 100644 src/clients/amazoncognito/provider/AmazonCognitoUser.php create mode 100644 src/providers/AmazonCognito.php diff --git a/src/clients/amazoncognito/provider/AmazonCognito.php b/src/clients/amazoncognito/provider/AmazonCognito.php new file mode 100644 index 0000000..3bd415e --- /dev/null +++ b/src/clients/amazoncognito/provider/AmazonCognito.php @@ -0,0 +1,221 @@ +hostedDomain = $options['hostedDomain']; + } elseif (!empty($options['cognitoDomain']) && !empty($options['region'])) { + $this->cognitoDomain = $options['cognitoDomain']; + $this->region = $options['region']; + } else { + throw new \InvalidArgumentException( + 'Neither "cognitoDomain" and "region" nor "hostedDomain" options are set. Please set one of them.' + ); + } + + if (!empty($options['scope'])) { + $this->scopes = explode($this->getScopeSeparator(), $options['scope']); + } + } + + /** + * @return array + */ + public function getScopes() + { + return $this->scopes; + } + + /** + * @return mixed + */ + public function getRegion() + { + return $this->region; + } + + /** + * @param $region + */ + public function setRegion($region) + { + $this->region = $region; + } + + /** + * @return string + */ + public function getHostedDomain() + { + return $this->hostedDomain; + } + + /** + * @param string $hostedDomain + */ + public function setHostedDomain($hostedDomain) + { + $this->hostedDomain = $hostedDomain; + } + + /** + * @return string + */ + public function getCognitoDomain() + { + return $this->cognitoDomain; + } + + /** + * @param string $cognitoDomain + */ + public function setCognitoDomain($cognitoDomain) + { + $this->cognitoDomain = $cognitoDomain; + } + + /** + * Returns the url for given action + * + * @param $action + * @return string + */ + private function getCognitoUrl($action) + { + return !empty($this->hostedDomain) ? $this->hostedDomain . $action : + sprintf(self::BASE_COGNITO_URL, $this->cognitoDomain, $this->region, $action); + } + + /** + * @return string + */ + public function getBaseAuthorizationUrl() + { + return $this->getCognitoUrl('/authorize'); + } + + /** + * @param array $params + * @return string + */ + public function getBaseAccessTokenUrl(array $params) + { + return $this->getCognitoUrl('/token'); + } + + /** + * @param AccessToken $token + * @return string + */ + public function getResourceOwnerDetailsUrl(AccessToken $token) + { + return $this->getCognitoUrl('/oauth2/userInfo'); + } + + /** + * @param array $options + * @return array + */ + protected function getAuthorizationParameters(array $options) + { + $scopes = array_merge($this->getDefaultScopes(), $this->scopes); + + if (!empty($options['scope'])) { + $scopes = array_merge($scopes, $options['scope']); + } + + $options['scope'] = array_unique($scopes); + + return parent::getAuthorizationParameters($options); + } + + /** + * @return array + */ + protected function getDefaultScopes() + { + return ['openid', 'email']; + } + + /** + * @return string + */ + protected function getScopeSeparator() + { + return ' '; + } + + /** + * @param ResponseInterface $response + * @param array|string $data + * @throws IdentityProviderException + */ + protected function checkResponse(ResponseInterface $response, $data) + { + if (empty($data['error'])) { + return; + } + + $code = 0; + $error = $data['error']; + + throw new IdentityProviderException($error, $code, $data); + } + + /** + * @param array $response + * @param AccessToken $token + * @return CognitoUser|\League\OAuth2\Client\Provider\ResourceOwnerInterface + */ + protected function createResourceOwner(array $response, AccessToken $token) + { + $user = new CognitoUser($response); + + return $user; + } +} \ No newline at end of file diff --git a/src/clients/amazoncognito/provider/AmazonCognitoUser.php b/src/clients/amazoncognito/provider/AmazonCognitoUser.php new file mode 100644 index 0000000..f5cb5b2 --- /dev/null +++ b/src/clients/amazoncognito/provider/AmazonCognitoUser.php @@ -0,0 +1,242 @@ +data = $response; + } + + /** + * Get id + * + * @return string + */ + public function getId() + { + return $this->getField('sub'); + } + + /** + * Get address. + * + * @return string|null + */ + public function getAddress() + { + return $this->getField('address'); + } + + /** + * Get username. + * + * @return string|null + */ + public function getUsername() + { + return $this->getField('username'); + } + + /** + * Get email address. + * + * @return string|null + */ + public function getEmail() + { + return $this->getField('email'); + } + + /** + * Get email verified. + * + * @return string|null + */ + public function getEmailVerified() + { + return $this->getField('email_verified'); + } + + /** + * Get phone number. + * + * @return string|null + */ + public function getPhoneNumber() + { + return $this->getField('phone_number'); + } + + /** + * Get phone number verified. + * + * @return string|null + */ + public function getPhoneNumberVerified() + { + return $this->getField('phone_number_verified'); + } + + /** + * Get birthdate. + * + * @return string|null + */ + public function getBirthdate() + { + return $this->getField('birthdate'); + } + + /** + * Get profile. + * + * @return string|null + */ + public function getProfile() + { + return $this->getField('profile'); + } + + /** + * Get gender. + * + * @return string|null + */ + public function getGender() + { + return $this->getField('gender'); + } + + /** + * Get name. + * + * @return string|null + */ + public function getName() + { + return $this->getField('name'); + } + + /** + * Get given name. + * + * @return string|null + */ + public function getGivenName() + { + return $this->getField('given_name'); + } + + /** + * Get middle name. + * + * @return string|null + */ + public function getMiddleName() + { + return $this->getField('middle_name'); + } + + /** + * Get family name. + * + * @return string|null + */ + public function getFamilyName() + { + return $this->getField('family_name'); + } + + /** + * Get locale. + * + * @return string|null + */ + public function getLocale() + { + return $this->getField('locale'); + } + + /** + * Get zone info. + * + * @return string|null + */ + public function getZoneinfo() + { + return $this->getField('zoneinfo'); + } + + /** + * Get preferred username. + * + * @return string|null + */ + public function getPreferredUsername() + { + return $this->getField('preferred_username'); + } + + /** + * Get nickname. + * + * @return string|null + */ + public function getNickname() + { + return $this->getField('nickname'); + } + + /** + * Get website. + * + * @return string|null + */ + public function getWebsite() + { + return $this->getField('website'); + } + + /** + * Get picture. + * + * @return string|null + */ + public function getPicture() + { + return $this->getField('picture'); + } + + /** + * Get user data as an array. + * + * @return array + */ + public function toArray() + { + return $this->data; + } + + /** + * Returns a field from the Graph node data. + * + * @param string $key + * + * @return mixed|null + */ + private function getField($key) + { + return isset($this->data[$key]) ? $this->data[$key] : null; + } +} \ No newline at end of file diff --git a/src/helpers/Provider.php b/src/helpers/Provider.php index 06aeaaa..d722d99 100644 --- a/src/helpers/Provider.php +++ b/src/helpers/Provider.php @@ -12,6 +12,7 @@ public static function getPrimaryColor(string $handle): ?string '500px' => '#0099e5', 'airbnb' => '#ff5b5e', 'amazon' => '#ff9900', + 'amazonCognito' => '#ff9900', 'angelList' => '#000000', 'appStore' => '#176fdc', 'apple' => '#000000', @@ -175,6 +176,8 @@ public static function getIcon(string $handle): ?string 'airbnb' => '', 'amazon' => '', + + 'amazonCognito' => '', 'angelList' => '', diff --git a/src/providers/AmazonCognito.php b/src/providers/AmazonCognito.php new file mode 100644 index 0000000..c42348d --- /dev/null +++ b/src/providers/AmazonCognito.php @@ -0,0 +1,22 @@ +