| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\MyYoast_Client\Domain; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use SensitiveParameter; |
| 7 |
|
| 8 |
/** |
| 9 |
* Immutable value object representing the result of Dynamic Client Registration. |
| 10 |
* |
| 11 |
* Stores the client_id, registration access token, and management endpoint URI |
| 12 |
* received from the authorization server during DCR (RFC 7591). |
| 13 |
*/ |
| 14 |
class Registered_Client { |
| 15 |
|
| 16 |
/** |
| 17 |
* The registered OAuth client ID. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $client_id; |
| 22 |
|
| 23 |
/** |
| 24 |
* The registration access token for RFC 7592 management operations. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $registration_access_token; |
| 29 |
|
| 30 |
/** |
| 31 |
* The management endpoint URL for this client registration. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $registration_client_uri; |
| 36 |
|
| 37 |
/** |
| 38 |
* Additional metadata from the registration response. |
| 39 |
* |
| 40 |
* @var array<string, string|array<string>> |
| 41 |
*/ |
| 42 |
private $metadata; |
| 43 |
|
| 44 |
/** |
| 45 |
* The redirect URIs that have completed the authorization-code flow on this site. |
| 46 |
* |
| 47 |
* Stored alongside the client (rather than in $metadata, which is reserved for standardized |
| 48 |
* authorization-server metadata) so the validation state is invalidated automatically whenever |
| 49 |
* the registration is removed or replaced. |
| 50 |
* |
| 51 |
* @var string[] |
| 52 |
*/ |
| 53 |
private $validated_uris; |
| 54 |
|
| 55 |
/** |
| 56 |
* Registered_Client constructor. |
| 57 |
* |
| 58 |
* @param string $client_id The registered client ID. |
| 59 |
* @param string $registration_access_token The registration access token. |
| 60 |
* @param string $registration_client_uri The management endpoint URL. |
| 61 |
* @param array<string, string|array<string>> $metadata Additional metadata from the registration response. |
| 62 |
* @param string[] $validated_uris Redirect URIs that have completed the auth-code flow. |
| 63 |
* |
| 64 |
* @throws InvalidArgumentException If client_id is empty. |
| 65 |
*/ |
| 66 |
public function __construct( |
| 67 |
string $client_id, |
| 68 |
// phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPNativeAttributeFound -- No-op on PHP < 8.2; redacts parameter from stack traces on PHP 8.2+. |
| 69 |
#[SensitiveParameter] |
| 70 |
string $registration_access_token, |
| 71 |
string $registration_client_uri, |
| 72 |
array $metadata = [], |
| 73 |
array $validated_uris = [] |
| 74 |
) { |
| 75 |
if ( $client_id === '' ) { |
| 76 |
throw new InvalidArgumentException( 'Registered_Client requires a non-empty client_id.' ); |
| 77 |
} |
| 78 |
|
| 79 |
$this->client_id = $client_id; |
| 80 |
$this->registration_access_token = $registration_access_token; |
| 81 |
$this->registration_client_uri = $registration_client_uri; |
| 82 |
$this->metadata = $metadata; |
| 83 |
$this->validated_uris = \array_values( $validated_uris ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the registered client ID. |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
public function get_client_id(): string { |
| 92 |
return $this->client_id; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the registration access token. |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function get_registration_access_token(): string { |
| 101 |
return $this->registration_access_token; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns the management endpoint URL. |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function get_registration_client_uri(): string { |
| 110 |
return $this->registration_client_uri; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns additional metadata from the registration response. |
| 115 |
* |
| 116 |
* @return array<string, string|array<string>> |
| 117 |
*/ |
| 118 |
public function get_metadata(): array { |
| 119 |
return $this->metadata; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the redirect URIs this client is registered with. |
| 124 |
* |
| 125 |
* @return string[] |
| 126 |
*/ |
| 127 |
public function get_redirect_uris(): array { |
| 128 |
$redirect_uris = ( $this->metadata['redirect_uris'] ?? [] ); |
| 129 |
|
| 130 |
return ( \is_array( $redirect_uris ) ) ? \array_values( $redirect_uris ) : []; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Whether this client's registered redirect URIs exactly match the given set (order-insensitive), |
| 135 |
* so both additions and removals count as a mismatch. |
| 136 |
* |
| 137 |
* @param string[] $redirect_uris The redirect-URI set to compare against. |
| 138 |
* |
| 139 |
* @return bool |
| 140 |
*/ |
| 141 |
public function has_redirect_uris( array $redirect_uris ): bool { |
| 142 |
$wanted = \array_unique( $redirect_uris ); |
| 143 |
$stored = \array_unique( $this->get_redirect_uris() ); |
| 144 |
\sort( $wanted ); |
| 145 |
\sort( $stored ); |
| 146 |
|
| 147 |
return $wanted === $stored; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns the redirect URIs that have completed the authorization-code flow on this site. |
| 152 |
* |
| 153 |
* @return string[] |
| 154 |
*/ |
| 155 |
public function get_validated_uris(): array { |
| 156 |
return $this->validated_uris; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Whether the given redirect URI has completed the authorization-code flow on this site. |
| 161 |
* |
| 162 |
* @param string $redirect_uri The redirect URI to check. |
| 163 |
* |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
public function is_uri_validated( string $redirect_uri ): bool { |
| 167 |
return \in_array( $redirect_uri, $this->validated_uris, true ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Returns a copy of this client with its validated redirect URIs replaced. |
| 172 |
* |
| 173 |
* @param string[] $validated_uris The redirect URIs that have completed the auth-code flow. |
| 174 |
* |
| 175 |
* @return self |
| 176 |
*/ |
| 177 |
public function with_validated_uris( array $validated_uris ): self { |
| 178 |
return new self( |
| 179 |
$this->client_id, |
| 180 |
$this->registration_access_token, |
| 181 |
$this->registration_client_uri, |
| 182 |
$this->metadata, |
| 183 |
$validated_uris, |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Converts the DTO to an associative array for storage. |
| 189 |
* |
| 190 |
* @return array<string, string|array<string>> |
| 191 |
*/ |
| 192 |
public function to_array(): array { |
| 193 |
return [ |
| 194 |
'client_id' => $this->client_id, |
| 195 |
'registration_access_token' => $this->registration_access_token, |
| 196 |
'registration_client_uri' => $this->registration_client_uri, |
| 197 |
'metadata' => $this->metadata, |
| 198 |
'validated_uris' => $this->validated_uris, |
| 199 |
]; |
| 200 |
} |
| 201 |
} |
| 202 |
|