diff --git a/app/config/providers.php b/app/config/providers.php index 866d563934..58c29e05c6 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -103,6 +103,12 @@ return [ 'enabled' => true, 'mock' => false, ], + 'yahoo' => [ + 'developers' => 'https://developer.yahoo.com/oauth2/guide/flows_authcode/', + 'icon' => 'icon-yahoo', + 'enabled' => true, + 'mock' => false, + ], // Keep Last 'mock' => [ 'developers' => 'https://appwrite.io', diff --git a/public/images/oauth/yahoo.png b/public/images/oauth/yahoo.png new file mode 100644 index 0000000000..67735afdfa Binary files /dev/null and b/public/images/oauth/yahoo.png differ diff --git a/src/Auth/OAuth/Yahoo.php b/src/Auth/OAuth/Yahoo.php new file mode 100644 index 0000000000..cdf865bbec --- /dev/null +++ b/src/Auth/OAuth/Yahoo.php @@ -0,0 +1,162 @@ +endpoint . 'request_auth?'. + http_build_query([ + 'response_type' => 'code', + 'client_id' => $this->appID, + 'scope' => implode(' ', $this->scope), + 'redirect_uri' => $this->callback, + 'state' => json_encode($this->state) + ]); + } + + /** + * @param string $code + * + * @return string + */ + public function getAccessToken(string $code):string + { + $header = [ + "Authorization: Basic " . base64_encode($this->appID . ":" . $this->appSecret), + "Content-Type: application/x-www-form-urlencoded", + ]; + + $result = json_decode($this->request( + 'POST', + $this->endpoint . 'get_token', + $header, + http_build_query([ + "code" => $code, + "grant_type" => "authorization_code", + "redirect_uri" => $this->callback + ]) + ), true); + + if (isset($result['access_token'])) { + return $result['access_token']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserID(string $accessToken):string + { + $user = $this->getUser($accessToken); + + if (isset($user['sub'])) { + return $user['sub']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken):string + { + $user = $this->getUser($accessToken); + + if (isset($user['email'])) { + return $user['email']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserName(string $accessToken):string + { + $user = $this->getUser($accessToken); + + if (isset($user['name'])) { + return $user['name']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken) + { + if (empty($this->user)) { + $this->user = json_decode($this->request('GET', + $this->resourceEndpoint, ['Authorization: Bearer '.urlencode($accessToken)]), true); + } + + return $this->user; + } +}