Passed
Pull Request — master (#71)
by Matthieu
05:19
created

GuzzleJWTMiddleware::authUserTokenMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Service;
6
7
use AtlassianConnectBundle\Entity\TenantInterface;
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\RequestOptions;
10
use Psr\Http\Message\RequestInterface;
11
12
class GuzzleJWTMiddleware
13
{
14
    public static function middleware(ClientInterface $client): callable
15
    {
16
        return static function (callable $handler) use ($client): callable {
17
            return static function (RequestInterface $request, array $options) use ($handler, $client) {
18
                if (!\array_key_exists('tenant', $options) || !$options['tenant'] instanceof TenantInterface) {
19
                    throw new \RuntimeException('Tenant not provided!');
20
                }
21
22
                $tenant = $options['tenant'];
23
24
                if (\array_key_exists('user_id', $options) && null !== $options['user_id']) {
25
                    if (!$tenant->getOauthClientId()) {
26
                        throw new \RuntimeException('Tenant is not set up as oath application. Install the app with "ACT_AS_USER" scope.');
27
                    }
28
29
                    return $handler($request
30
                        ->withHeader('Accept', 'application/json')
31
                        ->withHeader(
32
                            'Authorization',
33
                            'Bearer '.self::getAuthToken($client, $tenant->getOauthClientId(), $tenant->getSharedSecret(), $tenant->getBaseUrl(), $options['user_id']),
0 ignored issues
show
Bug introduced by
It seems like $tenant->getBaseUrl() can also be of type null; however, parameter $baseUrl of AtlassianConnectBundle\S...dleware::getAuthToken() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
                            'Bearer '.self::getAuthToken($client, $tenant->getOauthClientId(), $tenant->getSharedSecret(), /** @scrutinizer ignore-type */ $tenant->getBaseUrl(), $options['user_id']),
Loading history...
Bug introduced by
It seems like $tenant->getSharedSecret() can also be of type null; however, parameter $secret of AtlassianConnectBundle\S...dleware::getAuthToken() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
                            'Bearer '.self::getAuthToken($client, $tenant->getOauthClientId(), /** @scrutinizer ignore-type */ $tenant->getSharedSecret(), $tenant->getBaseUrl(), $options['user_id']),
Loading history...
34
                        ), $options);
35
                }
36
37
                return $handler($request->withHeader(
38
                    'Authorization',
39
                    'JWT '.JWTGenerator::generate($request, $tenant->getAddonKey(), $tenant->getSharedSecret()),
0 ignored issues
show
Bug introduced by
It seems like $tenant->getSharedSecret() can also be of type null; however, parameter $secret of AtlassianConnectBundle\S...WTGenerator::generate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                    'JWT '.JWTGenerator::generate($request, $tenant->getAddonKey(), /** @scrutinizer ignore-type */ $tenant->getSharedSecret()),
Loading history...
Bug introduced by
It seems like $tenant->getAddonKey() can also be of type null; however, parameter $issuer of AtlassianConnectBundle\S...WTGenerator::generate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                    'JWT '.JWTGenerator::generate($request, /** @scrutinizer ignore-type */ $tenant->getAddonKey(), $tenant->getSharedSecret()),
Loading history...
40
                ), $options);
41
            };
42
        };
43
    }
44
45
    private static function getAuthToken(
46
        ClientInterface $client,
47
        string $oauthClientId,
48
        string $secret,
49
        string $baseUrl,
50
        string $username
51
    ): string {
52
        $result = $client->request('POST', 'https://oauth-2-authorization-server.services.atlassian.com/oauth2/token', [
53
            RequestOptions::FORM_PARAMS => [
54
                'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
55
                'assertion' => JWTGenerator::generateAssertion($secret, $oauthClientId, $baseUrl, $username),
56
            ],
57
        ]);
58
59
        return json_decode($result->getBody()->getContents(), true)['access_token'];
60
    }
61
}
62