Skip to content

Commit

Permalink
Change logic of jwks refresh (#7)
Browse files Browse the repository at this point in the history
* Change logic of jwks refresh

* Update for 1.5.1

* Bump tested up to version

* Whitespace
  • Loading branch information
andrewheberle authored Nov 9, 2023
1 parent d568e2f commit 39eb3bf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ah-jwt-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @wordpress-plugin
* Plugin Name: AH JWT Auth
* Description: This plugin allows sign in to WordPress using a JSON Web Token (JWT) contained in a HTTP Header
* Version: 1.5.0
* Version: 1.5.1
* Author: Andrew Heberle
* Text Domain: ah-jwt-auth
* Author URI: https://github.com/andrewheberle/wordpress-ah-jwt-auth/
Expand Down
46 changes: 27 additions & 19 deletions includes/class-ahjwtauthsignin.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,22 @@ private function ahjwtauth_refresh_jwks() {
}

// retrieve json from JWKS URL with caching.
$keys = get_transient( 'ahjwtauth_jwks' );
$json = get_transient( 'ahjwtauth_jwks_json' );

// Does transient exist?
if ( false !== $keys ) {
return $keys;
if ( false !== $json ) {
// try to decode json.
$jwks = @json_decode( $json, true );
if ( null === $jwks ) {
$this->error = __( 'AH JWT Auth cannot decode the JSON retrieved from the JWKS URL', 'ah-jwt-auth' );
error_log( 'AH JWT Auth: ERROR: cannot decode the JSON retrieved from the JWKS URL' );
return false;
}

return $jwks;
}

// if transient did not exist, attempt to get url.
$jwks_url = get_option( 'ahjwtauth-jwks-url' );
$response = wp_remote_get( $jwks_url );
if ( is_wp_error( $response ) ) {
$this->error = __( 'AH JWT Auth: error retrieving the JWKS URL', 'ah-jwt-auth' );
Expand All @@ -172,7 +179,7 @@ private function ahjwtauth_refresh_jwks() {

// check that response was not empty.
if ( '' === $json ) {
$this->error = __( 'AH JWT Auth could not retrieve the specified JWKS URL', 'ah-jwt-auth' );
$this->error = __( 'AH JWT Auth could not retrieve the specified JWKS URL', 'ah-jwt-auth' );
error_log( 'AH JWT Auth: ERROR: could not retrieve the specified JWKS URL' );
return false;
}
Expand All @@ -185,21 +192,11 @@ private function ahjwtauth_refresh_jwks() {
return false;
}

// parse the JWKS response.
try {
$keys = JWK::parseKeySet( array( 'keys' => $jwks['keys'] ) );
} catch ( Exception $e ) {
$this->error = $e->getMessage();
error_log( 'AH JWT Auth: ERROR: Problem parsing key-set: ' . $e->getMessage() );
error_log( $json );
return false;
}

// cache JWKS for future.
set_transient( 'ahjwtauth_jwks', $keys, WEEK_IN_SECONDS );
// cache JWKS JSON for future.
set_transient( 'ahjwtauth_jwks_json', $json, WEEK_IN_SECONDS );

// return key set.
return $keys;
return $jwks;
}

/**
Expand Down Expand Up @@ -269,7 +266,18 @@ private function verify_token( $jwt ) {
private function get_key() {
$jwks_url = get_option( 'ahjwtauth-jwks-url' );
if ( '' !== $jwks_url ) {
return $this->ahjwtauth_refresh_jwks();
$jwks = $this->ahjwtauth_refresh_jwks();

try {
$keys = JWK::parseKeySet( array( 'keys' => $jwks['keys'] ) );
} catch ( Exception $e ) {
$this->error = $e->getMessage();
error_log( 'AH JWT Auth: ERROR: Problem parsing key-set: ' . $e->getMessage() );
error_log( $json );
return false;
}

return $keys;
}

// otherwise use shared secret.
Expand Down
7 changes: 5 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: andrewheberle
Donate link: https://paypal.me/andrewheberle
Tags: jwt, sso, login, auth, authentication
Requires at least: 4.7
Tested up to: 6.2.2
Stable tag: 1.4.1
Tested up to: 6.3.2
Stable tag: 1.5.1
Requires PHP: 7.0
License: GPLv3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Expand Down Expand Up @@ -57,6 +57,9 @@ Currently only the HS256 and RS256 alorithms are supported.

== Changelog ==

= 1.5.1 =
* Fixes for JWKS refresh process

= 1.5.0 =
* Add WP cron job to refresh JWKS daily

Expand Down

0 comments on commit 39eb3bf

Please sign in to comment.