| 1 |
<?php declare(strict_types=1); |
| 2 |
namespace Duo\Tests; |
| 3 |
|
| 4 |
use \Firebase\JWT\JWT; |
| 5 |
use \Firebase\JWT\Key; |
| 6 |
use Duo\DuoUniversal\Client; |
| 7 |
use Duo\DuoUniversal\DuoException; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
|
| 10 |
final class ClientTest extends TestCase |
| 11 |
{ |
| 12 |
public $username = "user"; |
| 13 |
public $bad_username = "baduser"; |
| 14 |
public $code = "abcdefghijkl"; |
| 15 |
public $bad_expiration = 1234567; |
| 16 |
public $nonce = "deadbeefdeadbeefdeadbeef"; |
| 17 |
public $bad_nonce = "beefdeadbeefdeadbeef"; |
| 18 |
public $client_id = "12345678901234567890"; |
| 19 |
public $client_secret = "1234567890123456789012345678901234567890"; |
| 20 |
public $api_host = "api-123456.duo.com"; |
| 21 |
public $redirect_url = "https://redirect_example.com"; |
| 22 |
public $url_enc_redirect_url = "https%3A%2F%2Fredirect_example.com"; |
| 23 |
public $bad_client_id = "1234567890123456789"; |
| 24 |
public $long_client_secret = "1234567890123456789012345678901234567890000"; |
| 25 |
public $bad_client_secret = "1111111111111111111111111111111111111111"; |
| 26 |
public $bad_api_host = 123456; |
| 27 |
public $good_http_request = ["response" => ["timestamp" => 1607009339], |
| 28 |
"stat" => "OK"]; |
| 29 |
public $bad_http_request = ["message" => "invalid_client", |
| 30 |
"code" => 40002, |
| 31 |
"timestamp" => 1607014550, |
| 32 |
"message_detail" => "Failed to verify signature.", |
| 33 |
"stat" => "FAIL"]; |
| 34 |
public $missing_stat_health_check = ["response" => ["timestamp" => 1607009339]]; |
| 35 |
public $missing_message_health_check = ["stat" => "Fail"]; |
| 36 |
public $good_state = "deadbeefdeadbeefdeadbeefdeadbeefdead"; |
| 37 |
public $short_state = "deadbeefdeadbeefdeadb"; |
| 38 |
public $bad_http_request_exception = "invalid_client: Failed to verify signature."; |
| 39 |
public $expected_good_http_request = array("response" => array("timestamp" => 1607009339), |
| 40 |
"stat" => "OK"); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private function getPaddedSecret(): string |
| 50 |
{ |
| 51 |
return str_pad($this->client_secret, Client::HS512_MIN_KEY_LENGTH, "\0"); |
| 52 |
} |
| 53 |
|
| 54 |
protected function setUp(): void |
| 55 |
{ |
| 56 |
|
| 57 |
JWT::$timestamp = null; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function createGoodClient(): Client |
| 64 |
{ |
| 65 |
return new Client( |
| 66 |
$this->client_id, |
| 67 |
$this->client_secret, |
| 68 |
$this->api_host, |
| 69 |
$this->redirect_url |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function createClientMockHttp(array $result, string $bad_client_secret = '') |
| 80 |
{ |
| 81 |
$client_secret = $bad_client_secret ? $bad_client_secret : $this->client_secret; |
| 82 |
$client = $this->getMockBuilder(Client::class) |
| 83 |
->setConstructorArgs([$this->client_id, $client_secret, $this->api_host, $this->redirect_url]) |
| 84 |
->setMethods(['makeHttpsCall']) |
| 85 |
->getMock(); |
| 86 |
$client->method('makeHttpsCall') |
| 87 |
->will($this->returnValue($result)); |
| 88 |
return $client; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function createIdToken(?string $remove_index = null, array $change_val = []): string |
| 100 |
{ |
| 101 |
$date = new \DateTime(); |
| 102 |
$current_date = $date->getTimestamp(); |
| 103 |
$payload = ["exp" => $current_date + Client::JWT_EXPIRATION, |
| 104 |
"iat" => $current_date, |
| 105 |
"iss" => "https://" . $this->api_host . Client::TOKEN_ENDPOINT, |
| 106 |
"aud" => $this->client_id, |
| 107 |
"preferred_username" => $this->username, |
| 108 |
"nonce" => $this->nonce |
| 109 |
]; |
| 110 |
if ($remove_index) { |
| 111 |
unset($payload[$remove_index]); |
| 112 |
} |
| 113 |
if ($change_val) { |
| 114 |
$payload[key($change_val)] = $change_val[key($change_val)]; |
| 115 |
} |
| 116 |
return JWT::encode($payload, $this->getPaddedSecret(), Client::SIG_ALGORITHM); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function createTokenResult(string $id_token = ''): array |
| 127 |
{ |
| 128 |
if (!$id_token) { |
| 129 |
$id_token = $this->createIdToken(); |
| 130 |
} |
| 131 |
return ["id_token" => $id_token, |
| 132 |
"access_token" => "90101112", |
| 133 |
"expires_in" => "1234567890", |
| 134 |
"token_type" => "Bearer"]; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function testPaddedSecretMatchesImplementation(): void |
| 142 |
{ |
| 143 |
$client = $this->createGoodClient(); |
| 144 |
|
| 145 |
|
| 146 |
$auth_url = $client->createAuthUrl($this->username, $this->good_state); |
| 147 |
|
| 148 |
|
| 149 |
$query_str = parse_url($auth_url, PHP_URL_QUERY); |
| 150 |
parse_str($query_str, $query_params); |
| 151 |
$token = $query_params["request"]; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
$jwt_key = new Key($this->getPaddedSecret(), Client::SIG_ALGORITHM); |
| 156 |
$decoded = JWT::decode($token, $jwt_key); |
| 157 |
|
| 158 |
|
| 159 |
$this->assertEquals($this->username, $decoded->duo_uname); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function testClientGood(): void |
| 166 |
{ |
| 167 |
$client = $this->createGoodClient(); |
| 168 |
$this->assertInstanceOf(Client::class, $client); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function testClientBadClientId(): void |
| 175 |
{ |
| 176 |
$this->expectException(DuoException::class); |
| 177 |
$this->expectExceptionMessage(Client::INVALID_CLIENT_ID_ERROR); |
| 178 |
$client = new Client( |
| 179 |
$this->bad_client_id, |
| 180 |
$this->client_secret, |
| 181 |
$this->api_host, |
| 182 |
$this->redirect_url |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function testClientBadClientSecret(): void |
| 191 |
{ |
| 192 |
$this->expectException(DuoException::class); |
| 193 |
$this->expectExceptionMessage(Client::INVALID_CLIENT_SECRET_ERROR); |
| 194 |
$client = new Client( |
| 195 |
$this->client_id, |
| 196 |
$this->long_client_secret, |
| 197 |
$this->api_host, |
| 198 |
$this->redirect_url |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function testGenerateState(): void |
| 207 |
{ |
| 208 |
$client = $this->createGoodClient(); |
| 209 |
$string_1 = $client->generateState(); |
| 210 |
$this->assertNotEquals( |
| 211 |
$string_1, |
| 212 |
$client->generateState() |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function testHealthCheckGood(): void |
| 220 |
{ |
| 221 |
$client = $this->createClientMockHttp($this->good_http_request); |
| 222 |
$result = $client->healthCheck(); |
| 223 |
$this->assertEquals($this->expected_good_http_request, $result); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function testHealthCheckConnectionFail(): void |
| 230 |
{ |
| 231 |
$this->expectException(DuoException::class); |
| 232 |
$this->expectExceptionMessage(Client::FAILED_CONNECTION); |
| 233 |
$client = $this->getMockBuilder(Client::class) |
| 234 |
->setConstructorArgs([$this->client_id, $this->client_secret, $this->api_host, $this->redirect_url]) |
| 235 |
->setMethods(['makeHttpsCall']) |
| 236 |
->getMock(); |
| 237 |
$client->method('makeHttpsCall') |
| 238 |
->will($this->throwException(new DuoException(Client::FAILED_CONNECTION))); |
| 239 |
$client->healthCheck(); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function testHealthCheckBadSig(): void |
| 246 |
{ |
| 247 |
$this->expectException(DuoException::class); |
| 248 |
$this->expectExceptionMessage($this->bad_http_request_exception); |
| 249 |
$client = $this->createClientMockHttp($this->bad_http_request); |
| 250 |
$client->healthCheck(); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function testHealthCheckMissingStat(): void |
| 257 |
{ |
| 258 |
$this->expectException(DuoException::class); |
| 259 |
$this->expectExceptionMessage(Client::MALFORMED_RESPONSE); |
| 260 |
$client = $this->createClientMockHttp($this->missing_stat_health_check); |
| 261 |
$client->healthCheck(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function testHealthCheckMissingMessage(): void |
| 268 |
{ |
| 269 |
$this->expectException(DuoException::class); |
| 270 |
$this->expectExceptionMessage(Client::MALFORMED_RESPONSE); |
| 271 |
$client = $this->createClientMockHttp($this->missing_message_health_check); |
| 272 |
$client->healthCheck(); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public function testMissingResponseField($missing_field): void |
| 279 |
{ |
| 280 |
$result = $this->createTokenResult(); |
| 281 |
unset($result[$missing_field]); |
| 282 |
$this->expectException(DuoException::class); |
| 283 |
$this->expectExceptionMessage(Client::MALFORMED_RESPONSE); |
| 284 |
$client = $this->createClientMockHttp($result); |
| 285 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public function providerMissingResponseField(): array |
| 292 |
{ |
| 293 |
return [ |
| 294 |
["token_type"], |
| 295 |
["access_token"], |
| 296 |
["expires_in"], |
| 297 |
["id_token"] |
| 298 |
]; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function testTokenExchangeBadTokenType(): void |
| 304 |
{ |
| 305 |
$result_good = $this->createTokenResult(); |
| 306 |
$result = str_replace('Bearer', 'BadTokenType', $result_good); |
| 307 |
$this->expectException(DuoException::class); |
| 308 |
$this->expectExceptionMessage(Client::MALFORMED_RESPONSE); |
| 309 |
$client = $this->createClientMockHttp($result); |
| 310 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function testTokenExchangeBadNonce(): void |
| 317 |
{ |
| 318 |
$payload = $this->createIdToken("nonce"); |
| 319 |
$result = $this->createTokenResult($payload); |
| 320 |
$this->expectException(DuoException::class); |
| 321 |
$this->expectExceptionMessage(Client::NONCE_ERROR); |
| 322 |
$client = $this->createClientMockHttp($result); |
| 323 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username, $this->bad_nonce); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public function testTokenExchangeBadSig(): void |
| 330 |
{ |
| 331 |
$result = $this->createTokenResult(); |
| 332 |
$this->expectException(DuoException::class); |
| 333 |
$this->expectExceptionMessage(Client::JWT_DECODE_ERROR); |
| 334 |
$client = $this->createClientMockHttp($result, $this->bad_client_secret); |
| 335 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public function testTokenExchangeExpired(): void |
| 342 |
{ |
| 343 |
$expired = ["exp" => $this->bad_expiration]; |
| 344 |
$payload = $this->createIdToken(null, $expired); |
| 345 |
$result = $this->createTokenResult($payload); |
| 346 |
$this->expectException(DuoException::class); |
| 347 |
$this->expectExceptionMessage(Client::JWT_DECODE_ERROR); |
| 348 |
$client = $this->createClientMockHttp($result); |
| 349 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function testTokenExchangeLargeClockSkew(): void |
| 356 |
{ |
| 357 |
|
| 358 |
JWT::$timestamp = time() - Client::JWT_LEEWAY * 2; |
| 359 |
|
| 360 |
$payload = $this->createIdToken(); |
| 361 |
$result = $this->createTokenResult($payload); |
| 362 |
$this->expectException(DuoException::class); |
| 363 |
$this->expectExceptionMessage(Client::JWT_DECODE_ERROR); |
| 364 |
$client = $this->createClientMockHttp($result); |
| 365 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public function testTokenExchangeSmallClockSkew(): void |
| 372 |
{ |
| 373 |
|
| 374 |
JWT::$timestamp = time() - Client::JWT_LEEWAY / 2; |
| 375 |
|
| 376 |
$payload = $this->createIdToken(); |
| 377 |
$result = $this->createTokenResult($payload); |
| 378 |
$client = $this->createClientMockHttp($result); |
| 379 |
$token = $client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 380 |
$this->assertEquals($this->username, $token['preferred_username']); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public function testMissingField(string $missing_field, string $expected_response): void |
| 387 |
{ |
| 388 |
$payload = $this->createIdToken($missing_field); |
| 389 |
$result = $this->createTokenResult($payload); |
| 390 |
$this->expectException(DuoException::class); |
| 391 |
$this->expectExceptionMessage($expected_response); |
| 392 |
$client = $this->createClientMockHttp($result); |
| 393 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username, $this->nonce); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public function providerMissingField(): array |
| 401 |
{ |
| 402 |
return [ |
| 403 |
[ "exp", Client::MALFORMED_RESPONSE], |
| 404 |
[ "iat", Client::MALFORMED_RESPONSE], |
| 405 |
[ "iss", Client::MALFORMED_RESPONSE], |
| 406 |
[ "aud", Client::MALFORMED_RESPONSE], |
| 407 |
[ "nonce", Client::NONCE_ERROR], |
| 408 |
[ "preferred_username", Client::USERNAME_ERROR ] |
| 409 |
]; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public function testTokenExchangeBadIss(): void |
| 416 |
{ |
| 417 |
$bad_iss = ["iss" => "https://" . $this->bad_api_host . Client::TOKEN_ENDPOINT]; |
| 418 |
$payload = $this->createIdToken(null, $bad_iss); |
| 419 |
$result = $this->createTokenResult($payload); |
| 420 |
$this->expectException(DuoException::class); |
| 421 |
$this->expectExceptionMessage(Client::MALFORMED_RESPONSE); |
| 422 |
$client = $this->createClientMockHttp($result); |
| 423 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public function testTokenExchangeBadAud(): void |
| 430 |
{ |
| 431 |
$bad_aud = ["aud" => $this->bad_client_id]; |
| 432 |
$payload = $this->createIdToken(null, $bad_aud); |
| 433 |
$result = $this->createTokenResult($payload); |
| 434 |
$this->expectException(DuoException::class); |
| 435 |
$this->expectExceptionMessage(Client::MALFORMED_RESPONSE); |
| 436 |
$client = $this->createClientMockHttp($result); |
| 437 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function testTokenExchangeBadUsername(): void |
| 444 |
{ |
| 445 |
$bad_aud = ["preferred_username" => $this->bad_username]; |
| 446 |
$payload = $this->createIdToken(null, $bad_aud); |
| 447 |
$result = $this->createTokenResult($payload); |
| 448 |
$this->expectException(DuoException::class); |
| 449 |
$this->expectExceptionMessage(Client::USERNAME_ERROR); |
| 450 |
$client = $this->createClientMockHttp($result); |
| 451 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public function testTokenExchangeSuccess(): void |
| 458 |
{ |
| 459 |
$id_token = $this->createIdToken(); |
| 460 |
$result = $this->createTokenResult($id_token); |
| 461 |
$jwt_key = new Key($this->getPaddedSecret(), Client::SIG_ALGORITHM); |
| 462 |
$expected_result_obj = JWT::decode($id_token, $jwt_key); |
| 463 |
$expected_result = json_decode(json_encode($expected_result_obj), true); |
| 464 |
$client = $this->createClientMockHttp($result); |
| 465 |
$exchange_result = $client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 466 |
$this->assertEquals($expected_result, $exchange_result); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public function testCreateAuthUrlState(string $state): void |
| 473 |
{ |
| 474 |
$this->expectException(DuoException::class); |
| 475 |
$this->expectExceptionMessage(Client::DUO_STATE_ERROR); |
| 476 |
$client = $this->createGoodClient(); |
| 477 |
$client->createAuthUrl($this->username, $state); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
public function providerState(): array |
| 484 |
{ |
| 485 |
$long_state = str_repeat("a", Client::MAX_STATE_LENGTH + 1); |
| 486 |
return [ |
| 487 |
[$this->short_state], |
| 488 |
[$long_state] |
| 489 |
]; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
public function testDuoCodeDefaultTrue(): void |
| 496 |
{ |
| 497 |
$client = new Client( |
| 498 |
$this->client_id, |
| 499 |
$this->client_secret, |
| 500 |
$this->api_host, |
| 501 |
$this->redirect_url |
| 502 |
); |
| 503 |
$auth_url = $client->createAuthUrl($this->username, $this->good_state); |
| 504 |
$jwt = $this->decodeJWTFromURL($auth_url); |
| 505 |
$this->assertTrue($jwt["use_duo_code_attribute"]); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public function testDuoCodeSetFalse(): void |
| 512 |
{ |
| 513 |
$client = new Client( |
| 514 |
$this->client_id, |
| 515 |
$this->client_secret, |
| 516 |
$this->api_host, |
| 517 |
$this->redirect_url, |
| 518 |
false |
| 519 |
); |
| 520 |
$auth_url = $client->createAuthUrl($this->username, $this->good_state); |
| 521 |
$jwt = $this->decodeJWTFromURL($auth_url); |
| 522 |
$this->assertFalse($jwt["use_duo_code_attribute"]); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
public function decodeJWTFromURL(string $url): array |
| 529 |
{ |
| 530 |
$query_str = parse_url($url, PHP_URL_QUERY); |
| 531 |
parse_str($query_str, $query_params); |
| 532 |
$token = $query_params["request"]; |
| 533 |
$jwt_key = new Key($this->getPaddedSecret(), Client::SIG_ALGORITHM); |
| 534 |
$result_obj = JWT::decode($token, $jwt_key); |
| 535 |
return json_decode(json_encode($result_obj), true); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public function testCreateAuthUrlSuccess(): void |
| 542 |
{ |
| 543 |
$client = $this->createGoodClient(); |
| 544 |
$duo_uri = $client->createAuthUrl($this->username, $this->good_state); |
| 545 |
$expected_client_id = "client_id=" . $this->client_id; |
| 546 |
$expected_redir_uri = "redirect_uri=" . $this->url_enc_redirect_url; |
| 547 |
|
| 548 |
$this->assertStringContainsString($expected_client_id, $duo_uri); |
| 549 |
$this->assertStringContainsString("response_type=code", $duo_uri); |
| 550 |
$this->assertStringContainsString("scope=openid", $duo_uri); |
| 551 |
$this->assertStringContainsString($expected_redir_uri, $duo_uri); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
public function testAppendToUserAgent(): void |
| 558 |
{ |
| 559 |
$custom_extension = "MyApp/1.0.0"; |
| 560 |
$id_token = $this->createIdToken(); |
| 561 |
$result = $this->createTokenResult($id_token); |
| 562 |
|
| 563 |
|
| 564 |
$client = $this->getMockBuilder(Client::class) |
| 565 |
->setConstructorArgs([$this->client_id, $this->client_secret, $this->api_host, $this->redirect_url]) |
| 566 |
->setMethods(['makeHttpsCall']) |
| 567 |
->getMock(); |
| 568 |
|
| 569 |
|
| 570 |
$captured_user_agent = null; |
| 571 |
$client->method('makeHttpsCall') |
| 572 |
->willReturnCallback(function ($endpoint, $request, $user_agent = null) use (&$captured_user_agent, $result) { |
| 573 |
$captured_user_agent = $user_agent; |
| 574 |
return $result; |
| 575 |
}); |
| 576 |
|
| 577 |
|
| 578 |
$client->appendToUserAgent($custom_extension); |
| 579 |
|
| 580 |
|
| 581 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 582 |
|
| 583 |
|
| 584 |
$this->assertNotNull($captured_user_agent); |
| 585 |
$this->assertStringContainsString($custom_extension, $captured_user_agent); |
| 586 |
$this->assertStringContainsString(Client::USER_AGENT, $captured_user_agent); |
| 587 |
$this->assertStringContainsString("php/" . phpversion(), $captured_user_agent); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public function testUserAgentWithoutExtension(): void |
| 594 |
{ |
| 595 |
$id_token = $this->createIdToken(); |
| 596 |
$result = $this->createTokenResult($id_token); |
| 597 |
|
| 598 |
|
| 599 |
$client = $this->getMockBuilder(Client::class) |
| 600 |
->setConstructorArgs([$this->client_id, $this->client_secret, $this->api_host, $this->redirect_url]) |
| 601 |
->setMethods(['makeHttpsCall']) |
| 602 |
->getMock(); |
| 603 |
|
| 604 |
|
| 605 |
$captured_user_agent = null; |
| 606 |
$client->method('makeHttpsCall') |
| 607 |
->willReturnCallback(function ($endpoint, $request, $user_agent = null) use (&$captured_user_agent, $result) { |
| 608 |
$captured_user_agent = $user_agent; |
| 609 |
return $result; |
| 610 |
}); |
| 611 |
|
| 612 |
|
| 613 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 614 |
|
| 615 |
|
| 616 |
$this->assertNotNull($captured_user_agent); |
| 617 |
$this->assertStringContainsString(Client::USER_AGENT, $captured_user_agent); |
| 618 |
$this->assertStringContainsString("php/" . phpversion(), $captured_user_agent); |
| 619 |
$this->assertStringContainsString(php_uname(), $captured_user_agent); |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
public function testAppendToUserAgentEmpty(): void |
| 626 |
{ |
| 627 |
$id_token = $this->createIdToken(); |
| 628 |
$result = $this->createTokenResult($id_token); |
| 629 |
|
| 630 |
|
| 631 |
$client = $this->getMockBuilder(Client::class) |
| 632 |
->setConstructorArgs([$this->client_id, $this->client_secret, $this->api_host, $this->redirect_url]) |
| 633 |
->setMethods(['makeHttpsCall']) |
| 634 |
->getMock(); |
| 635 |
|
| 636 |
|
| 637 |
$captured_user_agent = null; |
| 638 |
$client->method('makeHttpsCall') |
| 639 |
->willReturnCallback(function ($endpoint, $request, $user_agent = null) use (&$captured_user_agent, $result) { |
| 640 |
$captured_user_agent = $user_agent; |
| 641 |
return $result; |
| 642 |
}); |
| 643 |
|
| 644 |
|
| 645 |
$client->appendToUserAgent(""); |
| 646 |
|
| 647 |
|
| 648 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 649 |
|
| 650 |
|
| 651 |
$this->assertNotNull($captured_user_agent); |
| 652 |
$this->assertStringContainsString(Client::USER_AGENT, $captured_user_agent); |
| 653 |
$this->assertStringContainsString("php/" . phpversion(), $captured_user_agent); |
| 654 |
$this->assertStringContainsString(php_uname(), $captured_user_agent); |
| 655 |
|
| 656 |
$expected_base = Client::USER_AGENT . " php/" . phpversion() . " " . php_uname(); |
| 657 |
$this->assertEquals($expected_base, $captured_user_agent); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
public function testAppendToUserAgentWhitespace(): void |
| 664 |
{ |
| 665 |
$id_token = $this->createIdToken(); |
| 666 |
$result = $this->createTokenResult($id_token); |
| 667 |
|
| 668 |
|
| 669 |
$client = $this->getMockBuilder(Client::class) |
| 670 |
->setConstructorArgs([$this->client_id, $this->client_secret, $this->api_host, $this->redirect_url]) |
| 671 |
->setMethods(['makeHttpsCall']) |
| 672 |
->getMock(); |
| 673 |
|
| 674 |
|
| 675 |
$captured_user_agent = null; |
| 676 |
$client->method('makeHttpsCall') |
| 677 |
->willReturnCallback(function ($endpoint, $request, $user_agent = null) use (&$captured_user_agent, $result) { |
| 678 |
$captured_user_agent = $user_agent; |
| 679 |
return $result; |
| 680 |
}); |
| 681 |
|
| 682 |
|
| 683 |
$client->appendToUserAgent(" "); |
| 684 |
|
| 685 |
|
| 686 |
$client->exchangeAuthorizationCodeFor2FAResult($this->code, $this->username); |
| 687 |
|
| 688 |
|
| 689 |
$this->assertNotNull($captured_user_agent); |
| 690 |
$expected_base = Client::USER_AGENT . " php/" . phpversion() . " " . php_uname(); |
| 691 |
$this->assertEquals($expected_base, $captured_user_agent); |
| 692 |
} |
| 693 |
} |