Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a url to be passed in to load after login #41

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions one-time-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
* @param WP_User|null $user ID, email address, or user login for the user.
* @param int $count Generate a specified number of login tokens (default: 1).
* @param bool $delay_delete Delete existing tokens after 15 minutes, instead of immediately.
* @param string $redirect_to Send the user to this wp-admin URL after logging in.
*
* @return array
*/
function one_time_login_generate_tokens( $user, $count, $delay_delete ) {
function one_time_login_generate_tokens( $user, $count, $delay_delete, $redirect_to = '' ) {
$tokens = $new_tokens = array();
$login_urls = array();

Expand All @@ -35,7 +36,10 @@ function one_time_login_generate_tokens( $user, $count, $delay_delete ) {
for ( $i = 0; $i < $count; $i++ ) {
$password = wp_generate_password();
$token = sha1( $password );
$tokens[] = $token;
$tokens[] = array(
'token' => $token,
'redirect_to' => $redirect_to,
);
$new_tokens[] = $token;
}

Expand All @@ -61,6 +65,7 @@ function one_time_login_generate_tokens( $user, $count, $delay_delete ) {
* <user>
* [--count=<count>]
* [--delay-delete]
* [--redirect-to=<url>]
*
* ## EXAMPLES
*
Expand All @@ -77,8 +82,9 @@ function one_time_login_wp_cli_command( $args, $assoc_args ) {
$user = $fetcher->get_check( $args[0] );
$delay_delete = WP_CLI\Utils\get_flag_value( $assoc_args, 'delay-delete' );
$count = (int) ( $assoc_args['count'] ?? 1 );
$redirect_to = $assoc_args['redirect-to'] ?? '';

$login_urls = one_time_login_generate_tokens( $user, $count, $delay_delete );
$login_urls = one_time_login_generate_tokens( $user, $count, $delay_delete, $redirect_to );
foreach ( $login_urls as $login_url ) {
WP_CLI::log( $login_url );
}
Expand All @@ -89,7 +95,7 @@ function one_time_login_wp_cli_command( $args, $assoc_args ) {
}

/**
* Generate one-time tokens using WP CLI.
* Generate one-time tokens using WP REST API.
*
* ## OPTIONS
*
Expand All @@ -108,7 +114,6 @@ function one_time_login_wp_cli_command( $args, $assoc_args ) {
* @return WP_REST_Response
*/
function one_time_login_api_request( WP_REST_Request $request ) {

$user = get_user_by( 'login', $request['user'] );
$delay_delete = (bool) ( $request['delay_delete'] ?? false );
$count = (int) ( $request['count'] ?? 1 );
Expand Down Expand Up @@ -151,6 +156,7 @@ function one_time_login_rest_api_init() {
return false;
}
$user = get_user_by( 'login', $request['user'] );

return current_user_can( 'edit_user', $user->ID );
},
),
Expand Down Expand Up @@ -225,13 +231,26 @@ function one_time_login_handle_token() {
wp_die( $error );
}

$tokens = get_user_meta( $user->ID, 'one_time_login_token', true );
$tokens = is_string( $tokens ) ? array( $tokens ) : $tokens;
$is_valid = false;
$tokens = get_user_meta( $user->ID, 'one_time_login_token', true );
$tokens = is_string( $tokens ) ? array( $tokens ) : $tokens;
$is_valid = false;
$redirect_to = '';

foreach ( $tokens as $i => $token ) {
if ( hash_equals( $token, $_GET['one_time_login_token'] ) ) {
$this_token = '';
if ( is_array( $token ) ) {
$this_token = $token['token'];
} else {
$this_token = $token;
}
if ( hash_equals( $this_token, $_GET['one_time_login_token'] ) ) {
$is_valid = true;
unset( $tokens[ $i ] );

if ( is_array( $token ) && isset( $token['redirect_to'] ) ) {
$redirect_to = $token['redirect_to'];
}

break;
}
}
Expand All @@ -244,7 +263,7 @@ function one_time_login_handle_token() {
update_user_meta( $user->ID, 'one_time_login_token', $tokens );
wp_set_auth_cookie( $user->ID, true, is_ssl() );
do_action( 'one_time_login_after_auth_cookie_set', $user );
one_time_login_safe_redirect( admin_url() );
one_time_login_safe_redirect( admin_url() . $redirect_to );
}

add_action( 'init', 'one_time_login_handle_token' );
Expand Down
4 changes: 2 additions & 2 deletions tests/test-one-time-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ public function test_one_time_login_handle_token_invalid_token() {
public function test_one_time_login_handle_token_success() {
one_time_login_generate_tokens( self::$users['administrator'], 1, false );
$tokens = get_user_meta( self::$users['administrator']->ID, 'one_time_login_token', true );
$token = array_shift( $tokens );
$token = reset( $tokens );
$GLOBALS['pagenow'] = 'wp-login.php';
$_GET['one_time_login_token'] = $token;
$_GET['one_time_login_token'] = $token['token'];
$_GET['user_id'] = self::$users['administrator']->ID;
one_time_login_handle_token();
$this->assertRedirect( admin_url(), 302 );
Expand Down