class-wp-recovery-mode-key-service.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Error Protection API: WP_Recovery_Mode_Key_Service class
  4. *
  5. * @package WordPress
  6. * @since 5.2.0
  7. */
  8. /**
  9. * Core class used to generate and validate keys used to enter Recovery Mode.
  10. *
  11. * @since 5.2.0
  12. */
  13. final class WP_Recovery_Mode_Key_Service {
  14. /**
  15. * The option name used to store the keys.
  16. *
  17. * @since 5.2.0
  18. * @var string
  19. */
  20. private $option_name = 'recovery_keys';
  21. /**
  22. * Creates a recovery mode token.
  23. *
  24. * @since 5.2.0
  25. *
  26. * @return string $token A random string to identify its associated key in storage.
  27. */
  28. public function generate_recovery_mode_token() {
  29. return wp_generate_password( 22, false );
  30. }
  31. /**
  32. * Creates a recovery mode key.
  33. *
  34. * @since 5.2.0
  35. *
  36. * @global PasswordHash $wp_hasher
  37. *
  38. * @param string $token A token generated by {@see generate_recovery_mode_token()}.
  39. * @return string $key Recovery mode key.
  40. */
  41. public function generate_and_store_recovery_mode_key( $token ) {
  42. global $wp_hasher;
  43. $key = wp_generate_password( 22, false );
  44. if ( empty( $wp_hasher ) ) {
  45. require_once ABSPATH . WPINC . '/class-phpass.php';
  46. $wp_hasher = new PasswordHash( 8, true );
  47. }
  48. $hashed = $wp_hasher->HashPassword( $key );
  49. $records = $this->get_keys();
  50. $records[ $token ] = array(
  51. 'hashed_key' => $hashed,
  52. 'created_at' => time(),
  53. );
  54. $this->update_keys( $records );
  55. /**
  56. * Fires when a recovery mode key is generated.
  57. *
  58. * @since 5.2.0
  59. *
  60. * @param string $token The recovery data token.
  61. * @param string $key The recovery mode key.
  62. */
  63. do_action( 'generate_recovery_mode_key', $token, $key );
  64. return $key;
  65. }
  66. /**
  67. * Verifies if the recovery mode key is correct.
  68. *
  69. * Recovery mode keys can only be used once; the key will be consumed in the process.
  70. *
  71. * @since 5.2.0
  72. *
  73. * @param string $token The token used when generating the given key.
  74. * @param string $key The unhashed key.
  75. * @param int $ttl Time in seconds for the key to be valid for.
  76. * @return true|WP_Error True on success, error object on failure.
  77. */
  78. public function validate_recovery_mode_key( $token, $key, $ttl ) {
  79. $records = $this->get_keys();
  80. if ( ! isset( $records[ $token ] ) ) {
  81. return new WP_Error( 'token_not_found', __( 'Recovery Mode not initialized.' ) );
  82. }
  83. $record = $records[ $token ];
  84. $this->remove_key( $token );
  85. if ( ! is_array( $record ) || ! isset( $record['hashed_key'], $record['created_at'] ) ) {
  86. return new WP_Error( 'invalid_recovery_key_format', __( 'Invalid recovery key format.' ) );
  87. }
  88. if ( ! wp_check_password( $key, $record['hashed_key'] ) ) {
  89. return new WP_Error( 'hash_mismatch', __( 'Invalid recovery key.' ) );
  90. }
  91. if ( time() > $record['created_at'] + $ttl ) {
  92. return new WP_Error( 'key_expired', __( 'Recovery key expired.' ) );
  93. }
  94. return true;
  95. }
  96. /**
  97. * Removes expired recovery mode keys.
  98. *
  99. * @since 5.2.0
  100. *
  101. * @param int $ttl Time in seconds for the keys to be valid for.
  102. */
  103. public function clean_expired_keys( $ttl ) {
  104. $records = $this->get_keys();
  105. foreach ( $records as $key => $record ) {
  106. if ( ! isset( $record['created_at'] ) || time() > $record['created_at'] + $ttl ) {
  107. unset( $records[ $key ] );
  108. }
  109. }
  110. $this->update_keys( $records );
  111. }
  112. /**
  113. * Removes a used recovery key.
  114. *
  115. * @since 5.2.0
  116. *
  117. * @param string $token The token used when generating a recovery mode key.
  118. */
  119. private function remove_key( $token ) {
  120. $records = $this->get_keys();
  121. if ( ! isset( $records[ $token ] ) ) {
  122. return;
  123. }
  124. unset( $records[ $token ] );
  125. $this->update_keys( $records );
  126. }
  127. /**
  128. * Gets the recovery key records.
  129. *
  130. * @since 5.2.0
  131. *
  132. * @return array Associative array of $token => $data pairs, where $data has keys 'hashed_key'
  133. * and 'created_at'.
  134. */
  135. private function get_keys() {
  136. return (array) get_option( $this->option_name, array() );
  137. }
  138. /**
  139. * Updates the recovery key records.
  140. *
  141. * @since 5.2.0
  142. *
  143. * @param array $keys Associative array of $token => $data pairs, where $data has keys 'hashed_key'
  144. * and 'created_at'.
  145. * @return bool True on success, false on failure.
  146. */
  147. private function update_keys( array $keys ) {
  148. return update_option( $this->option_name, $keys );
  149. }
  150. }