1<?php
2
3namespace OAuth\OAuth2\Service;
4
5use OAuth\OAuth2\Token\StdOAuth2Token;
6use OAuth\Common\Http\Exception\TokenResponseException;
7use OAuth\Common\Http\Uri\Uri;
8use OAuth\Common\Consumer\CredentialsInterface;
9use OAuth\Common\Http\Client\ClientInterface;
10use OAuth\Common\Storage\TokenStorageInterface;
11use OAuth\Common\Http\Uri\UriInterface;
12
13class Spotify extends AbstractService
14{
15    /**
16     * Scopes
17     *
18     * @var string
19     */
20    const SCOPE_PLAYLIST_MODIFY_PUBLIC = 'playlist-modify-public';
21    const SCOPE_PLAYLIST_MODIFY_PRIVATE = 'playlist-modify-private';
22    const SCOPE_PLAYLIST_READ_PRIVATE = 'playlist-read-private';
23    const SCOPE_PLAYLIST_READ_COLABORATIVE = 'playlist-read-collaborative';
24    const SCOPE_STREAMING = 'streaming';
25    const SCOPE_USER_LIBRARY_MODIFY = 'user-library-modify';
26    const SCOPE_USER_LIBRARY_READ = 'user-library-read';
27    const SCOPE_USER_READ_PRIVATE = 'user-read-private';
28    const SCOPE_USER_READ_EMAIL = 'user-read-email';
29    const SCOPE_USER_READ_BIRTHDAY = 'user-read-birthdate';
30    const SCOPE_USER_READ_FOLLOW = 'user-follow-read';
31
32    public function __construct(
33        CredentialsInterface $credentials,
34        ClientInterface $httpClient,
35        TokenStorageInterface $storage,
36        $scopes = array(),
37        UriInterface $baseApiUri = null
38    ) {
39        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
40
41        if (null === $baseApiUri) {
42            $this->baseApiUri = new Uri('https://api.spotify.com/v1/');
43        }
44    }
45
46    /**
47     * {@inheritdoc}
48     */
49    public function getAuthorizationEndpoint()
50    {
51        return new Uri('https://accounts.spotify.com/authorize');
52    }
53
54    /**
55     * {@inheritdoc}
56     */
57    public function getAccessTokenEndpoint()
58    {
59        return new Uri('https://accounts.spotify.com/api/token');
60    }
61
62    /**
63     * {@inheritdoc}
64     */
65    protected function getAuthorizationMethod()
66    {
67        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
68    }
69
70    /**
71     * {@inheritdoc}
72     */
73    protected function parseAccessTokenResponse($responseBody)
74    {
75        $data = json_decode($responseBody, true);
76
77        if (null === $data || !is_array($data)) {
78            throw new TokenResponseException('Unable to parse response.');
79        } elseif (isset($data['error'])) {
80            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
81        }
82
83
84        $token = new StdOAuth2Token();
85        $token->setAccessToken($data['access_token']);
86
87        if (isset($data['expires_in'])) {
88            $token->setLifetime($data['expires_in']);
89            unset($data['expires_in']);
90        }
91
92        if (isset($data['refresh_token'])) {
93            $token->setRefreshToken($data['refresh_token']);
94            unset($data['refresh_token']);
95        }
96
97        unset($data['access_token']);
98
99        $token->setExtraParams($data);
100
101        return $token;
102    }
103
104    /**
105     * {@inheritdoc}
106     */
107    protected function getExtraOAuthHeaders()
108    {
109        return array('Authorization' => 'Basic ' .
110            base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()));
111    }
112}
113