|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SocialConnect project |
|
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
declare(strict_types=1); |
|
7
|
|
|
|
|
8
|
|
|
namespace SocialConnect\OAuth1\Provider; |
|
9
|
|
|
|
|
10
|
|
|
use SocialConnect\Common\ArrayHydrator; |
|
11
|
|
|
use SocialConnect\Common\HttpStack; |
|
12
|
|
|
use SocialConnect\OAuth1\Signature\MethodRSASHA1; |
|
13
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
|
14
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
|
15
|
|
|
use SocialConnect\OAuth1\AbstractProvider; |
|
16
|
|
|
use SocialConnect\Common\Entity\User; |
|
17
|
|
|
use SocialConnect\Provider\Session\SessionInterface; |
|
18
|
|
|
|
|
19
|
|
|
class Atlassian extends AbstractProvider |
|
20
|
|
|
{ |
|
21
|
|
|
const NAME = 'atlassian'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string The Base-URI of the Atlassian instance |
|
25
|
|
|
*/ |
|
26
|
|
|
private $baseUri; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
5 |
|
public function getBaseUri() |
|
32
|
|
|
{ |
|
33
|
5 |
|
return $this->baseUri; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function getAuthorizeUri() |
|
40
|
|
|
{ |
|
41
|
1 |
|
return $this->getBaseUri() . '/plugins/servlet/oauth/authorize'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getRequestTokenUri() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $this->getBaseUri() . '/plugins/servlet/oauth/request-token'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function getRequestTokenAccessUri() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->getBaseUri() . '/plugins/servlet/oauth/access-token'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function getName() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return self::NAME; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Atlassian constructor. |
|
71
|
|
|
* |
|
72
|
|
|
* Required configuration parameters are: |
|
73
|
|
|
* |
|
74
|
|
|
* * baseUri The base URI of your self-hosted atlassian product |
|
75
|
|
|
* * applicationSecret The path to the private key file used for signing. |
|
76
|
|
|
* * applicationId The ID shared with your Atlassian instance |
|
77
|
|
|
* |
|
78
|
|
|
* @param HttpStack $httpStack |
|
79
|
|
|
* @param \SocialConnect\Provider\Session\SessionInterface $session |
|
80
|
|
|
* @param array $parameters |
|
81
|
|
|
* @throws \SocialConnect\Provider\Exception\InvalidProviderConfiguration |
|
82
|
|
|
*/ |
|
83
|
9 |
|
public function __construct(HttpStack $httpStack, SessionInterface $session, array $parameters) |
|
84
|
|
|
{ |
|
85
|
9 |
|
if (!isset($parameters['baseUri'])) { |
|
86
|
1 |
|
throw new \InvalidArgumentException('There is no "baseUri" given in the configuration'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
8 |
|
$this->baseUri = $parameters['baseUri']; |
|
90
|
|
|
|
|
91
|
8 |
|
if (($lastSlash = strrpos($this->baseUri, '/')) == strlen($this->baseUri) - 1) { |
|
92
|
7 |
|
$this->baseUri = substr($this->baseUri, 0, $lastSlash); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
8 |
|
parent::__construct($httpStack, $session, $parameters); |
|
96
|
|
|
|
|
97
|
|
|
// needs to be set after calling the parent constructor as there the |
|
98
|
|
|
// signature is set as well. |
|
99
|
8 |
|
$this->signature = new MethodRSASHA1($this->consumer->getSecret()); |
|
100
|
8 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
|
106
|
|
|
{ |
|
107
|
|
|
$parameters = [ |
|
108
|
1 |
|
'oauth_consumer_key' => $this->consumer->getKey(), |
|
109
|
1 |
|
'oauth_token' => $accessToken->getToken(), |
|
110
|
|
|
]; |
|
111
|
|
|
|
|
112
|
1 |
|
$response = $this->oauthRequest( |
|
113
|
1 |
|
$this->getBaseUri() . '/rest/prototype/1/user/current', |
|
114
|
1 |
|
'GET', |
|
115
|
|
|
$parameters |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
1 |
|
$redirectMax = 30; |
|
119
|
1 |
|
$redirectCount = 0; |
|
120
|
|
|
|
|
121
|
1 |
|
while ($response->hasHeader('Location')) { |
|
122
|
|
|
if ($redirectMax < $redirectCount++) { |
|
123
|
|
|
throw new \RangeException('Too many redirects'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$response = $this->oauthRequest( |
|
127
|
|
|
$response->getHeaderLine('Location'), |
|
128
|
|
|
'GET', |
|
129
|
|
|
$parameters |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
$result = $this->hydrateResponse($response); |
|
134
|
|
|
|
|
135
|
1 |
|
if (!isset($result['name']) || !$result['name']) { |
|
136
|
|
|
throw new InvalidResponse( |
|
137
|
|
|
'API response without user inside JSON', |
|
138
|
|
|
$response |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
$hydrator = new ArrayHydrator([ |
|
143
|
1 |
|
'name' => 'username', |
|
144
|
|
|
'displayName' => 'fullname', |
|
145
|
|
|
'displayableEmail' => 'email', |
|
146
|
|
|
]); |
|
147
|
|
|
|
|
148
|
1 |
|
return $hydrator->hydrate(new User(), $result); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|