Tmeister / wp-api-jwt-auth

A simple plugin to add JSON Web Token (JWT) Authentication to WP REST API
GNU General Public License v2.0
546 stars 160 forks source link

jwt_auth_bad_iss response - Headless WordPress #301

Open phil-sola opened 3 months ago

phil-sola commented 3 months ago

jwt_auth_bad_iss response - Headless WordPress

Expected Behavior

The JWT Authentication for the WP REST API plugin gives a filter to filter the token before sign using 'jwt_auth_token_before_sign'. This is good for headless wordpress sites that change the Site Address (home_url) to the headless frontend site (not WP).

Unfortunately, the filter is irrelevant, as when you go to validate the token, it checks (hardcoded - no filter to change) if the iss matches get_bloginfo('url') (the home_url).

So I can't filter the iss address from get_bloginfo('url') as it would be ignored anyway and throw an error as soon as I go to validate the filtered token.

The issue is within the validate_token function starting at line 362 in the public/class-jwt-auth-public.php file here:

/** The Token is decoded now validate the iss */
if ( $token->iss !== get_bloginfo( 'url' ) ) {
    /** The iss do not match, return error */
    return new WP_Error(
        'jwt_auth_bad_iss',
        'The iss do not match with this server',
        [
        'status' => 403,
       ]
    );
}

Please describe the behavior you are expecting.

I would expect that the ISS should use site_url or get_bloginfo('wpurl') if anything as this will always point to the WP site, whereas home_url is always likely to change for a headless site, which is typically what this plugin would be used for.

It would be great if this could be changed to work with the WordPress admin URL instead of the frontend of the site.