|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Server\TokenEndpoint\AuthenticationMethod; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Client; |
|
17
|
|
|
use OAuth2Framework\Component\Server\Core\Client\ClientId; |
|
18
|
|
|
use OAuth2Framework\Component\Server\Core\DataBag\DataBag; |
|
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class ClientSecretPost implements AuthenticationMethod |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
private $secretLifetime; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* ClientSecretPost constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param int $secretLifetime |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(int $secretLifetime = 0) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($secretLifetime < 0) { |
|
36
|
|
|
throw new \InvalidArgumentException('The secret lifetime must be at least 0 (= unlimited).'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->secretLifetime = $secretLifetime; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getSchemesParameters(): array |
|
46
|
|
|
{ |
|
47
|
|
|
return []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function findClientIdAndCredentials(ServerRequestInterface $request, &$clientCredentials = null): ? ClientId |
|
54
|
|
|
{ |
|
55
|
|
|
$parameters = $request->getParsedBody() ?? []; |
|
56
|
|
|
if (array_key_exists('client_id', $parameters) && array_key_exists('client_secret', $parameters)) { |
|
57
|
|
|
$clientCredentials = $parameters['client_secret']; |
|
58
|
|
|
|
|
59
|
|
|
return ClientId::create($parameters['client_id']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function checkClientConfiguration(DataBag $command_parameters, DataBag $validated_parameters): DataBag |
|
69
|
|
|
{ |
|
70
|
|
|
$validated_parameters = $validated_parameters->with('client_secret', $this->createClientSecret()); |
|
71
|
|
|
$validated_parameters = $validated_parameters->with('client_secret_expires_at', (0 === $this->secretLifetime ? 0 : time() + $this->secretLifetime)); |
|
72
|
|
|
|
|
73
|
|
|
return $validated_parameters; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function isClientAuthenticated(Client $client, $clientCredentials, ServerRequestInterface $request): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return hash_equals($client->get('client_secret'), $clientCredentials); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getSupportedMethods(): array |
|
88
|
|
|
{ |
|
89
|
|
|
return ['client_secret_post']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
private function createClientSecret(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return bin2hex(random_bytes(128)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|