Skip to content

Commit

Permalink
Merge pull request #153 from Shopify/limit_session_return_to
Browse files Browse the repository at this point in the history
Return an HTTP 401 for XHRs that aren't logged in
  • Loading branch information
nickhoffman committed Aug 18, 2015
2 parents 260d3df + 082f733 commit 1fef25d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
6.2.0
-----

* Return an HTTP 401 for XHRs that aren't logged in

6.1.3
-----
* add redirect_uri which is now required
Expand Down
8 changes: 6 additions & 2 deletions lib/shopify_app/login_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ def login_again_if_different_shop
protected

def redirect_to_login
session[:return_to] = request.fullpath if request.get?
redirect_to login_path(shop: params[:shop])
if request.xhr?
head :unauthorized
else
session[:return_to] = request.fullpath if request.get?
redirect_to login_path(shop: params[:shop])
end
end

def close_session
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify_app/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ShopifyApp
VERSION = "6.1.3"
VERSION = '6.2.0'
end
22 changes: 22 additions & 0 deletions test/shopify_app/login_protection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class LoginProtectionController < ActionController::Base
include ShopifyApp::LoginProtection
helper_method :shop_session

around_action :shopify_session, only: [:index]
before_action :login_again_if_different_shop, only: [:second_login]

def index
Expand Down Expand Up @@ -64,6 +65,27 @@ class LoginProtectionTest < ActionController::TestCase
end
end

test '#shopify_session with no Shopify session, redirects to the login path' do
with_application_test_routes do
get :index, shop: 'foobar'
assert_redirected_to @controller.send(:login_path, shop: 'foobar')
end
end

test '#shopify_session with no Shopify session, sets session[:return_to]' do
with_application_test_routes do
get :index, shop: 'foobar'
assert_equal '/?shop=foobar', session[:return_to]
end
end

test '#shopify_session with no Shopify session, when the request is an XHR, returns an HTTP 401' do
with_application_test_routes do
xhr :get, :index, shop: 'foobar'
assert_equal 401, response.status
end
end

private

def with_application_test_routes
Expand Down

0 comments on commit 1fef25d

Please sign in to comment.