php - Google OAuth2: Custom Nonce Parameter Not Passed Back in Redirect - Stack Overflow

I'm working on integrating Google OAuth2 into a WordPress plugin and trying to secure the authoriz

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:

?...&nonce=abcdef123456

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

.php?page=seo-insights-settings&code=...&scope=...

What I've Tried :

  • Ensuring the nonce parameter is properly added to the authorization URL.
  • Logging and validating the generated nonce on both sides.
  • Switching to using the state parameter for validation, as suggested in the Google OAuth2 documentation.

My Questions :

  • Is Google OAuth2 designed to ignore custom parameters like nonce?
  • If state is the only reliable way to pass custom data, how do I handle both state and nonce securely in this flow? Any insights or recommendations for handling this issue securely within Google OAuth2 would be greatly appreciated!

Thanks in advance!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742323105a4422211.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信