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;
8
9class Yahoo extends AbstractService
10{
11
12    /**
13    * {@inheritdoc}
14    */
15    public function getAuthorizationEndpoint()
16    {
17        return new Uri('https://api.login.yahoo.com/oauth2/request_auth');
18    }
19
20    /**
21    * {@inheritdoc}
22    */
23    public function getAccessTokenEndpoint()
24    {
25        return new Uri('https://api.login.yahoo.com/oauth2/get_token');
26    }
27
28    /**
29    * {@inheritdoc}
30    */
31    protected function getAuthorizationMethod()
32    {
33        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
34    }
35
36    /**
37    * {@inheritdoc}
38    */
39    protected function parseAccessTokenResponse($responseBody)
40    {
41        $data = json_decode($responseBody, true);
42
43        if (null === $data || !is_array($data)) {
44            throw new TokenResponseException('Unable to parse response.');
45        } elseif (isset($data['error'])) {
46            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
47        }
48
49        $token = new StdOAuth2Token();
50        $token->setAccessToken($data['access_token']);
51        $token->setLifetime($data['expires_in']);
52
53        if (isset($data['refresh_token'])) {
54            $token->setRefreshToken($data['refresh_token']);
55            unset($data['refresh_token']);
56        }
57
58        unset($data['access_token']);
59        unset($data['expires_in']);
60
61        $token->setExtraParams($data);
62
63        return $token;
64    }
65
66    /**
67    * {@inheritdoc}
68    */
69    protected function getExtraOAuthHeaders()
70    {
71        $encodedCredentials = base64_encode(
72            $this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()
73        );
74        return array('Authorization' => 'Basic ' . $encodedCredentials);
75    }
76}
77