From 8a3b1292927b51a5a7b3fd51b03c7da332b911a7 Mon Sep 17 00:00:00 2001 From: Shivam Kumar Date: Tue, 9 Jun 2026 12:42:03 +0530 Subject: [PATCH] fix: Disable re-oauth flow for manual whatsapp (#13599) Restrict the WhatsApp reauthorization flag to channels whose provider_config source is 'embedded_signup'. Previously the view exposed resource.channel.reauthorization_required? for all WhatsApp channels; now it only returns true when (provider_config || {}).to_h['source'] == 'embedded_signup' && resource.channel.reauthorization_required?. This prevents showing reauthorization prompts for manual/API-key flows (non-OAuth) and safely handles nil provider_config. # Pull Request Template ## Description Please include a summary of the change and issue(s) fixed. Also, mention relevant motivation, context, and any dependencies that this change requires. Fixes #13553 ## Type of change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com> --- app/jobs/webhooks/whatsapp_events_job.rb | 7 ++++- app/views/api/v1/models/_inbox.json.jbuilder | 6 +++- .../v1/accounts/inboxes_controller_spec.rb | 29 +++++++++++++++++++ .../jobs/webhooks/whatsapp_events_job_spec.rb | 8 +++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/app/jobs/webhooks/whatsapp_events_job.rb b/app/jobs/webhooks/whatsapp_events_job.rb index f904b3723..14429e61c 100644 --- a/app/jobs/webhooks/whatsapp_events_job.rb +++ b/app/jobs/webhooks/whatsapp_events_job.rb @@ -126,12 +126,17 @@ class Webhooks::WhatsappEventsJob < MutexApplicationJob def channel_is_inactive?(channel) return true if channel.blank? - return true if channel.reauthorization_required? + # Only skip for embedded signup when reauth is required; manual flow uses API keys and should still receive webhooks + return true if channel.reauthorization_required? && embedded_signup_channel?(channel) return true unless channel.account.active? false end + def embedded_signup_channel?(channel) + (channel.provider_config || {}).to_h['source'] == 'embedded_signup' + end + def find_channel_by_url_param(params) return unless params[:phone_number] diff --git a/app/views/api/v1/models/_inbox.json.jbuilder b/app/views/api/v1/models/_inbox.json.jbuilder index 0ae0745cd..b34bbe95b 100644 --- a/app/views/api/v1/models/_inbox.json.jbuilder +++ b/app/views/api/v1/models/_inbox.json.jbuilder @@ -130,7 +130,11 @@ json.bot_name resource.channel.try(:bot_name) if resource.telegram? if resource.whatsapp? json.message_templates resource.channel.try(:message_templates) json.provider_config resource.channel.try(:provider_config) if Current.account_user&.administrator? - json.reauthorization_required resource.channel.try(:reauthorization_required?) + # Only show reauthorization for embedded signup; manual flow uses API keys, not OAuth + json.reauthorization_required( + (resource.channel.try(:provider_config) || {}).to_h['source'] == 'embedded_signup' && + resource.channel.try(:reauthorization_required?) + ) end ## Voice attributes for TwilioSms diff --git a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb index 9e03e2587..0fd6ad7bf 100644 --- a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb @@ -100,6 +100,35 @@ RSpec.describe 'Inboxes API', type: :request do expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(inbox.id) end + it 'returns reauthorization_required for embedded signup whatsapp channel when reauth required' do + whatsapp_channel = create(:channel_whatsapp, account: account, provider: 'whatsapp_cloud', sync_templates: false, + validate_provider_config: false) + whatsapp_inbox = create(:inbox, channel: whatsapp_channel, account: account) + whatsapp_channel.prompt_reauthorization! + + get "/api/v1/accounts/#{account.id}/inboxes/#{whatsapp_inbox.id}", + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.parsed_body['reauthorization_required']).to be(true) + end + + it 'does not flag reauthorization_required for manual whatsapp channel even when reauth required' do + whatsapp_channel = create(:channel_whatsapp, account: account, provider: 'whatsapp_cloud', sync_templates: false, + validate_provider_config: false) + whatsapp_channel.update!(provider_config: whatsapp_channel.provider_config.merge('source' => 'manual')) + whatsapp_inbox = create(:inbox, channel: whatsapp_channel, account: account) + whatsapp_channel.prompt_reauthorization! + + get "/api/v1/accounts/#{account.id}/inboxes/#{whatsapp_inbox.id}", + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.parsed_body['reauthorization_required']).to be(false) + end + it 'returns the inbox if assigned inbox is assigned as agent' do create(:inbox_member, user: agent, inbox: inbox) get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}", diff --git a/spec/jobs/webhooks/whatsapp_events_job_spec.rb b/spec/jobs/webhooks/whatsapp_events_job_spec.rb index d82658102..8d1b24b52 100644 --- a/spec/jobs/webhooks/whatsapp_events_job_spec.rb +++ b/spec/jobs/webhooks/whatsapp_events_job_spec.rb @@ -62,6 +62,14 @@ RSpec.describe Webhooks::WhatsappEventsJob do job.perform_now(params) end + it 'still enqueues for manual channels even when reauthorization required' do + channel.update!(provider_config: channel.provider_config.merge('source' => 'manual')) + channel.prompt_reauthorization! + allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service) + expect(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new) + job.perform_now(params) + end + it 'will not enqueue if channel is not present' do allow(Whatsapp::IncomingMessageWhatsappCloudService).to receive(:new).and_return(process_service) allow(Whatsapp::IncomingMessageService).to receive(:new).and_return(process_service)