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

feat(Sessions): Add remember me feature via a filter #513

Merged
merged 4 commits into from
Mar 17, 2024
Merged
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
7 changes: 5 additions & 2 deletions includes/openid-connect-generic-client-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,19 @@ public function login_user( $user, $token_response, $id_token_claim, $user_claim
// Allow plugins / themes to take action using current claims on existing user (e.g. update role).
do_action( 'openid-connect-generic-update-user-using-current-claim', $user, $user_claim );

$remember_me = apply_filters( 'openid-connect-generic-remember-me', false, $user, $token_response, $id_token_claim, $user_claim, $subject_identity );
$expiration_days = $remember_me ? 14 : 2;

// Create the WP session, so we know its token.
$expiration = time() + apply_filters( 'auth_cookie_expiration', 2 * DAY_IN_SECONDS, $user->ID, false );
$expiration = time() + apply_filters( 'auth_cookie_expiration', $expiration_days * DAY_IN_SECONDS, $user->ID, false );
$manager = WP_Session_Tokens::get_instance( $user->ID );
$token = $manager->create( $expiration );

// Save the refresh token in the session.
$this->save_refresh_token( $manager, $token, $token_response );

// you did great, have a cookie!
wp_set_auth_cookie( $user->ID, false, '', $token );
wp_set_auth_cookie( $user->ID, $remember_me, '', $token );
do_action( 'wp_login', $user->user_login, $user );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class OpenID_Connect_Generic_Client_Wrapper_Test extends WP_UnitTestCase {
*/
public function setUp(): void {

$this->client_wrapper = OpenID_Connect_Generic::instance()->client_wrapper;

parent::setUp();

}
Expand All @@ -43,4 +45,26 @@ public function test_plugin_client_wrapper_alternate_redirect_uri_parse_request(

}

public function test_plugin_client_wrapper_remember_me() {
// Set the remember me option to true
add_filter( 'openid-connect-generic-remember-me', '__return_true' );

// Create a user and log in using the login function of the client wrapper
$user = $this->factory()->user->create_and_get( array( 'user_login' => 'test-remember-me-user' ) );
$this->client_wrapper->login_user( $user, array(
'expires_in' => 14 * HOUR_IN_SECONDS, // This does not influence the length of the cookie
), array(), array(), '' );

// Retrieve the session tokens
$manager = WP_Session_Tokens::get_instance( $user->ID );
$token = $manager->get_all()[0];

// Assert if the token is set to expire in 14 days, with some seconds as a timing margin
$this->assertGreaterThan( time() + 13 * DAY_IN_SECONDS, $token['expiration'] );
$this->assertLessThan( time() + 15 * DAY_IN_SECONDS, $token['expiration'] );

// Reset the remember me option
remove_filter( 'openid-connect-generic-remember-me', '__return_true' );
}

}
Loading