Track refresh token expiration (fixes #51)

isekai
PJ Eby 7 years ago
parent caf1324499
commit 662e9fb25b

@ -141,18 +141,22 @@ class OpenID_Connect_Generic_Client_Wrapper {
$refresh_token_info = $session[ $this->cookie_token_refresh_key ]; $refresh_token_info = $session[ $this->cookie_token_refresh_key ];
$next_access_token_refresh_time = $refresh_token_info[ 'next_access_token_refresh_time' ]; $next_access_token_refresh_time = $refresh_token_info[ 'next_access_token_refresh_time' ];
$refresh_token = $refresh_token_info[ 'refresh_token' ];
if ( $current_time < $next_access_token_refresh_time ) { if ( $current_time < $next_access_token_refresh_time ) {
return; return;
} }
if ( ! $refresh_token ) { $refresh_token = $refresh_token_info[ 'refresh_token' ];
$refresh_expires = $refresh_token_info[ 'refresh_expires' ];
if ( ! $refresh_token || ( $refresh_expires && $current_time > $refresh_expires ) ) {
wp_logout(); wp_logout();
if ( $this->settings->redirect_on_logout ) { if ( $this->settings->redirect_on_logout ) {
$this->error_redirect( new WP_Error( 'access-token-expired', __( 'Session expired. Please login again.' ) ) ); $this->error_redirect( new WP_Error( 'access-token-expired', __( 'Session expired. Please login again.' ) ) );
} }
return;
} }
$token_result = $this->client->request_new_tokens( $refresh_token ); $token_result = $this->client->request_new_tokens( $refresh_token );
@ -438,10 +442,20 @@ class OpenID_Connect_Generic_Client_Wrapper {
*/ */
function save_refresh_token( $manager, $token, $token_response ) { function save_refresh_token( $manager, $token, $token_response ) {
$session = $manager->get($token); $session = $manager->get($token);
$now = current_time( 'timestamp' , TRUE );
$session[$this->cookie_token_refresh_key] = array( $session[$this->cookie_token_refresh_key] = array(
'next_access_token_refresh_time' => $token_response['expires_in'] + current_time( 'timestamp' , TRUE ), 'next_access_token_refresh_time' => $token_response['expires_in'] + $now,
'refresh_token' => isset( $token_response[ 'refresh_token' ] ) ? $token_response[ 'refresh_token' ] : false 'refresh_token' => isset( $token_response[ 'refresh_token' ] ) ? $token_response[ 'refresh_token' ] : false,
'refresh_expires' => false,
); );
if ( isset( $token_response[ 'refresh_expires_in' ] ) ) {
$refresh_expires_in = $token_response[ 'refresh_expires_in' ];
if ($refresh_expires_in > 0) {
// leave enough time for the actual refresh request to go through
$refresh_expires = $now + $refresh_expires_in - $this->alter_http_request_timeout(5);
$session[$this->cookie_token_refresh_key]['refresh_expires'] = $refresh_expires;
}
}
$manager->update($token, $session); $manager->update($token, $session);
return; return;
} }

Loading…
Cancel
Save