Skip to content

Commit

Permalink
Merge pull request #8 from newfold-labs/fix/sso-persistent-cache
Browse files Browse the repository at this point in the history
Fallback to an option when transients fail
  • Loading branch information
wpscholar authored Apr 18, 2024
2 parents 9e77291 + 5648cb0 commit 3281a2a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@
},
"require": {
"newfold-labs/wp-module-data": ">=2.1"
},
"require-dev": {
"newfold-labs/wp-php-standards": "^1.2"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
20 changes: 16 additions & 4 deletions includes/SSO_Helpers_Legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static function handleLegacyLogin( $nonce, $salt ) {
exit;
}

$has_epoch = preg_match( '/-e(\d+)$/', $nonce, $epoch );
$expired = ( $has_epoch && ( time() - $epoch[1] ) > 300 ) ? true : false;

// Too many failed attempts
if ( self::shouldThrottle() ) {
self::triggerFailure();
Expand All @@ -36,8 +39,13 @@ public static function handleLegacyLogin( $nonce, $salt ) {
}

// Validate token
$token = substr( base64_encode( hash( 'sha256', $nonce . $salt, false ) ), 0, 64 );
if ( get_transient( 'sso_token' ) !== $token ) {
$token = substr( base64_encode( hash( 'sha256', $nonce . $salt, false ) ), 0, 64 );
$stored_token = get_transient( 'sso_token' );
if ( false === $stored_token ) {
$stored_token = get_option( 'sso_token' );
delete_option( 'sso_token' );
}
if ( $expired || $stored_token !== $token ) {
self::triggerFailure();
exit;
}
Expand Down Expand Up @@ -69,13 +77,17 @@ public static function getUser() {

// If user wasn't found, find first admin user
if ( ! $user ) {
$users = get_users( array( 'role' => 'administrator', 'number' => 1 ) );
$users = get_users(
array(
'role' => 'administrator',
'number' => 1,
)
);
if ( isset( $users[0] ) && is_a( $users[0], 'WP_User' ) ) {
$user = $users[0];
}
}

return $user;
}

}

0 comments on commit 3281a2a

Please sign in to comment.