Passed
Push — master ( 0bb0aa...139b02 )
by Thomas Mauro
02:54
created

PrivateKeyJwt::createAuthJwt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3.0008

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 35
ccs 21
cts 22
cp 0.9545
rs 9.584
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3.0008
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient\AuthMethod;
6
7
use Jose\Component\Signature\JWSBuilder;
8
use Jose\Component\Signature\Serializer\Serializer;
9
use Psr\Http\Message\StreamFactoryInterface;
10
use Ramsey\Uuid\Uuid;
11
use TMV\OpenIdClient\ClientInterface as OpenIDClient;
12
use TMV\OpenIdClient\Exception\RuntimeException;
13
14
final class PrivateKeyJwt extends AbstractJwtAuth
15
{
16
    /** @var JWSBuilder */
17
    private $jwsBuilder;
18
19
    /** @var Serializer */
20
    private $jwsSerializer;
21
22
    /** @var null|string */
23
    private $kid;
24
25
    /** @var int */
26
    private $tokenTTL;
27
28
    /**
29
     * PrivateKeyJwt constructor.
30
     *
31
     * @param JWSBuilder $jwsBuilder
32
     * @param Serializer $serializer
33
     * @param string|null $kid
34
     * @param int $tokenTTL
35
     * @param null|StreamFactoryInterface $streamFactory
36
     */
37 2
    public function __construct(
38
        JWSBuilder $jwsBuilder,
39
        Serializer $serializer,
40
        ?string $kid = null,
41
        int $tokenTTL = 60,
42
        ?StreamFactoryInterface $streamFactory = null
43
    ) {
44 2
        parent::__construct($streamFactory);
45
46 2
        $this->jwsBuilder = $jwsBuilder;
47 2
        $this->jwsSerializer = $serializer;
48 2
        $this->kid = $kid;
49 2
        $this->tokenTTL = $tokenTTL;
50 2
    }
51
52 1
    public function getSupportedMethod(): string
53
    {
54 1
        return 'private_key_jwt';
55
    }
56
57 1
    protected function createAuthJwt(OpenIDClient $client, array $claims = []): string
58
    {
59 1
        $issuer = $client->getIssuer();
60 1
        $issuerMetadata = $issuer->getMetadata();
61
62 1
        $clientId = $client->getMetadata()->getClientId();
63
64 1
        $jwk = $client->getJWKS()->selectKey('sig', null, $this->kid ? ['kid' => $this->kid] : []);
65
66 1
        if (! $jwk) {
67
            throw new RuntimeException('Unable to get a client signature jwk');
68
        }
69
70 1
        $time = \time();
71 1
        $jti = Uuid::uuid4()->toString();
72
73
        /** @var string $payload */
74 1
        $payload = \json_encode(\array_merge(
75 1
            $claims,
76
            [
77 1
                'iss' => $clientId,
78 1
                'sub' => $clientId,
79 1
                'aud' => $issuerMetadata->getIssuer(),
80 1
                'iat' => $time,
81 1
                'exp' => $time + $this->tokenTTL,
82 1
                'jti' => $jti,
83
            ]
84
        ));
85
86 1
        $jws = $this->jwsBuilder->create()
87 1
            ->withPayload($payload)
88 1
            ->addSignature($jwk, ['alg' => $jwk->get('alg'), 'jti' => $jti])
89 1
            ->build();
90
91 1
        return $this->jwsSerializer->serialize($jws, 0);
92
    }
93
}
94