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\Uri\UriInterface;
10use OAuth\Common\Storage\TokenStorageInterface;
11use OAuth\Common\Http\Client\ClientInterface;
12
13class Pocket extends AbstractService
14{
15    public function __construct(
16        CredentialsInterface $credentials,
17        ClientInterface $httpClient,
18        TokenStorageInterface $storage,
19        $scopes = array(),
20        UriInterface $baseApiUri = null
21    ) {
22        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
23        if ($baseApiUri === null) {
24            $this->baseApiUri = new Uri('https://getpocket.com/v3/');
25        }
26    }
27
28    public function getRequestTokenEndpoint()
29    {
30        return new Uri('https://getpocket.com/v3/oauth/request');
31    }
32
33    public function getAuthorizationEndpoint()
34    {
35        return new Uri('https://getpocket.com/auth/authorize');
36    }
37
38    public function getAccessTokenEndpoint()
39    {
40        return new Uri('https://getpocket.com/v3/oauth/authorize');
41    }
42
43    public function getAuthorizationUri(array $additionalParameters = array())
44    {
45        $parameters = array_merge(
46            $additionalParameters,
47            array(
48                'redirect_uri' => $this->credentials->getCallbackUrl(),
49            )
50        );
51
52        // Build the url
53        $url = clone $this->getAuthorizationEndpoint();
54        foreach ($parameters as $key => $val) {
55            $url->addToQuery($key, $val);
56        }
57
58        return $url;
59    }
60
61    public function requestRequestToken()
62    {
63        $responseBody = $this->httpClient->retrieveResponse(
64            $this->getRequestTokenEndpoint(),
65            array(
66                'consumer_key' => $this->credentials->getConsumerId(),
67                'redirect_uri' => $this->credentials->getCallbackUrl(),
68            )
69        );
70
71        $code = $this->parseRequestTokenResponse($responseBody);
72
73        return $code;
74    }
75
76    protected function parseRequestTokenResponse($responseBody)
77    {
78        parse_str($responseBody, $data);
79
80        if (null === $data || !is_array($data)) {
81            throw new TokenResponseException('Unable to parse response.');
82        } elseif (!isset($data['code'])) {
83            throw new TokenResponseException('Error in retrieving code.');
84        }
85        return $data['code'];
86    }
87
88    public function requestAccessToken($code, $state = null)
89    {
90        $bodyParams = array(
91            'consumer_key'     => $this->credentials->getConsumerId(),
92            'code'             => $code,
93        );
94
95        $responseBody = $this->httpClient->retrieveResponse(
96            $this->getAccessTokenEndpoint(),
97            $bodyParams,
98            $this->getExtraOAuthHeaders()
99        );
100        $token = $this->parseAccessTokenResponse($responseBody);
101        $this->storage->storeAccessToken($this->service(), $token);
102
103        return $token;
104    }
105
106    protected function parseAccessTokenResponse($responseBody)
107    {
108        parse_str($responseBody, $data);
109
110        if ($data === null || !is_array($data)) {
111            throw new TokenResponseException('Unable to parse response.');
112        } elseif (isset($data['error'])) {
113            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
114        }
115
116        $token = new StdOAuth2Token();
117        #$token->setRequestToken($data['access_token']);
118        $token->setAccessToken($data['access_token']);
119        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
120        unset($data['access_token']);
121        $token->setExtraParams($data);
122
123        return $token;
124    }
125}
126