Simplify DFC authentication code

This commit is contained in:
Maikel Linke
2022-11-03 16:34:00 +11:00
committed by David Cook
parent 52a98989e0
commit fd274447fe
6 changed files with 12 additions and 22 deletions

View File

@@ -36,7 +36,7 @@ module DfcProvider
end
def current_user
@current_user ||= authorization_control.process
@current_user ||= authorization_control.user
end
def authorization_control

View File

@@ -8,17 +8,14 @@ module DfcProvider
@request = request
end
def process
def user
oidc_user || ofn_user
end
private
def oidc_user
return unless access_token
decode_token
find_ofn_user
find_ofn_user(decode_token) if access_token
end
def ofn_user
@@ -26,22 +23,15 @@ module DfcProvider
end
def decode_token
data = JWT.decode(
access_token,
nil,
false
)
@header = data.last
@payload = data.first
JWT.decode(access_token, nil, false).first
end
def access_token
@request.headers['Authorization'].to_s.split(' ').last
end
def find_ofn_user
Spree::User.where(email: @payload['email']).first
def find_ofn_user(payload)
Spree::User.find_by(email: payload["email"])
end
end
end

View File

@@ -19,7 +19,7 @@ describe DfcProvider::CatalogItemsController, type: :controller do
context 'with an authenticated user' do
before do
allow_any_instance_of(DfcProvider::AuthorizationControl)
.to receive(:process)
.to receive(:user)
.and_return(user)
end
@@ -83,7 +83,7 @@ describe DfcProvider::CatalogItemsController, type: :controller do
context 'without an authenticated user' do
it 'returns unauthorized head' do
allow_any_instance_of(DfcProvider::AuthorizationControl)
.to receive(:process)
.to receive(:user)
.and_return(nil)
api_get :index, enterprise_id: 'default'
@@ -117,7 +117,7 @@ describe DfcProvider::CatalogItemsController, type: :controller do
context 'with an authenticated user' do
before do
allow_any_instance_of(DfcProvider::AuthorizationControl)
.to receive(:process)
.to receive(:user)
.and_return(user)
end

View File

@@ -18,7 +18,7 @@ describe DfcProvider::EnterprisesController, type: :controller do
context 'with an authenticated user' do
before do
allow_any_instance_of(DfcProvider::AuthorizationControl)
.to receive(:process)
.to receive(:user)
.and_return(user)
end

View File

@@ -16,7 +16,7 @@ describe DfcProvider::PersonsController, type: :controller do
context 'with an authenticated user' do
before do
allow_any_instance_of(DfcProvider::AuthorizationControl)
.to receive(:process)
.to receive(:user)
.and_return(user)
end

View File

@@ -19,7 +19,7 @@ describe DfcProvider::SuppliedProductsController, type: :controller do
context 'with an authenticated user' do
before do
allow_any_instance_of(DfcProvider::AuthorizationControl)
.to receive(:process)
.to receive(:user)
.and_return(user)
end