Panganino
Panganino

Reputation: 150

Google OAuth2: Custom Nonce Parameter Not Passed Back in Redirect

I'm working on integrating Google OAuth2 into a WordPress plugin and trying to secure the authorization flow using a custom nonce parameter. Here’s what I’ve done:

1. Generated the nonce and added it to the authorization URL using add_query_arg:

nonce = wp_create_nonce('seo_insights_auth_nonce');
$authUrl = $client->createAuthUrl();
$authUrl = add_query_arg('nonce', $nonce, $authUrl);

2. Stored the nonce in the WordPress database for validation after the callback:

update_option('seo_insights_auth_nonce', $nonce);

3. Checked the nonce on the callback:

$stored_nonce = get_option('seo_insights_auth_nonce');
$received_nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : '';
if (!$stored_nonce || $received_nonce !== $stored_nonce) {
    error_log("Invalid or missing nonce. Stored: $stored_nonce, Received: $received_nonce");
    return;
}

However, after Google redirects back to my plugin, the nonce parameter is missing from the URL. Here’s the authorization URL being generated:

https://accounts.google.com/o/oauth2/v2/auth?...&nonce=abcdef123456

And here’s the URL received after Google redirects back:

https://my-site.com/wp-admin/admin.php?page=seo-insights-settings&code=...&scope=...

What I've Tried :

My Questions :

Thanks in advance!

Upvotes: 0

Views: 45

Answers (0)

Related Questions