Jira   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 134
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiUrl() 0 3 1
A createResourceOwner() 0 3 1
A getBaseAuthorizationUrl() 0 3 1
A getBaseAccessTokenUrl() 0 3 1
A getDefaultScopes() 0 3 1
A checkResponse() 0 6 3
A setApiUrl() 0 3 1
A getScopeSeparator() 0 3 1
A getResourceOwnerDetailsUrl() 0 21 2
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 15
    protected function checkResponse(ResponseInterface $response, $data)
33
    {
34 15
        if ($response->getStatusCode() >= 400) {
35 3
            throw JiraIdentityProviderException::clientException($response, $data);
36 12
        } elseif (isset($data['error'])) {
37 3
            throw JiraIdentityProviderException::oauthException($response, $data);
38
        }
39 9
    }
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 3
    protected function createResourceOwner(array $response, AccessToken $token)
50
    {
51 3
        return new JiraResourceOwner($response);
52
    }
53
    
54
    /**
55
     *
56
     * @return string URL used for non-OAuth API calls
57
     */
58 3
    public function getApiUrl()
59
    {
60 3
        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 15
    public function getBaseAccessTokenUrl(array $params)
71
    {
72 15
        return 'https://auth.atlassian.com/oauth/token';
73
    }
74
75
    /**
76
     * Get authorization url to begin OAuth flow
77
     *
78
     * @return string
79
     */
80 9
    public function getBaseAuthorizationUrl()
81
    {
82 9
        return 'https://auth.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 6
    protected function getDefaultScopes()
94
    {
95 6
        return ['read:jira-user'];
96
    }
97
    
98
    /**
99
     * Get provider url to fetch user details
100
     *
101
     * @param AccessToken $token
102
     *
103
     * @return string
104
     */
105 6
    public function getResourceOwnerDetailsUrl(AccessToken $token)
106
    {
107 6
        $request = $this->getAuthenticatedRequest(
108 6
            self::METHOD_GET,
109 6
            'https://api.atlassian.com/oauth/token/accessible-resources',
110 4
            $token
111 2
        );
112
113 6
        $response = $this->getParsedResponse($request);
114
115 6
        if (false === is_array($response)) {
0 ignored issues
show
introduced by
The condition false === is_array($response) is always false.
Loading history...
116 3
            throw new \UnexpectedValueException(
117 2
                'Invalid response received from Authorization Server. Expected JSON.'
118 1
            );
119
        }
120
        
121 3
        $cloudId = $this->getValueByKey($response, '0.id');
122
        
123 3
        $this->setApiUrl('https://api.atlassian.com/ex/jira/'.$cloudId);
124
125 3
        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 9
    protected function getScopeSeparator()
135
    {
136 9
        return ' ';
137
    }
138
    
139
    /**
140
     *
141
     * @param string $url URL used for non-OAuth API calls
142
     */
143 3
    protected function setApiUrl($url)
144
    {
145 3
        $this->apiUrl = $url;
146 3
    }
147
}
148