WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/16/2019 05:08:16 AM (5 months ago)
Author:
flixos90
Message:

Bootstrap/Load: Allow more than one recovery link to be valid at a time.

While currently a recovery link is only made available via the admin email address, this will be expanded in the future. In order to accomplish that, the mechanisms to store and validate recovery keys must support multiple keys to be valid at the same time.

This changeset adds that support, adding an additional token parameter which is part of a recovery link in addition to the key. A key itself is always associated with a token, so the two are only valid in combination. These associations are stored in a new recovery_keys option, which is regularly cleared in a new Cron hook, to prevent potential cluttering from unused recovery keys.

This changeset does not have any user-facing implications otherwise.

Props pbearne, timothyblynjacobs.
Fixes #46595. See #46130.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-recovery-mode-key-service.php

    r44973 r45211  
    11<?php
    22/**
    3  * Error Protection API: WP_Recovery_Mode_Key_service class
     3 * Error Protection API: WP_Recovery_Mode_Key_Service class
    44 *
    55 * @package WordPress
     
    1515
    1616    /**
     17     * The option name used to store the keys.
     18     *
     19     * @since 5.2.0
     20     * @var string
     21     */
     22    private $option_name = 'recovery_keys';
     23
     24    /**
     25     * Creates a recovery mode token.
     26     *
     27     * @since 5.2.0
     28     *
     29     * @return string $token A random string to identify its associated key in storage.
     30     */
     31    public function generate_recovery_mode_token() {
     32        return wp_generate_password( 22, false );
     33    }
     34
     35    /**
    1736     * Creates a recovery mode key.
    1837     *
     
    2140     * @global PasswordHash $wp_hasher
    2241     *
    23      * @return string Recovery mode key.
     42     * @param string $token A token generated by {@see generate_recovery_mode_token()}.
     43     * @return string $key Recovery mode key.
    2444     */
    25     public function generate_and_store_recovery_mode_key() {
     45    public function generate_and_store_recovery_mode_key( $token ) {
    2646
    2747        global $wp_hasher;
    2848
    2949        $key = wp_generate_password( 22, false );
    30 
    31         /**
    32          * Fires when a recovery mode key is generated for a user.
    33          *
    34          * @since 5.2.0
    35          *
    36          * @param string $key The recovery mode key.
    37          */
    38         do_action( 'generate_recovery_mode_key', $key );
    3950
    4051        if ( empty( $wp_hasher ) ) {
     
    4556        $hashed = $wp_hasher->HashPassword( $key );
    4657
    47         update_option(
    48             'recovery_key',
    49             array(
    50                 'hashed_key' => $hashed,
    51                 'created_at' => time(),
    52             )
     58        $records = $this->get_keys();
     59
     60        $records[ $token ] = array(
     61            'hashed_key' => $hashed,
     62            'created_at' => time(),
    5363        );
     64
     65        $this->update_keys( $records );
     66
     67        /**
     68         * Fires when a recovery mode key is generated.
     69         *
     70         * @since 5.2.0
     71         *
     72         * @param string $token The recovery data token.
     73         * @param string $key   The recovery mode key.
     74         */
     75        do_action( 'generate_recovery_mode_key', $token, $key );
    5476
    5577        return $key;
     
    5981     * Verifies if the recovery mode key is correct.
    6082     *
     83     * Recovery mode keys can only be used once; the key will be consumed in the process.
     84     *
    6185     * @since 5.2.0
    6286     *
    63      * @param string $key The unhashed key.
    64      * @param int    $ttl Time in seconds for the key to be valid for.
     87     * @param string $token The token used when generating the given key.
     88     * @param string $key   The unhashed key.
     89     * @param int    $ttl   Time in seconds for the key to be valid for.
    6590     * @return true|WP_Error True on success, error object on failure.
    6691     */
    67     public function validate_recovery_mode_key( $key, $ttl ) {
     92    public function validate_recovery_mode_key( $token, $key, $ttl ) {
    6893
    69         $record = get_option( 'recovery_key' );
     94        $records = $this->get_keys();
    7095
    71         if ( ! $record ) {
    72             return new WP_Error( 'no_recovery_key_set', __( 'Recovery Mode not initialized.' ) );
     96        if ( ! isset( $records[ $token ] ) ) {
     97            return new WP_Error( 'token_not_found', __( 'Recovery Mode not initialized.' ) );
    7398        }
     99
     100        $record = $records[ $token ];
     101
     102        $this->remove_key( $token );
    74103
    75104        if ( ! is_array( $record ) || ! isset( $record['hashed_key'], $record['created_at'] ) ) {
     
    87116        return true;
    88117    }
     118
     119    /**
     120     * Removes expired recovery mode keys.
     121     *
     122     * @since 5.2.0
     123     *
     124     * @param int $ttl Time in seconds for the keys to be valid for.
     125     */
     126    public function clean_expired_keys( $ttl ) {
     127
     128        $records = $this->get_keys();
     129
     130        foreach ( $records as $key => $record ) {
     131            if ( ! isset( $record['created_at'] ) || time() > $record['created_at'] + $ttl ) {
     132                unset( $records[ $key ] );
     133            }
     134        }
     135
     136        $this->update_keys( $records );
     137    }
     138
     139    /**
     140     * Removes a used recovery key.
     141     *
     142     * @since 5.2.0
     143     *
     144     * @param string $token The token used when generating a recovery mode key.
     145     */
     146    private function remove_key( $token ) {
     147
     148        $records = $this->get_keys();
     149
     150        if ( ! isset( $records[ $token ] ) ) {
     151            return;
     152        }
     153
     154        unset( $records[ $token ] );
     155
     156        $this->update_keys( $records );
     157    }
     158
     159    /**
     160     * Gets the recovery key records.
     161     *
     162     * @since 5.2.0
     163     *
     164     * @return array Associative array of $token => $data pairs, where $data has keys 'hashed_key'
     165     *               and 'created_at'.
     166     */
     167    private function get_keys() {
     168        return (array) get_option( $this->option_name, array() );
     169    }
     170
     171    /**
     172     * Updates the recovery key records.
     173     *
     174     * @since 5.2.0
     175     *
     176     * @param array $keys Associative array of $token => $data pairs, where $data has keys 'hashed_key'
     177     *                    and 'created_at'.
     178     * @return bool True on success, false on failure.
     179     */
     180    private function update_keys( array $keys ) {
     181        return update_option( $this->option_name, $keys );
     182    }
    89183}
Note: See TracChangeset for help on using the changeset viewer.