Completed
Pull Request — master (#483)
by
unknown
03:10
created

Yahoo::getExtraOAuthHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 4
b 1
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
class 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