feat: Switch to OAuth 2.0 authentication for Google Merchant API

- Replace service account authentication with OAuth 2.0 user flow
- Add "Authorize with Google" button in admin settings
- Handle OAuth callback and token exchange
- Store refresh token for automatic access token renewal
- Add revoke authorization functionality
- Update admin UI to show authorization status
- Update price updater to use new OAuth credentials
- Add CSRF protection with state parameter

This change supports organizations that have disabled service account
key creation via iam.disableServiceAccountKeyCreation policy.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-21 08:52:57 +01:00
parent d1f3607895
commit e313fce197
4 changed files with 456 additions and 140 deletions

View File

@@ -58,19 +58,21 @@ class Informatiq_SP_Price_Updater {
);
// Get plugin settings.
$merchant_id = get_option( 'informatiq_sp_merchant_id' );
$service_account = get_option( 'informatiq_sp_service_account' );
$minimum_margin = (float) get_option( 'informatiq_sp_minimum_margin', 10 );
$merchant_id = get_option( 'informatiq_sp_merchant_id' );
$client_id = get_option( 'informatiq_sp_client_id' );
$client_secret = get_option( 'informatiq_sp_client_secret' );
$refresh_token = get_option( 'informatiq_sp_refresh_token' );
$minimum_margin = (float) get_option( 'informatiq_sp_minimum_margin', 10 );
// Validate settings.
if ( empty( $merchant_id ) || empty( $service_account ) ) {
$this->logger->error( 'Google Merchant settings not configured. Please configure settings first.' );
if ( empty( $merchant_id ) || empty( $refresh_token ) ) {
$this->logger->error( 'Google Merchant settings not configured. Please configure and authorize first.' );
return $results;
}
// Initialize Google API.
try {
$google_api = new Informatiq_SP_Google_API( $merchant_id, $service_account, $this->logger );
$google_api = new Informatiq_SP_Google_API( $merchant_id, $client_id, $client_secret, $refresh_token, $this->logger );
} catch ( Exception $e ) {
$this->logger->error( 'Failed to initialize Google API: ' . $e->getMessage() );
return $results;
@@ -362,19 +364,21 @@ class Informatiq_SP_Price_Updater {
}
// Get settings.
$merchant_id = get_option( 'informatiq_sp_merchant_id' );
$service_account = get_option( 'informatiq_sp_service_account' );
$minimum_margin = (float) get_option( 'informatiq_sp_minimum_margin', 10 );
$merchant_id = get_option( 'informatiq_sp_merchant_id' );
$client_id = get_option( 'informatiq_sp_client_id' );
$client_secret = get_option( 'informatiq_sp_client_secret' );
$refresh_token = get_option( 'informatiq_sp_refresh_token' );
$minimum_margin = (float) get_option( 'informatiq_sp_minimum_margin', 10 );
if ( empty( $merchant_id ) || empty( $service_account ) ) {
if ( empty( $merchant_id ) || empty( $refresh_token ) ) {
return array(
'success' => false,
'message' => 'Google Merchant settings not configured',
'message' => 'Google Merchant settings not configured or not authorized',
);
}
try {
$google_api = new Informatiq_SP_Google_API( $merchant_id, $service_account, $this->logger );
$google_api = new Informatiq_SP_Google_API( $merchant_id, $client_id, $client_secret, $refresh_token, $this->logger );
$updated = $this->process_single_product( $product, $google_api, $minimum_margin );
return array(