Passed
Push — develop ( 51d2fc...7adcff )
by Alexandre
02:11
created

Jira::getScopeSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mrjoops\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Token\AccessToken;
7
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Mrjoops\OAuth2\Client\Provider\Exception\JiraIdentityProviderException;
10
use Psr\Http\Message\ResponseInterface;
11
12
class Jira extends AbstractProvider
13
{
14
    use ArrayAccessorTrait;
15
    use BearerAuthorizationTrait;
16
    
17
    /**
18
     *
19
     * @var string URL used for non-OAuth API calls
20
     */
21
    protected $apiUrl;
22
23
    /**
24
     * Check a provider response for errors.
25
     *
26
     * @throws IdentityProviderException
27
     * @param  ResponseInterface $response
28
     * @param  array $data Parsed response data
29
     *
30
     * @return void
31
     */
32
    protected function checkResponse(ResponseInterface $response, $data)
33
    {
34
        if ($response->getStatusCode() >= 400) {
35
            throw JiraIdentityProviderException::clientException($response, $data);
36
        } elseif (isset($data['error'])) {
37
            throw JiraIdentityProviderException::oauthException($response, $data);
38
        }
39
    }
40
41
    /**
42
     * Generate a user object from a successful user details request.
43
     *
44
     * @param array $response
45
     * @param AccessToken $token
46
     *
47
     * @return \League\OAuth2\Client\Provider\ResourceOwnerInterface
48
     */
49
    protected function createResourceOwner(array $response, AccessToken $token)
50
    {
51
        return new JiraResourceOwner($response);
52
    }
53
    
54
    /**
55
     *
56
     * @return string URL used for non-OAuth API calls
57
     */
58
    public function getApiUrl()
59
    {
60
        return $this->apiUrl;
61
    }
62
63
    /**
64
     * Get access token url to retrieve token
65
     *
66
     * @param  array $params
67
     *
68
     * @return string
69
     */
70
    public function getBaseAccessTokenUrl(array $params)
71
    {
72
        return 'https://accounts.atlassian.com/oauth/token';
73
    }
74
75
    /**
76
     * Get authorization url to begin OAuth flow
77
     *
78
     * @return string
79
     */
80
    public function getBaseAuthorizationUrl()
81
    {
82
        return 'https://accounts.atlassian.com/authorize?audience=api.atlassian.com&prompt=consent';
83
    }
84
85
    /**
86
     * Get the default scopes used by this provider.
87
     *
88
     * This should not be a complete list of all scopes, but the minimum
89
     * required for the provider user interface!
90
     *
91
     * @return array
92
     */
93
    protected function getDefaultScopes()
94
    {
95
        return ['jira:read-user'];
96
    }
97
    
98
    /**
99
     * Get provider url to fetch user details
100
     *
101
     * @param AccessToken $token
102
     *
103
     * @return string
104
     */
105
    public function getResourceOwnerDetailsUrl(AccessToken $token)
106
    {
107
        $request = $this->getAuthenticatedRequest(
108
            self::METHOD_GET,
109
            'https://api.atlassian.com/oauth/token/accessible-resources',
110
            $token
111
        );
112
113
        $response = $this->getParsedResponse($request);
114
115
        if (false === is_array($response)) {
0 ignored issues
show
introduced by
The condition false === is_array($response) is always false.
Loading history...
116
            throw new \UnexpectedValueException(
117
                'Invalid response received from Authorization Server. Expected JSON.'
118
            );
119
        }
120
        
121
        $cloudId = $this->getValueByKey($response, '0.id');
122
        
123
        $this->setApiUrl('https://api.atlassian.com/ex/jira/'.$cloudId);
124
125
        return $this->getApiUrl().'/rest/api/3/myself';
126
    }
127
    
128
    /**
129
     * Returns the string that should be used to separate scopes when building
130
     * the URL for requesting an access token.
131
     *
132
     * @return string Scope separator, defaults to ' '
133
     */
134
    protected function getScopeSeparator(): string
135
    {
136
        return ' ';
137
    }
138
    
139
    /**
140
     *
141
     * @param string $url URL used for non-OAuth API calls
142
     */
143
    protected function setApiUrl($url)
144
    {
145
        $this->apiUrl = $url;
146
    }
147
}
148