| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\Authorization; |
| 6 |
|
| 7 |
use Facile\OpenIDClient\Exception\InvalidArgumentException; |
| 8 |
use Override; |
| 9 |
|
| 10 |
use function array_diff; |
| 11 |
use function array_diff_key; |
| 12 |
use function array_flip; |
| 13 |
use function array_keys; |
| 14 |
use function array_merge; |
| 15 |
use function count; |
| 16 |
use function implode; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
final class AuthRequest implements AuthRequestInterface |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private array $params; |
| 29 |
|
| 30 |
|
| 31 |
private static array $requiredKeys = [ |
| 32 |
'client_id', |
| 33 |
'redirect_uri', |
| 34 |
]; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
string $clientId, |
| 41 |
string $redirectUri, |
| 42 |
array $params = [] |
| 43 |
) { |
| 44 |
$defaults = [ |
| 45 |
'scope' => 'openid', |
| 46 |
'response_type' => 'code', |
| 47 |
'response_mode' => 'query', |
| 48 |
]; |
| 49 |
|
| 50 |
$merged = array_merge($defaults, $params); |
| 51 |
|
| 52 |
$merged['client_id'] = $clientId; |
| 53 |
$merged['redirect_uri'] = $redirectUri; |
| 54 |
|
| 55 |
$this->params = $merged; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function fromParams(array $params): self |
| 68 |
{ |
| 69 |
$missingKeys = array_diff(self::$requiredKeys, array_keys($params)); |
| 70 |
if (0 !== count($missingKeys)) { |
| 71 |
throw new InvalidArgumentException(implode(', ', $missingKeys) . ' keys not provided'); |
| 72 |
} |
| 73 |
|
| 74 |
return new static( |
| 75 |
$params['client_id'], |
| 76 |
$params['redirect_uri'], |
| 77 |
$params |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[Override] |
| 85 |
public function getScope(): string |
| 86 |
{ |
| 87 |
return $this->params['scope']; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
#[Override] |
| 96 |
public function getResponseType(): string |
| 97 |
{ |
| 98 |
return $this->params['response_type']; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#[Override] |
| 105 |
public function getClientId(): string |
| 106 |
{ |
| 107 |
return $this->params['client_id']; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#[Override] |
| 114 |
public function getRedirectUri(): string |
| 115 |
{ |
| 116 |
return $this->params['redirect_uri']; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#[Override] |
| 123 |
public function getState(): ?string |
| 124 |
{ |
| 125 |
return $this->params['state'] ?? null; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
#[Override] |
| 133 |
public function getResponseMode(): ?string |
| 134 |
{ |
| 135 |
return $this->params['response_mode'] ?? null; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
#[Override] |
| 142 |
public function getNonce(): ?string |
| 143 |
{ |
| 144 |
return $this->params['nonce'] ?? null; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
#[Override] |
| 158 |
public function getDisplay(): ?string |
| 159 |
{ |
| 160 |
return $this->params['display'] ?? null; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#[Override] |
| 174 |
public function getPrompt(): ?string |
| 175 |
{ |
| 176 |
return $this->params['prompt'] ?? null; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
#[Override] |
| 184 |
public function getMaxAge(): ?int |
| 185 |
{ |
| 186 |
return $this->params['max_age'] ?? null; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
#[Override] |
| 194 |
public function getUiLocales(): ?string |
| 195 |
{ |
| 196 |
return $this->params['ui_locales'] ?? null; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[Override] |
| 204 |
public function getIdTokenHint(): ?string |
| 205 |
{ |
| 206 |
return $this->params['id_token_hint'] ?? null; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#[Override] |
| 213 |
public function getLoginHint(): ?string |
| 214 |
{ |
| 215 |
return $this->params['login_hint'] ?? null; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
#[Override] |
| 222 |
public function getAcrValues(): ?string |
| 223 |
{ |
| 224 |
return $this->params['acr_values'] ?? null; |
| 225 |
} |
| 226 |
|
| 227 |
#[Override] |
| 228 |
public function getRequest(): ?string |
| 229 |
{ |
| 230 |
return $this->params['request'] ?? null; |
| 231 |
} |
| 232 |
|
| 233 |
#[Override] |
| 234 |
public function getCodeChallenge(): ?string |
| 235 |
{ |
| 236 |
return $this->params['code_challenge'] ?? null; |
| 237 |
} |
| 238 |
|
| 239 |
#[Override] |
| 240 |
public function getCodeChallengeMethod(): ?string |
| 241 |
{ |
| 242 |
return $this->params['code_challenge_method'] ?? null; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
#[Override] |
| 251 |
public function withParams(array $params): AuthRequestInterface |
| 252 |
{ |
| 253 |
$instance = clone $this; |
| 254 |
|
| 255 |
$params = array_merge($instance->params, $params); |
| 256 |
|
| 257 |
$instance->params = $params; |
| 258 |
|
| 259 |
if (0 === count(array_diff_key($instance->params, array_flip(self::$requiredKeys)))) { |
| 260 |
throw new InvalidArgumentException(implode(', ', self::$requiredKeys) . ' should be provided'); |
| 261 |
} |
| 262 |
|
| 263 |
return $instance; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
#[Override] |
| 272 |
public function createParams(): array |
| 273 |
{ |
| 274 |
return $this->params; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
#[Override] |
| 281 |
public function jsonSerialize(): array |
| 282 |
{ |
| 283 |
return $this->createParams(); |
| 284 |
} |
| 285 |
} |