Skip to content

Commit 1fef25d

Browse files
committed
Merge pull request #153 from Shopify/limit_session_return_to
Return an HTTP 401 for XHRs that aren't logged in
2 parents 260d3df + 082f733 commit 1fef25d

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
6.2.0
2+
-----
3+
4+
* Return an HTTP 401 for XHRs that aren't logged in
5+
16
6.1.3
27
-----
38
* add redirect_uri which is now required

lib/shopify_app/login_protection.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ def login_again_if_different_shop
3535
protected
3636

3737
def redirect_to_login
38-
session[:return_to] = request.fullpath if request.get?
39-
redirect_to login_path(shop: params[:shop])
38+
if request.xhr?
39+
head :unauthorized
40+
else
41+
session[:return_to] = request.fullpath if request.get?
42+
redirect_to login_path(shop: params[:shop])
43+
end
4044
end
4145

4246
def close_session

lib/shopify_app/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ShopifyApp
2-
VERSION = "6.1.3"
2+
VERSION = '6.2.0'
33
end

test/shopify_app/login_protection_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class LoginProtectionController < ActionController::Base
66
include ShopifyApp::LoginProtection
77
helper_method :shop_session
88

9+
around_action :shopify_session, only: [:index]
910
before_action :login_again_if_different_shop, only: [:second_login]
1011

1112
def index
@@ -64,6 +65,27 @@ class LoginProtectionTest < ActionController::TestCase
6465
end
6566
end
6667

68+
test '#shopify_session with no Shopify session, redirects to the login path' do
69+
with_application_test_routes do
70+
get :index, shop: 'foobar'
71+
assert_redirected_to @controller.send(:login_path, shop: 'foobar')
72+
end
73+
end
74+
75+
test '#shopify_session with no Shopify session, sets session[:return_to]' do
76+
with_application_test_routes do
77+
get :index, shop: 'foobar'
78+
assert_equal '/?shop=foobar', session[:return_to]
79+
end
80+
end
81+
82+
test '#shopify_session with no Shopify session, when the request is an XHR, returns an HTTP 401' do
83+
with_application_test_routes do
84+
xhr :get, :index, shop: 'foobar'
85+
assert_equal 401, response.status
86+
end
87+
end
88+
6789
private
6890

6991
def with_application_test_routes

0 commit comments

Comments
 (0)