Skip to content
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

Fix omniauth strategy not being set correctly for apps using session tokens #1164

Merged
merged 1 commit into from
Jan 27, 2021

Conversation

rezaansyed
Copy link
Contributor

@rezaansyed rezaansyed commented Jan 26, 2021

What is the issue?

Currently, new apps using shopify_app have session tokens enabled and cookies based auth disabled by default. The problem is that even at the top level actions where cookies are accessible and appropriate to use, LoginProtection gates it's usage in user_session_by_cookie and shop_session_by_cookie using the allow_cookie_authentication setting. This has led to calls for the user_session and shop_session resulting in nil, leading the app to falsely believe that it doesn't have an offline and/or online token.

OAuth redirect loops for offline tokens

  1. [/login] Initiate OAuth to fetch the offline/shop access token
  2. Goes through OAuth process
  3. [/callback] Store the offline access token
  4. [/callback] Respond with redirect to login to fetch online token
  5. 🔥 [/login] Fails to set session[:user_tokens] to true as shop_session check returns nil
  6. OmniAuth falsely assumes app requires an offline token and starts process to fetch one
  7. Repeat steps 1-6 until Shopify stops looping process

OAuth redirect loops for online tokens

  1. [/login] Initiate OAuth to fetch the online/user access token
  2. Goes through OAuth process
  3. [/callback] Store the online access token
  4. [/callback] Checks to see if it needs to start online token flow
  5. 🔥 [/callback] user_session results in nil
  6. [/callback] Respond with redirect to login to fetch online token
  7. Repeat steps 1-6 until Shopify stops looping process

What is the fix?

SessionsController and CallbackController can both operate from the top-level. Use of session cookies are justified in these scenarios.

OAuth redirect loops for offline tokens

Override the shop_session_by_cookie method in SessionsController to ensure we are still using session cookies as before on the top level.

OAuth redirect loops for online tokens

Override the user_session_by_cookie method in CallbackController to ensure we are still using session cookies as before on the top level.

🎩

Before

Screen.Recording.2021-01-26.at.4.30.31.PM.mov

After

Screen.Recording.2021-01-26.at.4.22.26.PM.mov

Before submitting the PR, please consider if any of the following are needed:

  • Update CHANGELOG.md if the changes would impact users
  • Update README.md, if appropriate.
  • Update any relevant pages in docs/, if necessary
  • For security fixes, the Disclosure Policy must be followed.

Copy link
Contributor

@NabeelAhsen NabeelAhsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the overrides here, but let's mark follow-up work to see if we can refactor the *_session_by_cookie methods to behave differently under different contexts

@rezaansyed rezaansyed force-pushed the fix-online-token-oauth-flow branch from 801ce71 to a8c0d6c Compare January 26, 2021 23:44
@rezaansyed
Copy link
Contributor Author

I'm not a fan of the overrides here, but let's mark follow-up work to see if we can refactor the *_session_by_cookie methods to behave differently under different contexts

This is to limit the blast radius of our changes. SessionController and CallbackController are both controllers that operate on the top level and are justified in using session cookies. We need to look further into splitting up components whose lifecycles are in an embedded context from those with lifecycles at the top level.

@rezaansyed rezaansyed force-pushed the fix-online-token-oauth-flow branch from a8c0d6c to dd7fe28 Compare January 26, 2021 23:47
@rezaansyed rezaansyed requested a review from tanema January 26, 2021 23:50
@rezaansyed rezaansyed force-pushed the fix-online-token-oauth-flow branch from dd7fe28 to 66a879e Compare January 27, 2021 02:21
Copy link
Contributor

@greatestape greatestape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rezaansyed rezaansyed force-pushed the fix-online-token-oauth-flow branch from cce7e51 to a50cf7f Compare January 27, 2021 21:08
Copy link
Contributor

@greatestape greatestape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rezaansyed rezaansyed force-pushed the fix-online-token-oauth-flow branch from a50cf7f to 48f3ed1 Compare January 27, 2021 21:13
@rezaansyed rezaansyed force-pushed the fix-online-token-oauth-flow branch from 48f3ed1 to 9cf97a4 Compare January 27, 2021 21:13
@rezaansyed rezaansyed merged commit 4bb4593 into master Jan 27, 2021
@rezaansyed rezaansyed deleted the fix-online-token-oauth-flow branch January 27, 2021 21:15
@rezaansyed rezaansyed mentioned this pull request Jan 27, 2021
4 tasks
@rezaansyed rezaansyed temporarily deployed to rubygems January 27, 2021 21:31 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants