Heroku::parseAccessTokenResponse()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 29
c 1
b 0
f 0
rs 9.0777
cc 6
nc 4
nop 1
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
/**
14
 * Heroku service.
15
 *
16
 * @author Thomas Welton <[email protected]>
17
 *
18
 * @see https://devcenter.heroku.com/articles/oauth
19
 */
20
class Heroku extends AbstractService
21
{
22
    /**
23
     * Defined scopes.
24
     *
25
     * @see https://devcenter.heroku.com/articles/oauth#scopes
26
     */
27
    const SCOPE_GLOBAL = 'global';
28
    const SCOPE_IDENTITY = 'identity';
29
    const SCOPE_READ = 'read';
30
    const SCOPE_WRITE = 'write';
31
    const SCOPE_READ_PROTECTED = 'read-protected';
32
    const SCOPE_WRITE_PROTECTED = 'write-protected';
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function __construct(
38
        CredentialsInterface $credentials,
39
        ClientInterface $httpClient,
40
        TokenStorageInterface $storage,
41
        $scopes = [],
42
        ?UriInterface $baseApiUri = null
43
    ) {
44
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
45
46
        if (null === $baseApiUri) {
47
            $this->baseApiUri = new Uri('https://api.heroku.com/');
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getAuthorizationEndpoint()
55
    {
56
        return new Uri('https://id.heroku.com/oauth/authorize');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getAccessTokenEndpoint()
63
    {
64
        return new Uri('https://id.heroku.com/oauth/token');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function getAuthorizationMethod()
71
    {
72
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function parseAccessTokenResponse($responseBody)
79
    {
80
        $data = json_decode($responseBody, true);
81
82
        if (null === $data || !is_array($data)) {
83
            throw new TokenResponseException('Unable to parse response.');
84
        } elseif (isset($data['error_description']) || isset($data['error'])) {
85
            throw new TokenResponseException(
86
                sprintf(
87
                    'Error in retrieving token: "%s"',
88
                    $data['error_description'] ?? $data['error']
89
                )
90
            );
91
        }
92
93
        $token = new StdOAuth2Token();
94
        $token->setAccessToken($data['access_token']);
95
        $token->setLifeTime($data['expires_in']);
96
97
        if (isset($data['refresh_token'])) {
98
            $token->setRefreshToken($data['refresh_token']);
99
            unset($data['refresh_token']);
100
        }
101
102
        unset($data['access_token'], $data['expires_in']);
103
104
        $token->setExtraParams($data);
105
106
        return $token;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    protected function getExtraOAuthHeaders()
113
    {
114
        return ['Accept' => 'application/vnd.heroku+json; version=3'];
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    protected function getExtraApiHeaders()
121
    {
122
        return ['Accept' => 'application/vnd.heroku+json; version=3', 'Content-Type' => 'application/json'];
123
    }
124
}
125