diff --git a/app/config/providers.php b/app/config/providers.php index 061bbd9d8..866d56393 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -97,6 +97,12 @@ return [ 'enabled' => true, 'mock' => false, ], + 'spotify' => [ + 'developers' => 'https://developer.spotify.com/documentation/general/guides/authorization-guide/', + 'icon' => 'icon-spotify', + 'enabled' => true, + 'mock' => false, + ], // Keep Last 'mock' => [ 'developers' => 'https://appwrite.io', diff --git a/public/images/oauth/spotify.png b/public/images/oauth/spotify.png new file mode 100644 index 000000000..ad7f689d6 Binary files /dev/null and b/public/images/oauth/spotify.png differ diff --git a/src/Auth/OAuth/Spotify.php b/src/Auth/OAuth/Spotify.php new file mode 100644 index 000000000..44854fbb4 --- /dev/null +++ b/src/Auth/OAuth/Spotify.php @@ -0,0 +1,146 @@ +endpoint . 'authorize?'. + 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); + $result = json_decode($this->request( + 'POST', + $this->endpoint . 'api/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['id'])) { + return $user['id']; + } + + 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['display_name'])) { + return $user['display_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; + } +}