Skip to content

Commit

Permalink
ensure doorkeeper_token is valid when authenticating requests in v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Morantron authored and damianlegawiec committed Oct 14, 2020
1 parent c8b7557 commit e43643a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion api/app/controllers/spree/api/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ def render_error_payload(error, status = 422)
end

def spree_current_user
@spree_current_user ||= Spree.user_class.find_by(id: doorkeeper_token.resource_owner_id) if doorkeeper_token
return nil unless doorkeeper_token
return @spree_current_user if @spree_current_user

doorkeeper_authorize!

@spree_current_user ||= Spree.user_class.find_by(id: doorkeeper_token.resource_owner_id)
end

def spree_authorize!(action, subject, *args)
Expand Down
2 changes: 1 addition & 1 deletion api/lib/spree/api/testing_support/v2/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:headers_order_token) { { 'X-Spree-Order-Token' => order.token } }
end

[200, 201, 400, 404, 403, 422].each do |status_code|
[200, 201, 400, 401, 404, 403, 422].each do |status_code|
shared_examples "returns #{status_code} HTTP status" do
it "returns #{status_code}" do
expect(response.status).to eq(status_code)
Expand Down
15 changes: 15 additions & 0 deletions api/spec/requests/spree/api/v2/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@
expect(json_response['error']).to eq('You are not authorized to access this page.')
end
end

context 'expired token failure' do
let(:user) { create(:user) }
let(:headers) { headers_bearer }

include_context 'API v2 tokens'

before do
token.expires_in = -1
token.save
get '/api/v2/storefront/account', headers: headers
end

it_behaves_like 'returns 401 HTTP status'
end
end

0 comments on commit e43643a

Please sign in to comment.