-
Notifications
You must be signed in to change notification settings - Fork 685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate store sessions #1612
Validate store sessions #1612
Conversation
client = ShopifyAPI::Clients::Rest::Admin.new(session: installed_shop_session) | ||
client.get(path: "shop") | ||
rescue ShopifyAPI::Errors::HttpResponseError => error | ||
redirect_for_embedded if error.code == 401 # unauthorized due to uninstall |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be a redirected_for_embedded
situation? I think we would call this when NOT embedded, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ya, that's not clear because the conditional for this method is listed in the before_action
declaration. I'll fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it, we should only be doing this session sanity check if it is embedded, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should redirect to OAuth, no?
Also the comment here might be misleading - there could be a variety of reasons the token is no longer valid and not just because of uninstall (keys can be revoked, token could be expired, etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should redirect to oauth, which we sadly call login_url
for reasons 🤦
f26bf41
to
e13f5a4
Compare
bb3610f
to
debe184
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, only had non-blocking comments!
|
||
client = ShopifyAPI::Clients::Rest::Admin.new(session: installed_shop_session) | ||
client.get(path: "shop") | ||
rescue ShopifyAPI::Errors::HttpResponseError => error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we re-throw if the error isn't a 401 here? Unlikely, but may happen 🤷
@@ -58,5 +63,14 @@ def shop_login | |||
|
|||
url.to_s | |||
end | |||
|
|||
def validate_non_embedded_session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think calling this validate_session_before_embedding
would make it easier to understand this method?
@@ -61,6 +66,36 @@ def index | |||
assert_response :ok | |||
end | |||
|
|||
test "redirects to login_url (oauth path) to reinstall the app if the store's session token is no longer valid" do | |||
ShopifyApp.configuration.stubs(:embedded_app?).returns(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can actually change the configuration value as part of the tests, I'm pretty sure we reset them for each test.
shopify_domain = "shop1.myshopify.com" | ||
get :index, params: { shop: shopify_domain } | ||
|
||
assert_response :redirect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we assert that the location we go to is /login
?
36a30f7
to
7f88b9f
Compare
@nelsonwittwer @paulomarg this change checks whether the token is valid but callback_controller does not update the offline token. From what I see if store_session_storage is not empty, same values stay and database not updated with new token value. Could you please confirm whether this is the case? |
* Check shop offline session is still valid when embedded * only redirect on 401 and use better naming * use guard clause instead of conditional filter * redirect to shop_login if token failed * update tests to match expected behavior * Changelog updates * raise error if not 401 * better redirect test
Will all the documentation that refers to Also, does
|
What this PR does
When a store uninstalls an app we are currently dependent upon the uninstall webhook to tell our apps that a store should no longer be able to access the API / app resources. This leads to apps getting rejected using our templates as reported in #1591 .
This PR will do a lightweight API call after it has found the shop session in storage to ensure it is still valid. If it is not, it will redirect to the embedded login url.
Reviewer's guide to testing
My 🎩 testing looked like this:
http://localhost:62121/?shop=test-nelson27.myshopify.com&host=dGVzdC1uZWxzb24yNy5teXNob3BpZnkuY29tL2FkbWlu
Checklist
Before submitting the PR, please consider if any of the following are needed:
CHANGELOG.md
if the changes would impact usersREADME.md
, if appropriate./docs
, if necessary