Completed
Push — master ( deb54b...06ec1a )
by Дмитрий
01:56
created

src/OAuth1/Provider/Atlassian.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Andreas Heigl https://github.com/heiglandreas <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth1\Provider;
8
9
use SocialConnect\Common\Http\Client\Client;
10
use SocialConnect\Common\Http\Client\ClientInterface;
11
use SocialConnect\OAuth1\Signature\MethodRSASHA1;
12
use SocialConnect\Provider\AccessTokenInterface;
13
use SocialConnect\Provider\Consumer;
14
use SocialConnect\Provider\Exception\InvalidResponse;
15
use SocialConnect\OAuth1\AbstractProvider;
16
use SocialConnect\Common\Entity\User;
17
use SocialConnect\Common\Hydrator\ObjectMap;
18
use SocialConnect\Provider\Session\SessionInterface;
19
20
class Atlassian extends AbstractProvider
21
{
22
    /**
23
     * @var string The Base-URI of the Atlassian instance
24
     */
25
    private $baseUri;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getBaseUri()
31
    {
32
        return $this->baseUri;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getAuthorizeUri()
39
    {
40
        return $this->getBaseUri() . '/plugins/servlet/oauth/authorize';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getRequestTokenUri()
47
    {
48
        return $this->getBaseUri() . '/plugins/servlet/oauth/request-token';
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getRequestTokenAccessUri()
55
    {
56
        return $this->getBaseUri() . '/plugins/servlet/oauth/access-token';
57
    }
58
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getName()
64
    {
65
        return 'atlassian';
66
    }
67
68
    /**
69
     * Atlassian constructor.
70
     *
71
     * Required configuration parameters are:
72
     *
73
     * * baseUri The base URI of your self-hosted atlassian product
74
     * * applicationSecret The path to the private key file used for signing.
75
     * * applicationId The ID shared with your Atlassian instance
76
     *
77
     * @param \SocialConnect\Common\Http\Client\ClientInterface $httpClient
78
     * @param \SocialConnect\Provider\Session\SessionInterface  $session
79
     * @param \SocialConnect\Provider\Consumer           $consumer
80
     * @param array                                             $parameters
81
     */
82 3
    public function __construct(ClientInterface $httpClient, SessionInterface $session, Consumer $consumer, array $parameters)
83
    {
84 3
        if (!isset($parameters['baseUri'])) {
85 1
            throw new \InvalidArgumentException('There is no "baseUri" given in the configuration');
86
        }
87 2
        $this->baseUri = $parameters['baseUri'];
88 2
        if (($lastSlash = strrpos($this->baseUri, '/')) == strlen($this->baseUri) - 1) {
89 1
            $this->baseUri = substr($this->baseUri, 0, $lastSlash);
90 1
        }
91
92 2
        parent::__construct($httpClient, $session, $consumer, $parameters);
93
94
        // needs to be set after calling the parent constructor as there the
95
        // signature is set as well.
96 2
        $this->signature = new MethodRSASHA1($consumer->getSecret());
97 2
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getIdentity(AccessTokenInterface $accessToken)
103
    {
104
        $this->consumerToken = $accessToken;
0 ignored issues
show
Documentation Bug introduced by Andreas Heigl
It seems like $accessToken of type object<SocialConnect\Pro...r\AccessTokenInterface> is incompatible with the declared type object<SocialConnect\OAuth1\Token> of property $consumerToken.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
106
        $parameters = [
107
            'oauth_consumer_key' => $this->consumer->getKey(),
108
            'oauth_token' => $this->consumerToken->getToken(),
109
        ];
110
111
        $response = $this->oauthRequest(
112
            $this->getBaseUri() . '/rest/prototype/1/user/current',
113
            Client::GET,
114
            $parameters
115
        );
116
117
        $redirectMax = 30;
118
        $redirectCount = 0;
119
        while ($response->hasHeader('Location')) {
120
            if ($redirectMax < $redirectCount++) {
121
                throw new \RangeException('Too many redirects');
122
            }
123
124
            $response = $this->oauthRequest(
125
                $response->getHeader('Location'),
126
                Client::GET,
127
                $parameters
128
            );
129
        }
130
131
        if (!$response->isSuccess()) {
132
            throw new InvalidResponse(
133
                'API response with error code',
134
                $response
135
            );
136
        }
137
138
        $result = $response->json();
139
        if (!$result) {
140
            throw new InvalidResponse(
141
                'API response is not a valid JSON object',
142
                $response->getBody()
143
            );
144
        }
145
146
        if (!isset($result->name) || !$result->name) {
147
            throw new InvalidResponse(
148
                'API response without user inside JSON',
149
                $response->getBody()
150
            );
151
        }
152
153
        $hydrator = new ObjectMap([
154
            'name' => 'username',
155
            'displayName' => 'fullname',
156
            'displayableEmail' => 'email',
157
        ]);
158
159
        return $hydrator->hydrate(new User(), $result);
160
    }
161
}
162