From 96193a27a4e7c23ebe6fa5414c2f66936c556afa Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 3 Nov 2022 16:17:29 +1100 Subject: [PATCH] Simplify DFC authorisation control I want to add other ways to authenticate for easier testing and possibly more integrations. It will be easier to just test if we got a user or not instead of testing pre-conditions to that as well. --- .../app/controllers/dfc_provider/base_controller.rb | 13 ++----------- .../services/dfc_provider/authorization_control.rb | 2 ++ .../dfc_provider/catalog_items_controller_spec.rb | 4 ++-- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/engines/dfc_provider/app/controllers/dfc_provider/base_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/base_controller.rb index cbd04b1e20..84703f53d4 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/base_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/base_controller.rb @@ -5,23 +5,14 @@ module DfcProvider class BaseController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, with: :not_found - before_action :check_authorization, - :check_user + before_action :check_authorization respond_to :json private def check_authorization - return if access_token.present? - - head :unprocessable_entity - end - - def check_user - return if current_user.present? - - head :unauthorized + head :unauthorized if current_user.nil? end def check_enterprise diff --git a/engines/dfc_provider/app/services/dfc_provider/authorization_control.rb b/engines/dfc_provider/app/services/dfc_provider/authorization_control.rb index beb1885816..c3bec17480 100644 --- a/engines/dfc_provider/app/services/dfc_provider/authorization_control.rb +++ b/engines/dfc_provider/app/services/dfc_provider/authorization_control.rb @@ -9,6 +9,8 @@ module DfcProvider end def process + return unless @access_token + decode_token find_ofn_user end diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/catalog_items_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/catalog_items_controller_spec.rb index 4a38948d10..5e7984bff1 100644 --- a/engines/dfc_provider/spec/controllers/dfc_provider/catalog_items_controller_spec.rb +++ b/engines/dfc_provider/spec/controllers/dfc_provider/catalog_items_controller_spec.rb @@ -93,9 +93,9 @@ describe DfcProvider::CatalogItemsController, type: :controller do end context 'without an authorization token' do - it 'returns unprocessable_entity head' do + it 'returns unauthorized head' do api_get :index, enterprise_id: enterprise.id - expect(response).to be_unprocessable + expect(response).to be_unauthorized end end end