appwrite/src/Appwrite/Auth/OAuth2/Spotify.php

181 lines
4 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
// Reference Material
// https://dev.twitch.tv/docs/authentication
2020-02-16 11:41:03 +00:00
class Spotify extends OAuth2
{
/**
* @var string
*/
2022-05-12 15:56:20 +00:00
private string $endpoint = 'https://accounts.spotify.com/';
/**
* @var string
*/
2022-05-12 15:56:20 +00:00
private string $resourceEndpoint = 'https://api.spotify.com/v1/';
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $scopes = [
'user-read-email',
];
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $user = [];
2022-02-01 08:22:55 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $tokens = [];
2022-02-01 08:22:55 +00:00
/**
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getName(): string
{
return 'spotify';
}
/**
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getLoginURL(): string
{
2022-05-12 15:56:20 +00:00
return $this->endpoint . 'authorize?' .
\http_build_query([
'response_type' => 'code',
'client_id' => $this->appID,
'scope' => \implode(' ', $this->getScopes()),
'redirect_uri' => $this->callback,
'state' => \json_encode($this->state)
]);
}
/**
* @param string $code
*
2022-01-31 20:20:17 +00:00
* @return array
*/
2022-02-01 16:47:19 +00:00
protected function getTokens(string $code): array
{
2022-05-12 15:56:20 +00:00
if (empty($this->tokens)) {
2022-02-01 10:42:11 +00:00
$headers = ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)];
2022-02-01 08:22:55 +00:00
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'api/token',
2022-02-01 10:42:11 +00:00
$headers,
2022-02-01 08:22:55 +00:00
\http_build_query([
"code" => $code,
"grant_type" => "authorization_code",
"redirect_uri" => $this->callback
])
), true);
}
return $this->tokens;
}
2022-02-01 15:54:20 +00:00
/**
* @param string $refreshToken
*
* @return array
*/
2022-05-12 15:56:20 +00:00
public function refreshTokens(string $refreshToken): array
2022-02-01 15:54:20 +00:00
{
$headers = ['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)];
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'api/token',
$headers,
\http_build_query([
"refresh_token" => $refreshToken,
"grant_type" => "refresh_token",
])
), true);
2022-02-01 15:54:20 +00:00
2022-05-12 15:56:20 +00:00
if (empty($this->tokens['refresh_token'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
2022-02-01 15:54:20 +00:00
return $this->tokens;
}
/**
2022-05-12 15:56:20 +00:00
* @param string $accessToken
*
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserID(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['id'] ?? '';
}
/**
2022-05-12 15:56:20 +00:00
* @param string $accessToken
*
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['email'] ?? '';
}
/**
* Check if the OAuth email is verified
2022-05-23 14:54:50 +00:00
*
* Spotify does not assure that the email is verified
*
2022-04-27 19:58:52 +00:00
* @link https://developer.spotify.com/documentation/web-api/reference/#/operations/get-current-users-profile
2022-05-23 14:54:50 +00:00
*
2022-05-12 15:56:20 +00:00
* @param string $accessToken
2022-05-23 14:54:50 +00:00
*
* @return bool
*/
2022-04-27 20:27:21 +00:00
public function isEmailVerified(string $accessToken): bool
{
return false;
}
/**
2022-05-12 15:56:20 +00:00
* @param string $accessToken
*
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['display_name'] ?? '';
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken)
{
if (empty($this->user)) {
2020-06-24 21:05:16 +00:00
$this->user = \json_decode($this->request(
'GET',
2022-04-27 19:58:52 +00:00
$this->resourceEndpoint . 'me',
2022-05-12 15:56:20 +00:00
['Authorization: Bearer ' . \urlencode($accessToken)]
2020-06-24 21:05:16 +00:00
), true);
}
return $this->user;
}
}