diff --git a/.gitignore b/.gitignore index 3d6001ca06..a0e910f79b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ debug/ app/sdks dev/yasd_init.php +.phpunit.result.cache diff --git a/app/config/providers.php b/app/config/providers.php index 205d07123b..98ad93b30c 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -121,6 +121,16 @@ return [ // Ordered by ABC. 'beta' => false, 'mock' => false, ], + 'etsy' => [ + 'name' => 'Etsy', + 'developers' => 'https://developers.etsy.com/', + 'icon' => 'icon-etsy', + 'enabled' => true, + 'sandbox' => false, + 'form' => false, + 'beta' => false, + 'mock' => false, + ], 'facebook' => [ 'name' => 'Facebook', 'developers' => 'https://developers.facebook.com/', diff --git a/public/images/users/etsy.png b/public/images/users/etsy.png new file mode 100644 index 0000000000..ac038ec97e Binary files /dev/null and b/public/images/users/etsy.png differ diff --git a/src/Appwrite/Auth/OAuth2/Etsy.php b/src/Appwrite/Auth/OAuth2/Etsy.php new file mode 100644 index 0000000000..7ff16fcb78 --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Etsy.php @@ -0,0 +1,200 @@ +pkce)) { + $this->pkce = \bin2hex(\random_bytes(rand(43, 128))); + } + + return $this->pkce; + } + + /** + * @return string + */ + public function getName(): string + { + return 'etsy'; + } + + /** + * @return string + */ + public function getLoginURL(): string + { + return 'https://www.etsy.com/oauth/connect/oauth/authorize?' . \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'response_type' => 'code', + 'state' => \json_encode($this->state), + 'scope' => $this->scopes, + 'code_challenge' => $this->getPKCE(), + 'code_challenge_method' => 'S256', + ]); + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + if (empty($this->tokens)) { + $headers = ['Content-Type: application/x-www-form-urlencoded']; + + $this->tokens = \json_decode($this->request( + 'POST', + $this->endpoint . '/oauth/token', + $headers, + \http_build_query([ + 'grant_type' => 'authorization_code', + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'code' => $code, + 'code_verifier' => $this->getPKCE(), + ]) + ), true); + } + + return $this->tokens; + } + + /** + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $headers = ['Content-Type: application/x-www-form-urlencoded']; + + $this->tokens = \json_decode($this->request( + 'POST', + $this->endpoint . '/oauth/token', + $headers, + \http_build_query([ + 'grant_type' => 'refresh_token', + 'client_id' => $this->appID, + 'refresh_token' => $refreshToken, + ]) + ), true); + + if (empty($this->tokens['refresh_token'])) { + $this->tokens['refresh_token'] = $refreshToken; + } + + return $this->tokens; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserID(string $accessToken): string + { + $components = explode('.', $accessToken); + + return $components[0]; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + return $this->getUser($accessToken)['primary_email']; + } + + /** + * Check if the OAuth email is verified + * + * OAuth is only allowed if account has been verified through Etsy, itself. + * + * @param string $accessToken + * + * @return bool + */ + public function isEmailVerified(string $accessToken): bool + { + $email = $this->getUserEmail($accessToken); + + return !empty($email); + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + return $this->getUser($accessToken)['login_name']; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken): array + { + if (!empty($this->user)) { + return $this->user; + } + + $headers = ['Authorization: Bearer ' . $accessToken]; + + $this->user = \json_decode($this->request( + 'GET', + 'https://api.etsy.com/v3/application/users/' . $this->getUserID($accessToken), + ), true); + + return $this->user; + } +}