Passed
Push — master ( f88833...ddd52d )
by Thomas Mauro
03:50
created

ClientSecretJwt::getJwsBuilder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 3
cts 6
cp 0.5
crap 4.125
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient\AuthMethod;
6
7
use Jose\Component\Core\AlgorithmManager;
8
use Jose\Component\Signature\Algorithm\HS256;
9
use Jose\Component\Signature\JWSBuilder;
10
use Jose\Component\Signature\Serializer\CompactSerializer;
11
use Jose\Component\Signature\Serializer\Serializer;
12
use TMV\OpenIdClient\ClientInterface as OpenIDClient;
13
use TMV\OpenIdClient\Exception\InvalidArgumentException;
14
use TMV\OpenIdClient\Exception\LogicException;
15
use function TMV\OpenIdClient\jose_secret_key;
16
17
final class ClientSecretJwt extends AbstractJwtAuth
18
{
19
    /** @var null|JWSBuilder */
20
    private $jwsBuilder;
21
22
    /** @var Serializer */
23
    private $jwsSerializer;
24
25
    /**
26
     * ClientSecretJwt constructor.
27
     *
28
     * @param null|JWSBuilder $jwsBuilder
29
     * @param null|Serializer $jwsSerializer
30
     */
31 2
    public function __construct(
32
        ?JWSBuilder $jwsBuilder = null,
33
        ?Serializer $jwsSerializer = null
34
    ) {
35 2
        $this->jwsBuilder = $jwsBuilder;
36 2
        $this->jwsSerializer = $jwsSerializer ?: new CompactSerializer();
37 2
    }
38
39 1
    public function getSupportedMethod(): string
40
    {
41 1
        return 'client_secret_jwt';
42
    }
43
44 1
    private function getJwsBuilder(): JWSBuilder
45
    {
46 1
        if ($this->jwsBuilder) {
47 1
            return $this->jwsBuilder;
48
        }
49
50
        if (! \class_exists(HS256::class)) {
51
            throw new LogicException('To use the client_secret_jwt auth method you should install web-token/jwt-signature-algorithm-hmac package');
52
        }
53
54
        return $this->jwsBuilder = new JWSBuilder(new AlgorithmManager([new HS256()]));
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 1
        $clientSecret = $client->getMetadata()->getClientSecret();
64
65 1
        if (! $clientSecret) {
66
            throw new InvalidArgumentException($this->getSupportedMethod() . ' cannot be used without client_secret metadata');
67
        }
68
69 1
        $jwk = jose_secret_key($clientSecret);
70
71 1
        $time = \time();
72 1
        $jti = \bin2hex(\random_bytes(32));
73
74
        /** @var string $payload */
75 1
        $payload = \json_encode(\array_merge(
76 1
            $claims,
77
            [
78 1
                'iss' => $clientId,
79 1
                'sub' => $clientId,
80 1
                'aud' => $issuerMetadata->getIssuer(),
81 1
                'iat' => $time,
82 1
                'exp' => $time + 60,
83 1
                'jti' => $jti,
84
            ]
85
        ));
86
87 1
        $jws = $this->getJwsBuilder()->create()
88 1
            ->withPayload($payload)
89 1
            ->addSignature($jwk, ['alg' => 'HS256', 'jti' => $jti])
90 1
            ->build();
91
92 1
        return $this->jwsSerializer->serialize($jws, 0);
93
    }
94
}
95