Skip to content

Commit 5253c66

Browse files
committed
fix(Wrapper): Fixes handling of optional expires_in attribute in Access Token
- Fixes #439 - Properly handles `expires_in` being OPTIONAl according to spec. (See: 3.2.2.5. Successful Authentication Response)
1 parent 0038ce7 commit 5253c66

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

includes/openid-connect-generic-client-wrapper.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@ public function refresh_user_claim( $user, $token_response ) {
663663
/**
664664
* Record user meta data, and provide an authorization cookie.
665665
*
666+
* @todo All uses of `expires_in` values to control application session
667+
* length need to be removed as this is not in spec.
668+
*
666669
* @param WP_User $user The user object.
667670
* @param array $token_response The token response.
668671
* @param array $id_token_claim The ID token claim.
@@ -671,7 +674,7 @@ public function refresh_user_claim( $user, $token_response ) {
671674
*
672675
* @return void
673676
*/
674-
public function login_user( $user, $token_response, $id_token_claim, $user_claim, $subject_identity ) {
677+
public function login_user( $user, $token_response, $id_token_claim, $user_claim, $subject_identity ): void {
675678
// Store the tokens for future reference.
676679
update_user_meta( $user->ID, 'openid-connect-generic-last-token-response', $token_response );
677680
update_user_meta( $user->ID, 'openid-connect-generic-last-id-token-claim', $id_token_claim );
@@ -716,6 +719,9 @@ public function login_user( $user, $token_response, $id_token_claim, $user_claim
716719
* openid token refresh expiration. This is applied both when creating the session
717720
* token as well as when wp_set_auth_cookie is called.
718721
*
722+
* @todo This method needs to be remove as token refresh expiration is not
723+
* intended for application sesssion expiration.
724+
*
719725
* @param integer $expiration_in_seconds The expiration time in seconds.
720726
* @return integer
721727
*/
@@ -731,17 +737,19 @@ public function set_cookie_expiration_to_openid_token_refresh_expiration( $expir
731737
* @param string $token The current users session token.
732738
* @param array|WP_Error|null $token_response The authentication token response.
733739
*/
734-
public function save_refresh_token( $manager, $token, $token_response ) {
740+
public function save_refresh_token( $manager, $token, $token_response ): void {
735741
if ( ! $this->settings->token_refresh_enable ) {
736742
return;
737743
}
738744
$session = $manager->get( $token );
739745
$now = time();
746+
740747
$session[ $this->cookie_token_refresh_key ] = array(
741-
'next_access_token_refresh_time' => $token_response['expires_in'] + $now,
748+
'next_access_token_refresh_time' => $now + ( $token_response['expires_in'] ?? 0 ),
742749
'refresh_token' => isset( $token_response['refresh_token'] ) ? $token_response['refresh_token'] : false,
743750
'refresh_expires' => false,
744751
);
752+
745753
if ( isset( $token_response['refresh_expires_in'] ) ) {
746754
$refresh_expires_in = $token_response['refresh_expires_in'];
747755
if ( $refresh_expires_in > 0 ) {

tests/phpunit/includes/openid-connect-generic-client-wrapper_test.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,31 @@
1010
*/
1111
class OpenID_Connect_Generic_Client_Wrapper_Test extends WP_UnitTestCase {
1212

13+
/**
14+
* @var OpenID_Connect_Generic_Client_Wrapper
15+
*/
16+
private $client_wrapper;
17+
18+
/**
19+
* @var WP_User_Meta_Session_Tokens
20+
*/
21+
private $manager;
22+
1323
/**
1424
* Test case setup method.
1525
*
1626
* @return void
1727
*/
1828
public function setUp(): void {
1929

20-
$this->client_wrapper = OpenID_Connect_Generic::instance()->client_wrapper;
21-
2230
parent::setUp();
2331

32+
remove_all_filters( 'session_token_manager' );
33+
$user_id = self::factory()->user->create();
34+
$this->manager = WP_Session_Tokens::get_instance( $user_id );
35+
36+
$this->client_wrapper = OpenID_Connect_Generic::instance()->client_wrapper;
37+
2438
}
2539

2640
/**
@@ -30,6 +44,8 @@ public function setUp(): void {
3044
*/
3145
public function tearDown(): void {
3246

47+
unset( $this->client_wrapper );
48+
3349
parent::tearDown();
3450

3551
}
@@ -108,4 +124,23 @@ public function test_plugin_client_wrapper_token_expiration() {
108124
wp_clear_auth_cookie();
109125
}
110126

127+
/**
128+
* Test proper handling of saving refresh tokens.
129+
*
130+
* @group ClientWrapperTests
131+
*/
132+
public function test_save_refresh_token() {
133+
$expiration = time() + DAY_IN_SECONDS;
134+
$token = $this->manager->create( $expiration );
135+
$session = $this->manager->get( $token );
136+
$refresh_token_info = $session['openid-connect-generic-refresh'];
137+
$refresh_token = $refresh_token_info['refresh_token'];
138+
$token_result = $this->client->request_new_tokens( $refresh_token );
139+
$token_response = $this->client->get_token_response( $token_result );
140+
141+
$this->client_wrapper->save_refresh_token( $this->manager, $token, $token_response );
142+
143+
$this->manager->destroy( $token );
144+
}
145+
111146
}

0 commit comments

Comments
 (0)