xref: /plugin/oauth/vendor/lusitanian/oauth/src/OAuth/OAuth2/Service/Dropbox.php (revision 98a3611675f696f14f2d12205fb81f7db0cf7b25)
1*98a36116SAndreas Gohr<?php
2*98a36116SAndreas Gohr
3*98a36116SAndreas Gohrnamespace OAuth\OAuth2\Service;
4*98a36116SAndreas Gohr
5*98a36116SAndreas Gohruse OAuth\OAuth2\Token\StdOAuth2Token;
6*98a36116SAndreas Gohruse OAuth\Common\Http\Exception\TokenResponseException;
7*98a36116SAndreas Gohruse OAuth\Common\Http\Uri\Uri;
8*98a36116SAndreas Gohruse OAuth\Common\Consumer\CredentialsInterface;
9*98a36116SAndreas Gohruse OAuth\Common\Http\Client\ClientInterface;
10*98a36116SAndreas Gohruse OAuth\Common\Storage\TokenStorageInterface;
11*98a36116SAndreas Gohruse OAuth\Common\Http\Uri\UriInterface;
12*98a36116SAndreas Gohr
13*98a36116SAndreas Gohr/**
14*98a36116SAndreas Gohr * Dropbox service.
15*98a36116SAndreas Gohr *
16*98a36116SAndreas Gohr * @author Flávio Heleno <flaviohbatista@gmail.com>
17*98a36116SAndreas Gohr * @link https://www.dropbox.com/developers/core/docs
18*98a36116SAndreas Gohr */
19*98a36116SAndreas Gohrclass Dropbox extends AbstractService
20*98a36116SAndreas Gohr{
21*98a36116SAndreas Gohr    public function __construct(
22*98a36116SAndreas Gohr        CredentialsInterface $credentials,
23*98a36116SAndreas Gohr        ClientInterface $httpClient,
24*98a36116SAndreas Gohr        TokenStorageInterface $storage,
25*98a36116SAndreas Gohr        $scopes = array(),
26*98a36116SAndreas Gohr        UriInterface $baseApiUri = null
27*98a36116SAndreas Gohr    ) {
28*98a36116SAndreas Gohr        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
29*98a36116SAndreas Gohr
30*98a36116SAndreas Gohr        if (null === $baseApiUri) {
31*98a36116SAndreas Gohr            $this->baseApiUri = new Uri('https://api.dropbox.com/1/');
32*98a36116SAndreas Gohr        }
33*98a36116SAndreas Gohr    }
34*98a36116SAndreas Gohr
35*98a36116SAndreas Gohr    /**
36*98a36116SAndreas Gohr     * {@inheritdoc}
37*98a36116SAndreas Gohr     */
38*98a36116SAndreas Gohr    public function getAuthorizationUri(array $additionalParameters = array())
39*98a36116SAndreas Gohr    {
40*98a36116SAndreas Gohr        $parameters = array_merge(
41*98a36116SAndreas Gohr            $additionalParameters,
42*98a36116SAndreas Gohr            array(
43*98a36116SAndreas Gohr                'client_id'     => $this->credentials->getConsumerId(),
44*98a36116SAndreas Gohr                'redirect_uri'  => $this->credentials->getCallbackUrl(),
45*98a36116SAndreas Gohr                'response_type' => 'code',
46*98a36116SAndreas Gohr            )
47*98a36116SAndreas Gohr        );
48*98a36116SAndreas Gohr
49*98a36116SAndreas Gohr        $parameters['scope'] = implode(' ', $this->scopes);
50*98a36116SAndreas Gohr
51*98a36116SAndreas Gohr        // Build the url
52*98a36116SAndreas Gohr        $url = clone $this->getAuthorizationEndpoint();
53*98a36116SAndreas Gohr        foreach ($parameters as $key => $val) {
54*98a36116SAndreas Gohr            $url->addToQuery($key, $val);
55*98a36116SAndreas Gohr        }
56*98a36116SAndreas Gohr
57*98a36116SAndreas Gohr        return $url;
58*98a36116SAndreas Gohr    }
59*98a36116SAndreas Gohr
60*98a36116SAndreas Gohr    /**
61*98a36116SAndreas Gohr     * {@inheritdoc}
62*98a36116SAndreas Gohr     */
63*98a36116SAndreas Gohr    public function getAuthorizationEndpoint()
64*98a36116SAndreas Gohr    {
65*98a36116SAndreas Gohr        return new Uri('https://www.dropbox.com/1/oauth2/authorize');
66*98a36116SAndreas Gohr    }
67*98a36116SAndreas Gohr
68*98a36116SAndreas Gohr    /**
69*98a36116SAndreas Gohr     * {@inheritdoc}
70*98a36116SAndreas Gohr     */
71*98a36116SAndreas Gohr    public function getAccessTokenEndpoint()
72*98a36116SAndreas Gohr    {
73*98a36116SAndreas Gohr        return new Uri('https://api.dropbox.com/1/oauth2/token');
74*98a36116SAndreas Gohr    }
75*98a36116SAndreas Gohr
76*98a36116SAndreas Gohr    /**
77*98a36116SAndreas Gohr     * {@inheritdoc}
78*98a36116SAndreas Gohr     */
79*98a36116SAndreas Gohr    protected function getAuthorizationMethod()
80*98a36116SAndreas Gohr    {
81*98a36116SAndreas Gohr        return static::AUTHORIZATION_METHOD_QUERY_STRING;
82*98a36116SAndreas Gohr    }
83*98a36116SAndreas Gohr
84*98a36116SAndreas Gohr    /**
85*98a36116SAndreas Gohr     * {@inheritdoc}
86*98a36116SAndreas Gohr     */
87*98a36116SAndreas Gohr    protected function parseAccessTokenResponse($responseBody)
88*98a36116SAndreas Gohr    {
89*98a36116SAndreas Gohr        $data = json_decode($responseBody, true);
90*98a36116SAndreas Gohr
91*98a36116SAndreas Gohr        if (null === $data || !is_array($data)) {
92*98a36116SAndreas Gohr            throw new TokenResponseException('Unable to parse response.');
93*98a36116SAndreas Gohr        } elseif (isset($data['error'])) {
94*98a36116SAndreas Gohr            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
95*98a36116SAndreas Gohr        }
96*98a36116SAndreas Gohr
97*98a36116SAndreas Gohr        $token = new StdOAuth2Token();
98*98a36116SAndreas Gohr        $token->setAccessToken($data['access_token']);
99*98a36116SAndreas Gohr
100*98a36116SAndreas Gohr        if (isset($data['refresh_token'])) {
101*98a36116SAndreas Gohr            $token->setRefreshToken($data['refresh_token']);
102*98a36116SAndreas Gohr            unset($data['refresh_token']);
103*98a36116SAndreas Gohr        }
104*98a36116SAndreas Gohr
105*98a36116SAndreas Gohr        unset($data['access_token']);
106*98a36116SAndreas Gohr
107*98a36116SAndreas Gohr        $token->setExtraParams($data);
108*98a36116SAndreas Gohr
109*98a36116SAndreas Gohr        return $token;
110*98a36116SAndreas Gohr    }
111*98a36116SAndreas Gohr}
112