diff --git a/app/controllers/api/v1/accounts/integrations/captain_controller.rb b/app/controllers/api/v1/accounts/integrations/captain_controller.rb index 7df0e61df..7e65b0d48 100644 --- a/app/controllers/api/v1/accounts/integrations/captain_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/captain_controller.rb @@ -1,22 +1,25 @@ class Api::V1::Accounts::Integrations::CaptainController < Api::V1::Accounts::BaseController before_action :check_admin_authorization? - before_action :fetch_hook + before_action :hook - def sso_url - params_string = - "token=#{URI.encode_www_form_component(@hook['settings']['access_token'])}" \ - "&email=#{URI.encode_www_form_component(@hook['settings']['account_email'])}" \ - "&account_id=#{URI.encode_www_form_component(@hook['settings']['account_id'])}" + def proxy + base_url = InstallationConfig.find_by(name: 'CAPTAIN_API_URL').value + url = "#{base_url}/api/v1/accounts/#{hook.settings[:account_id]}/#{params[:route]}" - installation_config = InstallationConfig.find_by(name: 'CAPTAIN_APP_URL') + # make the request to the Captain service + # also add the access token and email to header use X-User-Email and X-User-Token + # + response = HTTParty.send(params[:method].downcase, url, body: params[:body], headers: { + 'X-User-Email' => hook.settings[:account_email], + 'X-User-Token' => hook.settings[:access_token] + }) - sso_url = "#{installation_config.value}/sso?#{params_string}" - render json: { sso_url: sso_url }, status: :ok + render json: response.body, status: response.code end private - def fetch_hook - @hook = Current.account.hooks.find_by!(app_id: 'captain') + def hook + @hook ||= Current.account.hooks.find_by!(app_id: 'captain') end end diff --git a/config/routes.rb b/config/routes.rb index 7006e8f4b..1c913155b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -219,7 +219,7 @@ Rails.application.routes.draw do resources :apps, only: [:index, :show] resource :captain, controller: 'captain', only: [] do collection do - get :sso_url + post :proxy end end resources :hooks, only: [:show, :create, :update, :destroy] do