WebAuthnRecoveryControllerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Extensions\WebauthnCredentialBroker;
  4. use App\Http\Controllers\Auth\WebAuthnRecoveryController;
  5. use App\Http\Requests\WebauthnRecoveryRequest;
  6. use App\Models\User;
  7. use App\Providers\AuthServiceProvider;
  8. use Database\Factories\UserFactory;
  9. use Illuminate\Support\Facades\Date;
  10. use Illuminate\Support\Facades\DB;
  11. use PHPUnit\Framework\Attributes\CoversClass;
  12. use Tests\FeatureTestCase;
  13. /**
  14. * WebAuthnRecoveryControllerTest test class
  15. */
  16. #[CoversClass(WebAuthnRecoveryController::class)]
  17. #[CoversClass(WebauthnCredentialBroker::class)]
  18. #[CoversClass(WebauthnRecoveryRequest::class)]
  19. #[CoversClass(AuthServiceProvider::class)]
  20. class WebAuthnRecoveryControllerTest extends FeatureTestCase
  21. {
  22. /**
  23. * @var \App\Models\User
  24. */
  25. protected $user;
  26. protected $now;
  27. const STORED_TOKEN_VALUE = '$2y$10$P6q8rl8te5QaO1EdpyJcNO0s9VFlVgf62KaItQhrPTskxfyu97mlW';
  28. const ACTUAL_TOKEN_VALUE = '9e583e3fb6c32034164ac62415c9657dcbd1fb861b434340b08a94c2075cac66';
  29. const CREDENTIAL_ID = '-VOLFKPY-_FuMI_sJ7gMllK76L3VoRUINj6lL_Z3qDg';
  30. /**
  31. * @test
  32. */
  33. public function setUp() : void
  34. {
  35. parent::setUp();
  36. $this->user = User::factory()->create();
  37. Date::setTestNow($this->now = Date::create(2022, 11, 16, 9, 4));
  38. DB::table(config('auth.passwords.webauthn.table'))->insert([
  39. 'email' => $this->user->email,
  40. 'token' => self::STORED_TOKEN_VALUE,
  41. 'created_at' => $this->now->toDateTimeString(),
  42. ]);
  43. }
  44. /**
  45. * @test
  46. */
  47. public function test_recover_fails_if_no_recovery_is_set()
  48. {
  49. DB::table(config('auth.passwords.webauthn.table'))->delete();
  50. $this->json('POST', '/webauthn/recover', [
  51. 'token' => self::ACTUAL_TOKEN_VALUE,
  52. 'email' => $this->user->email,
  53. 'password' => UserFactory::USER_PASSWORD,
  54. ])
  55. ->assertStatus(422)
  56. ->assertJsonValidationErrors('token');
  57. }
  58. /**
  59. * @test
  60. */
  61. public function test_recover_with_wrong_token_returns_validation_error()
  62. {
  63. $response = $this->json('POST', '/webauthn/recover', [
  64. 'token' => 'wrong_token',
  65. 'email' => $this->user->email,
  66. 'password' => UserFactory::USER_PASSWORD,
  67. ])
  68. ->assertStatus(422)
  69. ->assertJsonMissingValidationErrors('email')
  70. ->assertJsonValidationErrors('token');
  71. }
  72. /**
  73. * @test
  74. */
  75. public function test_recover_with_expired_token_returns_validation_error()
  76. {
  77. Date::setTestNow($now = Date::create(2020, 01, 01, 16, 30));
  78. DB::table(config('auth.passwords.webauthn.table'))->delete();
  79. DB::table(config('auth.passwords.webauthn.table'))->insert([
  80. 'token' => self::STORED_TOKEN_VALUE,
  81. 'email' => $this->user->email,
  82. 'created_at' => $now->clone()->subHour()->subSecond()->toDateTimeString(),
  83. ]);
  84. $this->json('POST', '/webauthn/recover', [
  85. 'token' => self::ACTUAL_TOKEN_VALUE,
  86. 'email' => $this->user->email,
  87. 'password' => UserFactory::USER_PASSWORD,
  88. ])
  89. ->assertStatus(422)
  90. ->assertJsonValidationErrors('token');
  91. }
  92. /**
  93. * @test
  94. */
  95. public function test_recover_with_invalid_password_returns_authentication_error()
  96. {
  97. $this->json('POST', '/webauthn/recover', [
  98. 'token' => self::ACTUAL_TOKEN_VALUE,
  99. 'email' => $this->user->email,
  100. 'password' => 'bad_password',
  101. ])
  102. ->assertStatus(401);
  103. }
  104. /**
  105. * @test
  106. */
  107. public function test_recover_returns_validation_error_when_no_user_exists()
  108. {
  109. $this->json('POST', '/webauthn/recover', [
  110. 'token' => self::ACTUAL_TOKEN_VALUE,
  111. 'email' => 'no@user.com',
  112. 'password' => UserFactory::USER_PASSWORD,
  113. ])
  114. ->assertStatus(422)
  115. ->assertJsonMissingValidationErrors('password')
  116. ->assertJsonMissingValidationErrors('token')
  117. ->assertJsonValidationErrors('email');
  118. }
  119. /**
  120. * @test
  121. */
  122. public function test_recover_returns_success()
  123. {
  124. $response = $this->json('POST', '/webauthn/recover', [
  125. 'token' => self::ACTUAL_TOKEN_VALUE,
  126. 'email' => $this->user->email,
  127. 'password' => UserFactory::USER_PASSWORD,
  128. ])
  129. ->assertStatus(200);
  130. $this->assertDatabaseMissing(config('auth.passwords.webauthn.table'), [
  131. 'token' => self::STORED_TOKEN_VALUE,
  132. ]);
  133. }
  134. /**
  135. * @test
  136. */
  137. public function test_recover_resets_useWebauthnOnly_user_preference()
  138. {
  139. $this->user['preferences->useWebauthnOnly'] = true;
  140. $this->user->save();
  141. $response = $this->json('POST', '/webauthn/recover', [
  142. 'token' => self::ACTUAL_TOKEN_VALUE,
  143. 'email' => $this->user->email,
  144. 'password' => UserFactory::USER_PASSWORD,
  145. ])
  146. ->assertStatus(200);
  147. $this->user->refresh();
  148. $this->assertFalse($this->user->preferences['useWebauthnOnly']);
  149. }
  150. /**
  151. * @test
  152. */
  153. public function test_revoke_all_credentials_clear_registered_credentials()
  154. {
  155. DB::table('webauthn_credentials')->insert([
  156. 'id' => self::CREDENTIAL_ID,
  157. 'authenticatable_type' => \App\Models\User::class,
  158. 'authenticatable_id' => $this->user->id,
  159. 'user_id' => 'e8af6f703f8042aa91c30cf72289aa07',
  160. 'counter' => 0,
  161. 'rp_id' => 'http://localhost',
  162. 'origin' => 'http://localhost',
  163. 'aaguid' => '00000000-0000-0000-0000-000000000000',
  164. 'attestation_format' => 'none',
  165. 'public_key' => 'eyJpdiI6Imp0U0NVeFNNbW45KzEvMXpad2p2SUE9PSIsInZhbHVlIjoic0VxZ2I1WnlHM2lJakhkWHVkK2kzMWtibk1IN2ZlaExGT01qOElXMDdRTjhnVlR0TDgwOHk1S0xQUy9BQ1JCWHRLNzRtenNsMml1dVQydWtERjFEU0h0bkJGT2RwUXE1M1JCcVpablE2Y2VGV2YvVEE2RGFIRUE5L0x1K0JIQXhLVE1aNVNmN3AxeHdjRUo2V0hwREZSRTJYaThNNnB1VnozMlVXZEVPajhBL3d3ODlkTVN3bW54RTEwSG0ybzRQZFFNNEFrVytUYThub2IvMFRtUlBZamoyZElWKzR1bStZQ1IwU3FXbkYvSm1FU2FlMTFXYUo0SG9kc1BDME9CNUNKeE9IelE5d2dmNFNJRXBKNUdlVzJ3VHUrQWJZRFluK0hib0xvVTdWQ0ZISjZmOWF3by83aVJES1dxbU9Zd1lhRTlLVmhZSUdlWmlBOUFtcTM2ZVBaRWNKNEFSQUhENk5EaC9hN3REdnVFbm16WkRxekRWOXd4cVcvZFdKa2tlWWJqZWlmZnZLS0F1VEVCZEZQcXJkTExiNWRyQmxsZWtaSDRlT3VVS0ZBSXFBRG1JMjRUMnBKRXZxOUFUa2xxMjg2TEplUzdscVo2UytoVU5SdXk1OE1lcFN6aU05ZkVXTkdIM2tKM3Q5bmx1TGtYb1F5bGxxQVR3K3BVUVlia1VybDFKRm9lZDViNzYraGJRdmtUb2FNTEVGZmZYZ3lYRDRiOUVjRnJpcTVvWVExOHJHSTJpMnVBZ3E0TmljbUlKUUtXY2lSWDh1dE5MVDNRUzVRSkQrTjVJUU8rSGhpeFhRRjJvSEdQYjBoVT0iLCJtYWMiOiI5MTdmNWRkZGE5OTEwNzQ3MjhkYWVhYjRlNjk0MWZlMmI5OTQ4YzlmZWI1M2I4OGVkMjE1MjMxNjUwOWRmZTU2IiwidGFnIjoiIn0=',
  166. 'updated_at' => now(),
  167. 'created_at' => now(),
  168. ]);
  169. $response = $this->json('POST', '/webauthn/recover', [
  170. 'token' => self::ACTUAL_TOKEN_VALUE,
  171. 'email' => $this->user->email,
  172. 'password' => UserFactory::USER_PASSWORD,
  173. 'revokeAll' => true,
  174. ])
  175. ->assertStatus(200);
  176. $this->assertDatabaseMissing('webauthn_credentials', [
  177. 'authenticatable_id' => $this->user->id,
  178. ]);
  179. }
  180. }