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 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 |
|
if (! $jwsBuilder && ! \class_exists(HS256::class)) { |
36
|
|
|
throw new LogicException('To use the client_secret_jwt auth method you should install web-token/jwt-signature-algorithm-hmac package'); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
$this->jwsBuilder = $jwsBuilder ?: new JWSBuilder(new AlgorithmManager([new HS256()])); |
40
|
2 |
|
$this->jwsSerializer = $jwsSerializer ?: new CompactSerializer(); |
41
|
2 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function getSupportedMethod(): string |
44
|
|
|
{ |
45
|
1 |
|
return 'client_secret_jwt'; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
protected function createAuthJwt(OpenIDClient $client, array $claims = []): string |
49
|
|
|
{ |
50
|
1 |
|
$issuer = $client->getIssuer(); |
51
|
1 |
|
$issuerMetadata = $issuer->getMetadata(); |
52
|
|
|
|
53
|
1 |
|
$clientId = $client->getMetadata()->getClientId(); |
54
|
1 |
|
$clientSecret = $client->getMetadata()->getClientSecret(); |
55
|
|
|
|
56
|
1 |
|
if (! $clientSecret) { |
57
|
|
|
throw new InvalidArgumentException($this->getSupportedMethod() . ' cannot be used without client_secret metadata'); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$jwk = jose_secret_key($clientSecret); |
61
|
|
|
|
62
|
1 |
|
$time = \time(); |
63
|
1 |
|
$jti = \bin2hex(\random_bytes(32)); |
64
|
|
|
|
65
|
|
|
/** @var string $payload */ |
66
|
1 |
|
$payload = \json_encode(\array_merge( |
67
|
1 |
|
$claims, |
68
|
|
|
[ |
69
|
1 |
|
'iss' => $clientId, |
70
|
1 |
|
'sub' => $clientId, |
71
|
1 |
|
'aud' => $issuerMetadata->getIssuer(), |
72
|
1 |
|
'iat' => $time, |
73
|
1 |
|
'exp' => $time + 60, |
74
|
1 |
|
'jti' => $jti, |
75
|
|
|
] |
76
|
|
|
)); |
77
|
|
|
|
78
|
1 |
|
$jws = $this->jwsBuilder->create() |
79
|
1 |
|
->withPayload($payload) |
80
|
1 |
|
->addSignature($jwk, ['alg' => 'HS256', 'jti' => $jti]) |
81
|
1 |
|
->build(); |
82
|
|
|
|
83
|
1 |
|
return $this->jwsSerializer->serialize($jws, 0); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|