Issues (19)

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

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
    const NAME = 'atlassian';
23
24
    /**
25
     * @var string The Base-URI of the Atlassian instance
26
     */
27
    private $baseUri;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getBaseUri()
33
    {
34
        return $this->baseUri;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAuthorizeUri()
41
    {
42
        return $this->getBaseUri() . '/plugins/servlet/oauth/authorize';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getRequestTokenUri()
49
    {
50
        return $this->getBaseUri() . '/plugins/servlet/oauth/request-token';
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getRequestTokenAccessUri()
57
    {
58
        return $this->getBaseUri() . '/plugins/servlet/oauth/access-token';
59
    }
60
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getName()
66
    {
67
        return self::NAME;
68
    }
69
70
    /**
71
     * Atlassian constructor.
72
     *
73
     * Required configuration parameters are:
74
     *
75
     * * baseUri The base URI of your self-hosted atlassian product
76
     * * applicationSecret The path to the private key file used for signing.
77
     * * applicationId The ID shared with your Atlassian instance
78
     *
79
     * @param \SocialConnect\Common\Http\Client\ClientInterface $httpClient
80
     * @param \SocialConnect\Provider\Session\SessionInterface  $session
81
     * @param \SocialConnect\Provider\Consumer           $consumer
82
     * @param array                                             $parameters
83
     */
84 3
    public function __construct(ClientInterface $httpClient, SessionInterface $session, Consumer $consumer, array $parameters)
85
    {
86 3
        if (!isset($parameters['baseUri'])) {
87 1
            throw new \InvalidArgumentException('There is no "baseUri" given in the configuration');
88
        }
89 2
        $this->baseUri = $parameters['baseUri'];
90 2
        if (($lastSlash = strrpos($this->baseUri, '/')) == strlen($this->baseUri) - 1) {
91 1
            $this->baseUri = substr($this->baseUri, 0, $lastSlash);
92
        }
93
94 2
        parent::__construct($httpClient, $session, $consumer, $parameters);
95
96
        // needs to be set after calling the parent constructor as there the
97
        // signature is set as well.
98 2
        $this->signature = new MethodRSASHA1($consumer->getSecret());
99 2
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getIdentity(AccessTokenInterface $accessToken)
105
    {
106
        $this->consumerToken = $accessToken;
0 ignored issues
show
Documentation Bug introduced by Andreas Heigl
It seems like $accessToken of type SocialConnect\Provider\AccessTokenInterface is incompatible with the declared type 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...
107
108
        $parameters = [
109
            'oauth_consumer_key' => $this->consumer->getKey(),
110
            'oauth_token' => $this->consumerToken->getToken(),
111
        ];
112
113
        $response = $this->oauthRequest(
114
            $this->getBaseUri() . '/rest/prototype/1/user/current',
115
            Client::GET,
116
            $parameters
117
        );
118
119
        $redirectMax = 30;
120
        $redirectCount = 0;
121
        while ($response->hasHeader('Location')) {
122
            if ($redirectMax < $redirectCount++) {
123
                throw new \RangeException('Too many redirects');
124
            }
125
126
            $response = $this->oauthRequest(
127
                $response->getHeader('Location'),
128
                Client::GET,
129
                $parameters
130
            );
131
        }
132
133
        if (!$response->isSuccess()) {
134
            throw new InvalidResponse(
135
                'API response with error code',
136
                $response
137
            );
138
        }
139
140
        $result = $response->json();
141
        if (!$result) {
142
            throw new InvalidResponse(
143
                'API response is not a valid JSON object',
144
                $response
145
            );
146
        }
147
148
        if (!isset($result->name) || !$result->name) {
149
            throw new InvalidResponse(
150
                'API response without user inside JSON',
151
                $response
152
            );
153
        }
154
155
        $hydrator = new ObjectMap([
156
            'name' => 'username',
157
            'displayName' => 'fullname',
158
            'displayableEmail' => 'email',
159
        ]);
160
161
        return $hydrator->hydrate(new User(), $result);
162
    }
163
}
164