返回介绍

wp_validate_auth_cookie()

发布于 2017-09-11 13:10:04 字数 5125 浏览 1144 评论 0 收藏 0

wp_validate_auth_cookie( string $cookie = '',  string $scheme = '' )

Validates authentication cookie.


description

The checks include making sure that the authentication cookie is set and pulling in the contents (if $cookie is not used).

Makes sure the cookie is not expired. Verifies the hash in cookie is what is should be and compares the two.


参数

$cookie

(string) (Optional) If used, will validate contents instead of cookie's

Default value: ''

$scheme

(string) (Optional) The cookie scheme to use: auth, secure_auth, or logged_in

Default value: ''


返回值

(false|int) False if invalid cookie, User ID if valid.


源代码

File: wp-includes/pluggable.php

function wp_validate_auth_cookie($cookie = '', $scheme = '') {
	if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
		/**
		 * Fires if an authentication cookie is malformed.
		 *
		 * @since 2.7.0
		 *
		 * @param string $cookie Malformed auth cookie.
		 * @param string $scheme Authentication scheme. Values include 'auth', 'secure_auth',
		 *                       or 'logged_in'.
		 */
		do_action( 'auth_cookie_malformed', $cookie, $scheme );
		return false;
	}

	$scheme = $cookie_elements['scheme'];
	$username = $cookie_elements['username'];
	$hmac = $cookie_elements['hmac'];
	$token = $cookie_elements['token'];
	$expired = $expiration = $cookie_elements['expiration'];

	// Allow a grace period for POST and Ajax requests
	if ( wp_doing_ajax() || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
		$expired += HOUR_IN_SECONDS;
	}

	// Quick check to see if an honest cookie has expired
	if ( $expired < time() ) {
		/**
		 * Fires once an authentication cookie has expired.
		 *
		 * @since 2.7.0
		 *
		 * @param array $cookie_elements An array of data for the authentication cookie.
		 */
		do_action( 'auth_cookie_expired', $cookie_elements );
		return false;
	}

	$user = get_user_by('login', $username);
	if ( ! $user ) {
		/**
		 * Fires if a bad username is entered in the user authentication process.
		 *
		 * @since 2.7.0
		 *
		 * @param array $cookie_elements An array of data for the authentication cookie.
		 */
		do_action( 'auth_cookie_bad_username', $cookie_elements );
		return false;
	}

	$pass_frag = substr($user->user_pass, 8, 4);

	$key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );

	// If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
	$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
	$hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key );

	if ( ! hash_equals( $hash, $hmac ) ) {
		/**
		 * Fires if a bad authentication cookie hash is encountered.
		 *
		 * @since 2.7.0
		 *
		 * @param array $cookie_elements An array of data for the authentication cookie.
		 */
		do_action( 'auth_cookie_bad_hash', $cookie_elements );
		return false;
	}

	$manager = WP_Session_Tokens::get_instance( $user->ID );
	if ( ! $manager->verify( $token ) ) {
		do_action( 'auth_cookie_bad_session_token', $cookie_elements );
		return false;
	}

	// Ajax/POST grace period set above
	if ( $expiration < time() ) {
		$GLOBALS['login_grace_period'] = 1;
	}

	/**
	 * Fires once an authentication cookie has been validated.
	 *
	 * @since 2.7.0
	 *
	 * @param array   $cookie_elements An array of data for the authentication cookie.
	 * @param WP_User $user            User object.
	 */
	do_action( 'auth_cookie_valid', $cookie_elements, $user );

	return $user->ID;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/load.php: wp_doing_ajax()
  • wp-includes/class-wp-session-tokens.php: WP_Session_Tokens::get_instance()
  • wp-includes/compat.php: hash_equals()
  • wp-includes/pluggable.php: wp_hash()
  • wp-includes/pluggable.php: wp_parse_auth_cookie()
  • wp-includes/pluggable.php: auth_cookie_malformed
  • wp-includes/pluggable.php: auth_cookie_expired
  • wp-includes/pluggable.php: auth_cookie_bad_username
  • wp-includes/pluggable.php: auth_cookie_bad_hash
  • wp-includes/pluggable.php: auth_cookie_valid
  • wp-includes/pluggable.php: get_user_by()
  • wp-includes/plugin.php: do_action()
  • Show 7 more uses Hide more uses

Used By

  • wp-includes/pluggable.php: auth_redirect()
  • wp-includes/user.php: wp_authenticate_cookie()
  • wp-includes/user.php: wp_validate_logged_in_cookie()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文