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 1 commit
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
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 covered_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 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.

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

clear_shopify_session
return redirect_to_login
end
Expand Down