Passed
Push — master ( 75f5d2...2ea80c )
by Bukashk0zzz
02:37
created

JWTGenerator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 14 2
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Service;
4
5
use Firebase\JWT\JWT;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Class JWTGenerator
10
 */
11
class JWTGenerator
12
{
13
    /**
14
     * Create JWT token used by Atlassian REST API request
15
     *
16
     * @param RequestInterface $request
17
     * @param string           $issuer  Key of the add-on
18
     * @param string           $secret  Shared secret of the Tenant
19
     * @param null|string      $user
20
     *
21
     * @return string
22
     */
23
    public static function generate(RequestInterface $request, string $issuer, string $secret, ?string $user): string
24
    {
25
        $data = [
26
            'iss' => $issuer,
27
            'iat' => \time(),
28
            'exp' => \strtotime('+1 day'),
29
            'qsh' => QSHGenerator::generate((string) $request->getUri(), $request->getMethod()),
30
        ];
31
32
        if ($user !== null) {
33
            $data['sub'] = $user;
34
        }
35
36
        return JWT::encode($data, $secret);
37
    }
38
}
39