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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
----------
* Move covered scopes check into user access strategy [#1600](https://github.com/Shopify/shopify_app/pull/1600)
* Add configuration option for user access strategy [#1599](https://github.com/Shopify/shopify_app/pull/1599)
* Fixes a bug with `EnsureAuthenticatedLinks` causing deep links to not work [#1549](https://github.com/Shopify/shopify_app/pull/1549)
* Ensure online token is properly used when using `current_shopify_session` [#1566](https://github.com/Shopify/shopify_app/pull/1566)
Expand Down
4 changes: 4 additions & 0 deletions lib/shopify_app/access_scopes/noop_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class << self
def update_access_scopes?(*_args)
false
end

def covers_scopes?(*_args)
true
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/shopify_app/access_scopes/user_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 covers_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.

end

private

def update_access_scopes_for_user_id?(user_id)
Expand Down
4 changes: 1 addition & 3 deletions lib/shopify_app/controller_concerns/login_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def activate_shopify_session
return redirect_to_login
end

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?...

clear_shopify_session
return redirect_to_login
end
Expand Down