1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AtlassianConnectBundle\Service; |
6
|
|
|
|
7
|
|
|
use AtlassianConnectBundle\Entity\TenantInterface; |
8
|
|
|
use Symfony\Component\HttpClient\DecoratorTrait; |
9
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
10
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
11
|
|
|
|
12
|
|
|
final class AuthenticatedAtlassianClient implements HttpClientInterface |
13
|
|
|
{ |
14
|
|
|
use DecoratorTrait; |
15
|
|
|
|
16
|
|
|
public function request(string $method, string $url, array $options = []): ResponseInterface |
17
|
|
|
{ |
18
|
|
|
/** @var TenantInterface $tenant */ |
19
|
|
|
$tenant = $options['tenant']; |
20
|
|
|
$userId = $options['user_id']; |
21
|
|
|
unset($options['tenant']); |
22
|
|
|
unset($options['user_id']); |
23
|
|
|
|
24
|
|
|
if (!$userId) { |
25
|
|
|
$options['headers']['Authorization'] = 'JWT '.JWTGenerator::generate($url, $method, $tenant->getAddonKey(), $tenant->getSharedSecret()); |
|
|
|
|
26
|
|
|
|
27
|
|
|
return $this->client->request($method, $url, $options); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!$tenant->getOauthClientId()) { |
31
|
|
|
throw new \RuntimeException('Tenant is not set up as oath application. Install the app with "ACT_AS_USER" scope.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$result = $this->client->request('POST', 'https://oauth-2-authorization-server.services.atlassian.com/oauth2/token', [ |
35
|
|
|
'body' => [ |
36
|
|
|
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
37
|
|
|
'assertion' => JWTGenerator::generateAssertion($tenant->getSharedSecret(), $tenant->getOauthClientId(), $tenant->getBaseUrl(), $userId), |
|
|
|
|
38
|
|
|
], |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$options['headers']['Authorization'] = 'Bearer '.$result->toArray()['access_token']; |
42
|
|
|
|
43
|
|
|
return $this->client->request($method, $url, $options); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|