1
0
Fork 0
mirror of synced 2024-06-25 01:30:56 +12:00

Added Spotify OAuth provider initial files

This commit is contained in:
Armino Popp 2020-01-16 12:48:05 +02:00
parent 0e98afb29d
commit 5bfd1e602a
3 changed files with 152 additions and 0 deletions

View file

@ -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',

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

146
src/Auth/OAuth/Spotify.php Normal file
View file

@ -0,0 +1,146 @@
<?php
namespace Auth\OAuth;
use Auth\OAuth;
// Reference Material
// https://dev.twitch.tv/docs/authentication
class Spotify extends OAuth
{
/**
* @var string
*/
private $endpoint = 'https://accounts.spotify.com/';
/**
* @var string
*/
private $resourceEndpoint = 'https://api.spotify.com/v1/';
/**
* @var array
*/
protected $scope = [
'user-read-email',
];
/**
* @var array
*/
protected $user = [];
/**
* @return string
*/
public function getName():string
{
return 'spotify';
}
/**
* @return string
*/
public function getLoginURL():string
{
return $this->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;
}
}