1<?php
2
3namespace OAuth\OAuth2\Service;
4
5use OAuth\Common\Exception\Exception;
6use OAuth\OAuth2\Token\StdOAuth2Token;
7use OAuth\Common\Http\Exception\TokenResponseException;
8use OAuth\Common\Http\Uri\Uri;
9use OAuth\Common\Consumer\CredentialsInterface;
10use OAuth\Common\Http\Client\ClientInterface;
11use OAuth\Common\Storage\TokenStorageInterface;
12use OAuth\Common\Http\Uri\UriInterface;
13
14class DeviantArt extends AbstractService
15{
16    /**
17     * DeviantArt www url - used to build dialog urls
18     */
19    const WWW_URL = 'https://www.deviantart.com/';
20
21    /**
22     * Defined scopes
23     *
24     * If you don't think this is scary you should not be allowed on the web at all
25     *
26     * @link https://www.deviantart.com/developers/authentication
27     * @link https://www.deviantart.com/developers/http/v1/20150217
28     */
29    const SCOPE_FEED                       = 'feed';
30    const SCOPE_BROWSE                     = 'browse';
31    const SCOPE_COMMENT                    = 'comment.post';
32    const SCOPE_STASH                      = 'stash';
33    const SCOPE_USER                       = 'user';
34    const SCOPE_USERMANAGE                 = 'user.manage';
35
36    public function __construct(
37        CredentialsInterface $credentials,
38        ClientInterface $httpClient,
39        TokenStorageInterface $storage,
40        $scopes = array(),
41        UriInterface $baseApiUri = null
42    ) {
43        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
44
45        if (null === $baseApiUri) {
46            $this->baseApiUri = new Uri('https://www.deviantart.com/api/v1/oauth2/');
47        }
48    }
49
50    /**
51     * {@inheritdoc}
52     */
53    public function getAuthorizationEndpoint()
54    {
55        return new Uri('https://www.deviantart.com/oauth2/authorize');
56    }
57
58    /**
59     * {@inheritdoc}
60     */
61    public function getAccessTokenEndpoint()
62    {
63        return new Uri('https://www.deviantart.com/oauth2/token');
64    }
65
66    /**
67     * {@inheritdoc}
68     */
69    protected function parseAccessTokenResponse($responseBody)
70    {
71
72        $data = json_decode($responseBody, true);
73
74        if (null === $data || !is_array($data)) {
75            throw new TokenResponseException('Unable to parse response.');
76        } elseif (isset($data['error'])) {
77            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
78        }
79
80        $token = new StdOAuth2Token();
81        $token->setAccessToken($data['access_token']);
82
83        if (isset($data['expires_in'])) {
84            $token->setLifeTime($data['expires_in']);
85        }
86
87        if (isset($data['refresh_token'])) {
88            $token->setRefreshToken($data['refresh_token']);
89            unset($data['refresh_token']);
90        }
91
92        unset($data['access_token']);
93        unset($data['expires_in']);
94
95        $token->setExtraParams($data);
96
97        return $token;
98    }
99}
100