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

Move covered scopes check into strategy #1600

Merged
merged 3 commits into from
Dec 1, 2022

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Nov 30, 2022

What this PR does

There are two places that we check the user's scopes when determining if a re-auth is needed:

  • update_access_scopes? in the access scopes user strategy.
  • in LoginProtection#activate_shopify_session

In #1599 we added an option to set the user strategy. But this has a gap, since only one of the above listed places is within the strategy. To have proper control over these checks, we need both of them to be in the strategy.

This PR aims to not alter any existing behaviour, it only moves code around.

Reviewer's guide to testing

Things to focus on

Checklist

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.

@@ -12,6 +12,11 @@ def update_access_scopes?(user_id: nil, shopify_user_id: nil)
"#update_access_scopes? requires user_id or shopify_user_id parameter inputs")
end

def covered_scopes?(current_shopify_session)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe covers_scopes??

Copy link
Contributor

Choose a reason for hiding this comment

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

I like that better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@andyw8
Copy link
Contributor Author

andyw8 commented Nov 30, 2022

@paulomarg @nelsonwittwer sharing as a draft, any thoughts so far?

unless current_shopify_session.scope.to_a.empty? ||
current_shopify_session.scope.covers?(ShopifyAPI::Context.scope)

unless ShopifyApp.configuration.user_access_scopes_strategy.covered_scopes?(current_shopify_session)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should both clauses of the unless be in the strategy? Or only the second? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it's really possible for a session to have empty scopes, unless there's a bug in the storage code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would expect so. I don't want to change the behaviour though so I'll leave it in.

Copy link
Contributor

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

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

Makes sense to me so far!

@@ -12,6 +12,11 @@ def update_access_scopes?(user_id: nil, shopify_user_id: nil)
"#update_access_scopes? requires user_id or shopify_user_id parameter inputs")
end

def covered_scopes?(current_shopify_session)
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that better.

unless current_shopify_session.scope.to_a.empty? ||
current_shopify_session.scope.covers?(ShopifyAPI::Context.scope)

unless ShopifyApp.configuration.user_access_scopes_strategy.covered_scopes?(current_shopify_session)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it's really possible for a session to have empty scopes, unless there's a bug in the storage code.

@@ -12,6 +12,11 @@ def update_access_scopes?(user_id: nil, shopify_user_id: nil)
"#update_access_scopes? requires user_id or shopify_user_id parameter inputs")
end

def covered_scopes?(current_shopify_session)
# NOTE: this not Ruby's `covers?` method, it is defined in ShopifyAPI::Auth::AuthScopes
current_shopify_session.scope.to_a.empty? || current_shopify_session.scope.covers?(ShopifyAPI::Context.scope)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to illustrate what covers? does:

abc = ShopifyAPI::Auth::AuthScopes.new(['A', 'B', 'C'])
ab = ShopifyAPI::Auth::AuthScopes.new(['A', 'B'])

abc.covers?(ab) => true
ab.covers?(abc) => false
abc.covers?(abc)= > true

See https://github.com/Shopify/shopify-api-ruby/blob/main/test/auth/auth_scopes_test.rb for details.

@andyw8 andyw8 marked this pull request as ready for review December 1, 2022 14:07
Copy link
Contributor

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

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

Just a non-blocking comment.

unless current_shopify_session.scope.to_a.empty? ||
current_shopify_session.scope.covers?(ShopifyAPI::Context.scope)

unless ShopifyApp.configuration.user_access_scopes_strategy.covers_scopes?(current_shopify_session)
Copy link
Contributor

Choose a reason for hiding this comment

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

I remember we saw another place where we did a similar check (callback controller?), we should make sure to use the same check in both cases for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤔 This seem to be the only use of covers?...

@andyw8 andyw8 merged commit 43c2cea into main Dec 1, 2022
@andyw8 andyw8 deleted the andyw8/move-covered-scopes-check-into-strategy branch December 1, 2022 14:31
fabriazza pushed a commit to fabriazza/shopify_app that referenced this pull request Feb 1, 2023
* Move covered scopes check into strategy

* Rename method

* Update CHANGELOG

Co-authored-by: Andy Waite <andyw8@users.noreply.github.com>
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.

None yet

2 participants