mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-02 21:57:17 +00:00
Simplify DFC authentication code
This commit is contained in:
@@ -36,7 +36,7 @@ module DfcProvider
|
||||
end
|
||||
|
||||
def current_user
|
||||
@current_user ||= authorization_control.process
|
||||
@current_user ||= authorization_control.user
|
||||
end
|
||||
|
||||
def authorization_control
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user