wp/wp-includes/class-wp-session-tokens.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    21 	 * @var int User ID.
    21 	 * @var int User ID.
    22 	 */
    22 	 */
    23 	protected $user_id;
    23 	protected $user_id;
    24 
    24 
    25 	/**
    25 	/**
    26 	 * Protected constructor.
    26 	 * Protected constructor. Use the `get_instance()` method to get the instance.
    27 	 *
    27 	 *
    28 	 * @since 4.0.0
    28 	 * @since 4.0.0
    29 	 *
    29 	 *
    30 	 * @param int $user_id User whose session to manage.
    30 	 * @param int $user_id User whose session to manage.
    31 	 */
    31 	 */
    32 	protected function __construct( $user_id ) {
    32 	protected function __construct( $user_id ) {
    33 		$this->user_id = $user_id;
    33 		$this->user_id = $user_id;
    34 	}
    34 	}
    35 
    35 
    36 	/**
    36 	/**
    37 	 * Retrieves a session token manager instance for a user.
    37 	 * Retrieves a session manager instance for a user.
    38 	 *
    38 	 *
    39 	 * This method contains a {@see 'session_token_manager'} filter, allowing a plugin to swap out
    39 	 * This method contains a {@see 'session_token_manager'} filter, allowing a plugin to swap out
    40 	 * the session manager for a subclass of `WP_Session_Tokens`.
    40 	 * the session manager for a subclass of `WP_Session_Tokens`.
    41 	 *
    41 	 *
    42 	 * @since 4.0.0
    42 	 * @since 4.0.0
    43 	 * @static
       
    44 	 *
    43 	 *
    45 	 * @param int $user_id User whose session to manage.
    44 	 * @param int $user_id User whose session to manage.
    46 	 * @return WP_User_Meta_Session_Tokens WP_User_Meta_Session_Tokens class instance by default.
    45 	 * @return WP_Session_Tokens The session object, which is by default an instance of
       
    46 	 *                           the `WP_User_Meta_Session_Tokens` class.
    47 	 */
    47 	 */
    48 	final public static function get_instance( $user_id ) {
    48 	final public static function get_instance( $user_id ) {
    49 		/**
    49 		/**
    50 		 * Filters the session token manager used.
    50 		 * Filters the class name for the session token manager.
    51 		 *
    51 		 *
    52 		 * @since 4.0.0
    52 		 * @since 4.0.0
    53 		 *
    53 		 *
    54 		 * @param string $session Name of class to use as the manager.
    54 		 * @param string $session Name of class to use as the manager.
    55 		 *                        Default 'WP_User_Meta_Session_Tokens'.
    55 		 *                        Default 'WP_User_Meta_Session_Tokens'.
    57 		$manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
    57 		$manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
    58 		return new $manager( $user_id );
    58 		return new $manager( $user_id );
    59 	}
    59 	}
    60 
    60 
    61 	/**
    61 	/**
    62 	 * Hashes a session token for storage.
    62 	 * Hashes the given session token for storage.
    63 	 *
    63 	 *
    64 	 * @since 4.0.0
    64 	 * @since 4.0.0
    65 	 *
    65 	 *
    66 	 * @param string $token Session token to hash.
    66 	 * @param string $token Session token to hash.
    67 	 * @return string A hash of the session token (a verifier).
    67 	 * @return string A hash of the session token (a verifier).
    74 			return sha1( $token );
    74 			return sha1( $token );
    75 		}
    75 		}
    76 	}
    76 	}
    77 
    77 
    78 	/**
    78 	/**
    79 	 * Get a user's session.
    79 	 * Retrieves a user's session for the given token.
    80 	 *
    80 	 *
    81 	 * @since 4.0.0
    81 	 * @since 4.0.0
    82 	 *
    82 	 *
    83 	 * @param string $token Session token
    83 	 * @param string $token Session token.
    84 	 * @return array User session
    84 	 * @return array|null The session, or null if it does not exist.
    85 	 */
    85 	 */
    86 	final public function get( $token ) {
    86 	final public function get( $token ) {
    87 		$verifier = $this->hash_token( $token );
    87 		$verifier = $this->hash_token( $token );
    88 		return $this->get_session( $verifier );
    88 		return $this->get_session( $verifier );
    89 	}
    89 	}
    90 
    90 
    91 	/**
    91 	/**
    92 	 * Validate a user's session token as authentic.
    92 	 * Validates the given session token for authenticity and validity.
    93 	 *
    93 	 *
    94 	 * Checks that the given token is present and hasn't expired.
    94 	 * Checks that the given token is present and hasn't expired.
    95 	 *
    95 	 *
    96 	 * @since 4.0.0
    96 	 * @since 4.0.0
    97 	 *
    97 	 *
   102 		$verifier = $this->hash_token( $token );
   102 		$verifier = $this->hash_token( $token );
   103 		return (bool) $this->get_session( $verifier );
   103 		return (bool) $this->get_session( $verifier );
   104 	}
   104 	}
   105 
   105 
   106 	/**
   106 	/**
   107 	 * Generate a session token and attach session information to it.
   107 	 * Generates a session token and attaches session information to it.
   108 	 *
   108 	 *
   109 	 * A session token is a long, random string. It is used in a cookie
   109 	 * A session token is a long, random string. It is used in a cookie
   110 	 * link that cookie to an expiration time and to ensure the cookie
   110 	 * to link that cookie to an expiration time and to ensure the cookie
   111 	 * becomes invalidated upon logout.
   111 	 * becomes invalidated when the user logs out.
   112 	 *
   112 	 *
   113 	 * This function generates a token and stores it with the associated
   113 	 * This function generates a token and stores it with the associated
   114 	 * expiration time (and potentially other session information via the
   114 	 * expiration time (and potentially other session information via the
   115 	 * {@see 'attach_session_information'} filter).
   115 	 * {@see 'attach_session_information'} filter).
   116 	 *
   116 	 *
   121 	 */
   121 	 */
   122 	final public function create( $expiration ) {
   122 	final public function create( $expiration ) {
   123 		/**
   123 		/**
   124 		 * Filters the information attached to the newly created session.
   124 		 * Filters the information attached to the newly created session.
   125 		 *
   125 		 *
   126 		 * Could be used in the future to attach information such as
   126 		 * Can be used to attach further information to a session.
   127 		 * IP address or user agent to a session.
       
   128 		 *
   127 		 *
   129 		 * @since 4.0.0
   128 		 * @since 4.0.0
   130 		 *
   129 		 *
   131 		 * @param array $session Array of extra data.
   130 		 * @param array $session Array of extra data.
   132 		 * @param int   $user_id User ID.
   131 		 * @param int   $user_id User ID.
   133 		 */
   132 		 */
   134 		$session = apply_filters( 'attach_session_information', array(), $this->user_id );
   133 		$session               = apply_filters( 'attach_session_information', array(), $this->user_id );
   135 		$session['expiration'] = $expiration;
   134 		$session['expiration'] = $expiration;
   136 
   135 
   137 		// IP address.
   136 		// IP address.
   138 		if ( !empty( $_SERVER['REMOTE_ADDR'] ) ) {
   137 		if ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
   139 			$session['ip'] = $_SERVER['REMOTE_ADDR'];
   138 			$session['ip'] = $_SERVER['REMOTE_ADDR'];
   140 		}
   139 		}
   141 
   140 
   142 		// User-agent.
   141 		// User-agent.
   143 		if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
   142 		if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
   153 
   152 
   154 		return $token;
   153 		return $token;
   155 	}
   154 	}
   156 
   155 
   157 	/**
   156 	/**
   158 	 * Update a session token.
   157 	 * Updates the data for the session with the given token.
   159 	 *
   158 	 *
   160 	 * @since 4.0.0
   159 	 * @since 4.0.0
   161 	 *
   160 	 *
   162 	 * @param string $token Session token to update.
   161 	 * @param string $token Session token to update.
   163 	 * @param array  $session Session information.
   162 	 * @param array  $session Session information.
   166 		$verifier = $this->hash_token( $token );
   165 		$verifier = $this->hash_token( $token );
   167 		$this->update_session( $verifier, $session );
   166 		$this->update_session( $verifier, $session );
   168 	}
   167 	}
   169 
   168 
   170 	/**
   169 	/**
   171 	 * Destroy a session token.
   170 	 * Destroys the session with the given token.
   172 	 *
   171 	 *
   173 	 * @since 4.0.0
   172 	 * @since 4.0.0
   174 	 *
   173 	 *
   175 	 * @param string $token Session token to destroy.
   174 	 * @param string $token Session token to destroy.
   176 	 */
   175 	 */
   178 		$verifier = $this->hash_token( $token );
   177 		$verifier = $this->hash_token( $token );
   179 		$this->update_session( $verifier, null );
   178 		$this->update_session( $verifier, null );
   180 	}
   179 	}
   181 
   180 
   182 	/**
   181 	/**
   183 	 * Destroy all session tokens for this user,
   182 	 * Destroys all sessions for this user except the one with the given token (presumably the one in use).
   184 	 * except a single token, presumably the one in use.
       
   185 	 *
   183 	 *
   186 	 * @since 4.0.0
   184 	 * @since 4.0.0
   187 	 *
   185 	 *
   188 	 * @param string $token_to_keep Session token to keep.
   186 	 * @param string $token_to_keep Session token to keep.
   189 	 */
   187 	 */
   190 	final public function destroy_others( $token_to_keep ) {
   188 	final public function destroy_others( $token_to_keep ) {
   191 		$verifier = $this->hash_token( $token_to_keep );
   189 		$verifier = $this->hash_token( $token_to_keep );
   192 		$session = $this->get_session( $verifier );
   190 		$session  = $this->get_session( $verifier );
   193 		if ( $session ) {
   191 		if ( $session ) {
   194 			$this->destroy_other_sessions( $verifier );
   192 			$this->destroy_other_sessions( $verifier );
   195 		} else {
   193 		} else {
   196 			$this->destroy_all_sessions();
   194 			$this->destroy_all_sessions();
   197 		}
   195 		}
   198 	}
   196 	}
   199 
   197 
   200 	/**
   198 	/**
   201 	 * Determine whether a session token is still valid,
   199 	 * Determines whether a session is still valid, based on its expiration timestamp.
   202 	 * based on expiration.
       
   203 	 *
   200 	 *
   204 	 * @since 4.0.0
   201 	 * @since 4.0.0
   205 	 *
   202 	 *
   206 	 * @param array $session Session to check.
   203 	 * @param array $session Session to check.
   207 	 * @return bool Whether session is valid.
   204 	 * @return bool Whether session is valid.
   209 	final protected function is_still_valid( $session ) {
   206 	final protected function is_still_valid( $session ) {
   210 		return $session['expiration'] >= time();
   207 		return $session['expiration'] >= time();
   211 	}
   208 	}
   212 
   209 
   213 	/**
   210 	/**
   214 	 * Destroy all session tokens for a user.
   211 	 * Destroys all sessions for a user.
   215 	 *
   212 	 *
   216 	 * @since 4.0.0
   213 	 * @since 4.0.0
   217 	 */
   214 	 */
   218 	final public function destroy_all() {
   215 	final public function destroy_all() {
   219 		$this->destroy_all_sessions();
   216 		$this->destroy_all_sessions();
   220 	}
   217 	}
   221 
   218 
   222 	/**
   219 	/**
   223 	 * Destroy all session tokens for all users.
   220 	 * Destroys all sessions for all users.
   224 	 *
   221 	 *
   225 	 * @since 4.0.0
   222 	 * @since 4.0.0
   226 	 * @static
       
   227 	 */
   223 	 */
   228 	final public static function destroy_all_for_all_users() {
   224 	final public static function destroy_all_for_all_users() {
   229 		/** This filter is documented in wp-includes/class-wp-session-tokens.php */
   225 		/** This filter is documented in wp-includes/class-wp-session-tokens.php */
   230 		$manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
   226 		$manager = apply_filters( 'session_token_manager', 'WP_User_Meta_Session_Tokens' );
   231 		call_user_func( array( $manager, 'drop_sessions' ) );
   227 		call_user_func( array( $manager, 'drop_sessions' ) );
   232 	}
   228 	}
   233 
   229 
   234 	/**
   230 	/**
   235 	 * Retrieve all sessions of a user.
   231 	 * Retrieves all sessions for a user.
   236 	 *
   232 	 *
   237 	 * @since 4.0.0
   233 	 * @since 4.0.0
   238 	 *
   234 	 *
   239 	 * @return array Sessions of a user.
   235 	 * @return array Sessions for a user.
   240 	 */
   236 	 */
   241 	final public function get_all() {
   237 	final public function get_all() {
   242 		return array_values( $this->get_sessions() );
   238 		return array_values( $this->get_sessions() );
   243 	}
   239 	}
   244 
   240 
   245 	/**
   241 	/**
   246 	 * This method should retrieve all sessions of a user, keyed by verifier.
   242 	 * Retrieves all sessions of the user.
   247 	 *
   243 	 *
   248 	 * @since 4.0.0
   244 	 * @since 4.0.0
   249 	 *
   245 	 *
   250 	 * @return array Sessions of a user, keyed by verifier.
   246 	 * @return array Sessions of the user.
   251 	 */
   247 	 */
   252 	abstract protected function get_sessions();
   248 	abstract protected function get_sessions();
   253 
   249 
   254 	/**
   250 	/**
   255 	 * This method should look up a session by its verifier (token hash).
   251 	 * Retrieves a session based on its verifier (token hash).
   256 	 *
   252 	 *
   257 	 * @since 4.0.0
   253 	 * @since 4.0.0
   258 	 *
   254 	 *
   259 	 * @param string $verifier Verifier of the session to retrieve.
   255 	 * @param string $verifier Verifier for the session to retrieve.
   260 	 * @return array|null The session, or null if it does not exist.
   256 	 * @return array|null The session, or null if it does not exist.
   261 	 */
   257 	 */
   262 	abstract protected function get_session( $verifier );
   258 	abstract protected function get_session( $verifier );
   263 
   259 
   264 	/**
   260 	/**
   265 	 * This method should update a session by its verifier.
   261 	 * Updates a session based on its verifier (token hash).
   266 	 *
   262 	 *
   267 	 * Omitting the second argument should destroy the session.
   263 	 * Omitting the second argument destroys the session.
   268 	 *
   264 	 *
   269 	 * @since 4.0.0
   265 	 * @since 4.0.0
   270 	 *
   266 	 *
   271 	 * @param string $verifier Verifier of the session to update.
   267 	 * @param string $verifier Verifier for the session to update.
   272 	 * @param array  $session  Optional. Session. Omitting this argument destroys the session.
   268 	 * @param array  $session  Optional. Session. Omitting this argument destroys the session.
   273 	 */
   269 	 */
   274 	abstract protected function update_session( $verifier, $session = null );
   270 	abstract protected function update_session( $verifier, $session = null );
   275 
   271 
   276 	/**
   272 	/**
   277 	 * This method should destroy all session tokens for this user,
   273 	 * Destroys all sessions for this user, except the single session with the given verifier.
   278 	 * except a single session passed.
       
   279 	 *
   274 	 *
   280 	 * @since 4.0.0
   275 	 * @since 4.0.0
   281 	 *
   276 	 *
   282 	 * @param string $verifier Verifier of the session to keep.
   277 	 * @param string $verifier Verifier of the session to keep.
   283 	 */
   278 	 */
   284 	abstract protected function destroy_other_sessions( $verifier );
   279 	abstract protected function destroy_other_sessions( $verifier );
   285 
   280 
   286 	/**
   281 	/**
   287 	 * This method should destroy all sessions for a user.
   282 	 * Destroys all sessions for the user.
   288 	 *
   283 	 *
   289 	 * @since 4.0.0
   284 	 * @since 4.0.0
   290 	 */
   285 	 */
   291 	abstract protected function destroy_all_sessions();
   286 	abstract protected function destroy_all_sessions();
   292 
   287 
   293 	/**
   288 	/**
   294 	 * This static method should destroy all session tokens for all users.
   289 	 * Destroys all sessions for all users.
   295 	 *
   290 	 *
   296 	 * @since 4.0.0
   291 	 * @since 4.0.0
   297 	 * @static
       
   298 	 */
   292 	 */
   299 	public static function drop_sessions() {}
   293 	public static function drop_sessions() {}
   300 }
   294 }