Skip to:
Content

bbPress.org

Changeset 1832


Ignore:
Timestamp:
12/08/2008 07:18:40 PM (18 years ago)
Author:
sambauers
Message:

Add BB_NONCE_KEY and associated functionality.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/bb-admin/includes/class.bb-install.php

    r1804 r1832  
    678678                                                'prerequisite' => 'toggle_1'
    679679                                        ),
     680                                        'bb_nonce_key' => array(
     681                                                'value'        => $_bb_default_secret_key,
     682                                                'label'        => __('bbPress "nonce" key'),
     683                                                'note'         => __('This should be a unique and secret phrase, it will be used to make form submission harder for an attacker to spoof.'),
     684                                                'prerequisite' => 'toggle_1'
     685                                        ),
    680686                                        'bb_table_prefix' => array(
    681687                                                'value'        => 'bb_',
     
    11741180                        $data['bb_secure_auth_key']['value'] = addslashes(stripslashes($data['bb_secure_auth_key']['value']));
    11751181                        $data['bb_logged_in_key']['value']   = addslashes(stripslashes($data['bb_logged_in_key']['value']));
     1182                        $data['bb_nonce_key']['value']       = addslashes(stripslashes($data['bb_nonce_key']['value']));
    11761183                }
    11771184               
     
    12241231                                "define('BB_SECURE_AU"  => array("'put your unique phrase here'", "'" . $data['bb_secure_auth_key']['value'] . "'"),
    12251232                                "define('BB_LOGGED_IN"  => array("'put your unique phrase here'", "'" . $data['bb_logged_in_key']['value'] . "'"),
     1233                                "define('BB_NONCE_KEY"  => array("'put your unique phrase here'", "'" . $data['bb_nonce_key']['value'] . "'"),
    12261234                                "\$bb_table_prefix = '" => array("'bb_'",                         "'" . $data['bb_table_prefix']['value'] . "'"),
    12271235                                "define('BB_LANG', ''"  => array("''",                            "'" . $data['bb_lang']['value'] . "'")
  • trunk/bb-admin/install.php

    r1802 r1832  
    6868                                        $bb_install->input_text('bb_secure_auth_key');
    6969                                        $bb_install->input_text('bb_logged_in_key');
     70                                        $bb_install->input_text('bb_nonce_key');
    7071                                        $bb_install->input_text('bb_table_prefix', 'ltr');
    7172?>
  • trunk/bb-config-sample.php

    r1809 r1832  
    1919define('BB_SECURE_AUTH_KEY', 'put your unique phrase here'); // Change this to a unique phrase.
    2020define('BB_LOGGED_IN_KEY', 'put your unique phrase here'); // Change this to a unique phrase.
     21define('BB_NONCE_KEY', 'put your unique phrase here'); // Change this to a unique phrase.
    2122
    2223// If you are running multiple bbPress installations in a single database,
  • trunk/bb-includes/functions.bb-pluggable.php

    r1807 r1832  
    272272endif;
    273273
     274if ( !function_exists('wp_nonce_tick') ) :
     275/**
     276 * Get the time-dependent variable for nonce creation.
     277 *
     278 * A nonce has a lifespan of two ticks. Nonces in their second tick may be
     279 * updated, e.g. by autosave.
     280 *
     281 * @since 1.0
     282 *
     283 * @return int
     284 */
     285function bb_nonce_tick() {
     286        $nonce_life = apply_filters('bb_nonce_life', 86400);
     287
     288        return ceil(time() / ( $nonce_life / 2 ));
     289}
     290endif;
     291
    274292if ( !function_exists('bb_verify_nonce') ) :
     293/**
     294 * Verify that correct nonce was used with time limit.
     295 *
     296 * The user is given an amount of time to use the token, so therefore, since the
     297 * UID and $action remain the same, the independent variable is the time.
     298 *
     299 * @param string $nonce Nonce that was used in the form to verify
     300 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
     301 * @return bool Whether the nonce check passed or failed.
     302 */
    275303function bb_verify_nonce($nonce, $action = -1) {
    276304        $user = bb_get_current_user();
    277         $uid = $user->ID;
    278 
    279         $i = ceil(time() / 43200);
     305        $uid = (int) $user->ID;
     306
     307        $i = bb_nonce_tick();
    280308
    281309        // Nonce generated 0-12 hours ago
    282         if ( substr(wp_hash($i . $action . $uid), -12, 10) == $nonce )
     310        if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
    283311                return 1;
    284312        // Nonce generated 12-24 hours ago
    285         if ( substr(wp_hash(($i - 1) . $action . $uid), -12, 10) == $nonce )
     313        if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
    286314                return 2;
    287315        // Invalid nonce
     
    291319
    292320if ( !function_exists('bb_create_nonce') ) :
     321/**
     322 * Creates a random, one time use token.
     323 *
     324 * @since 2.0.4
     325 *
     326 * @param string|int $action Scalar value to add context to the nonce.
     327 * @return string The one use form token
     328 */
    293329function bb_create_nonce($action = -1) {
    294330        $user = bb_get_current_user();
    295         $uid = $user->ID;
    296 
    297         $i = ceil(time() / 43200);
    298        
    299         return substr(wp_hash($i . $action . $uid), -12, 10);
    300 }
    301 endif;
    302 
    303 // Not verbatim WP,  constants have different names.
     331        $uid = (int) $user->ID;
     332
     333        $i = bb_nonce_tick();
     334       
     335        return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
     336}
     337endif;
     338
     339function _bb_get_key( $key, $default_key = false ) {
     340        if ( !$default_key ) {
     341                global $bb_default_secret_key;
     342                $default_key = $bb_default_secret_key;
     343        }
     344
     345        if ( defined( $key ) && '' != constant( $key ) && $default_key != constant( $key ) ) {
     346                return $key;
     347        }
     348
     349        return $default_key;
     350}
     351
     352function _bb_get_salt( $constants, $option = false ) {
     353        if ( !is_array( $constants ) ) {
     354                $constants = array( $constants );
     355        }
     356
     357        foreach ($constants as $constant ) {
     358                if ( defined( $constant ) ) {
     359                        return constant( $constant );
     360                }
     361        }
     362
     363        if ( !defined( 'BB_INSTALLING' ) || !BB_INSTALLING ) {
     364                if ( !$option ) {
     365                        $option = strtolower( $constants[0] );
     366                }
     367                $salt = bb_get_option( $option );
     368                if ( empty( $salt ) ) {
     369                        $salt = wp_generate_password();
     370                        bb_update_option( $option, $salt );
     371                }
     372                return $salt;
     373        }
     374
     375        return '';
     376}
     377
     378// Not verbatim WP, constants have different names, uses helper functions.
    304379if ( !function_exists('wp_salt') ) :
    305380function wp_salt($scheme = 'auth') {
    306         global $bb_default_secret_key;
    307        
    308         $secret_key = '';
    309         if ( defined('BB_SECRET_KEY') && ('' != BB_SECRET_KEY) && ($bb_default_secret_key != BB_SECRET_KEY) )
    310                 $secret_key = BB_SECRET_KEY;
    311        
     381        $secret_key = _bb_get_key( 'BB_SECRET_KEY' );
     382
    312383        switch ($scheme) {
    313384                case 'auth':
    314                         if ( defined('BB_AUTH_KEY') && ('' != BB_AUTH_KEY) && ( $bb_default_secret_key != BB_AUTH_KEY) )
    315                                 $secret_key = BB_AUTH_KEY;
    316                        
    317                         if ( defined('BB_AUTH_SALT') ) {
    318                                 $salt = BB_AUTH_SALT;
    319                         } elseif ( defined('BB_SECRET_SALT') ) {
    320                                 $salt = BB_SECRET_SALT;
    321                         } elseif ( !BB_INSTALLING ) {
    322                                 $salt = bb_get_option('bb_auth_salt');
    323                                 if ( empty($salt) ) {
    324                                         $salt = wp_generate_password();
    325                                         bb_update_option('bb_auth_salt', $salt);
    326                                 }
    327                         }
     385                        $secret_key = _bb_get_key( 'BB_AUTH_KEY', $secret_key );
     386                        $salt = _bb_get_salt( array( 'BB_AUTH_SALT', 'BB_SECRET_SALT' ) );
    328387                        break;
    329                
     388
    330389                case 'secure_auth':
    331                         if ( defined('BB_SECURE_AUTH_KEY') && ('' != BB_SECURE_AUTH_KEY) && ( $bb_default_secret_key != BB_SECURE_AUTH_KEY) )
    332                                 $secret_key = BB_SECURE_AUTH_KEY;
    333                        
    334                         if ( defined('BB_SECURE_AUTH_SALT') ) {
    335                                 $salt = BB_SECURE_AUTH_SALT;
    336                         } else {
    337                                 $salt = bb_get_option('bb_secure_auth_salt');
    338                                 if ( empty($salt) ) {
    339                                         $salt = wp_generate_password();
    340                                         bb_update_option('bb_secure_auth_salt', $salt);
    341                                 }
    342                         }
     390                        $secret_key = _bb_get_key( 'BB_SECURE_AUTH_KEY', $secret_key );
     391                        $salt = _bb_get_salt( 'BB_SECURE_AUTH_SALT' );
    343392                        break;
    344                
     393
    345394                case 'logged_in':
    346                         if ( defined('BB_LOGGED_IN_KEY') && ('' != BB_LOGGED_IN_KEY) && ( $bb_default_secret_key != BB_LOGGED_IN_KEY) )
    347                                 $secret_key = BB_LOGGED_IN_KEY;
    348                        
    349                         if ( defined('BB_LOGGED_IN_SALT') ) {
    350                                 $salt = BB_LOGGED_IN_SALT;
    351                         } else {
    352                                 $salt = bb_get_option('bb_logged_in_salt');
    353                                 if ( empty($salt) && ( !defined( 'BB_INSTALLING' ) || !BB_INSTALLING ) ) {
    354                                         $salt = wp_generate_password();
    355                                         bb_update_option('bb_logged_in_salt', $salt);
    356                                 }
    357                         }
     395                        $secret_key = _bb_get_key( 'BB_LOGGED_IN_KEY', $secret_key );
     396                        $salt = _bb_get_salt( 'BB_LOGGED_IN_SALT' );
    358397                        break;
    359         }
    360        
    361         return apply_filters('salt', $secret_key . $salt, $scheme);
     398
     399                case 'nonce':
     400                        $secret_key = _bb_get_key( 'BB_NONCE_KEY', $secret_key );
     401                        $salt = _bb_get_salt( 'BB_NONCE_SALT' );
     402                        break;
     403
     404                default:
     405                        // ensure each auth scheme has its own unique salt
     406                        $salt = hash_hmac( 'md5', $scheme, $secret_key );
     407                        break;
     408        }
     409
     410        return apply_filters( 'salt', $secret_key . $salt, $scheme );
    362411}
    363412endif;
Note: See TracChangeset for help on using the changeset viewer.