Skip to content

Commit

Permalink
Check session expiry to trigger a re-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
gbzodek committed Nov 24, 2023
1 parent 2957c9a commit 000ab74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/shopify_app/controller_concerns/login_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def activate_shopify_session
return redirect_to_login
end

if current_shopify_session.expires && current_shopify_session.expires < Time.now
ShopifyApp::Logger.debug("Session expired, redirecting to login")
clear_shopify_session
return redirect_to_login
end

if ShopifyApp.configuration.reauth_on_access_scope_changes &&
!ShopifyApp.configuration.user_access_scopes_strategy.covers_scopes?(current_shopify_session)
clear_shopify_session
Expand Down
28 changes: 28 additions & 0 deletions test/shopify_app/controller_concerns/login_protection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,34 @@ class LoginProtectionControllerTest < ActionController::TestCase
end
end

test "#activate_shopify_session with an expired Shopify session, redirects to the login url" do
with_application_test_routes do
cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME] = "cookie"
ShopifyApp::SessionRepository.expects(:load_session)
.returns(ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com", expires: 1.minute.ago))

get :index, params: { shop: "foobar" }

assert_redirected_to "/login?shop=foobar.myshopify.com"
assert_nil cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME]
end
end

test "#activate_shopify_session with an expired Shopify session, when the request is an XHR, returns an HTTP 401" do
with_application_test_routes do
cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME] = "cookie"
ShopifyApp::SessionRepository.expects(:load_session)
.returns(ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com", expires: 1.minute.ago))

get :index, params: { shop: "foobar" }, xhr: true

assert_equal 401, response.status
assert_match "1", response.headers["X-Shopify-API-Request-Failure-Reauthorize"]
assert_match "/login?shop=foobar", response.headers["X-Shopify-API-Request-Failure-Reauthorize-Url"]
assert_nil cookies.encrypted[ShopifyAPI::Auth::Oauth::SessionCookie::SESSION_COOKIE_NAME]
end
end

test "#fullpage_redirect_to sends a post message to that shop in the shop param" do
with_application_test_routes do
example_shop = "shop.myshopify.com"
Expand Down

0 comments on commit 000ab74

Please sign in to comment.