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

feature: Allow the scope to be passed in as an array. #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ end

You can configure the scope, which you pass in to the `provider` method via a `Hash`:

* `scope`: A comma-separated list of permissions you want to request from the user. See [the Shopify API docs](http://docs.shopify.com/api/tutorials/oauth) for a full list of available permissions.
* `scope`: An array of permissions you want to request from the user. See [the Shopify API docs](http://docs.shopify.com/api/tutorials/oauth) for a full list of available permissions.

For example, to request `read_products`, `read_orders` and `write_content` permissions and display the authentication page:

```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'], :scope => 'read_products,read_orders,write_content'
provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'], :scope => ['read_products', 'read_orders', 'write_content']
end
```

Expand Down
2 changes: 1 addition & 1 deletion example/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'bundler/setup'
require 'sinatra/base'
require 'omniauth-shopify-oauth2'

SCOPE = 'read_products,read_orders,read_customers,write_shipping'
SCOPE = ['read_products', 'read_orders', 'read_customers', 'write_shipping']
SHOPIFY_API_KEY = ENV['SHOPIFY_API_KEY']
SHOPIFY_SHARED_SECRET = ENV['SHOPIFY_SHARED_SECRET']

Expand Down
5 changes: 5 additions & 0 deletions lib/omniauth/strategies/shopify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class Shopify < OmniAuth::Strategies::OAuth2

uid { URI.parse(options[:client_options][:site]).host }

def initialize(*args, &block)
super(*args, &block)
options[:scope] = options[:scope].join(',') if options[:scope].is_a?(Array)
end

def valid_site?
!!(/\A(https|http)\:\/\/[a-zA-Z0-9][a-zA-Z0-9\-]*\.#{Regexp.quote(options[:myshopify_domain])}[\/]?\z/ =~ options[:client_options][:site])
end
Expand Down
11 changes: 11 additions & 0 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ def test_callback_with_spaces_in_scope
assert_callback_success(response, access_token, code)
end

def test_callback_with_array_as_scope
build_app scope: ['write_products', 'read_orders']
access_token = SecureRandom.hex(16)
code = SecureRandom.hex(16)
expect_access_token_request(access_token, 'read_orders,write_products')

response = callback(sign_params(shop: 'snowdevil.myshopify.com', code: code, state: opts["rack.session"]["omniauth.state"]))

assert_callback_success(response, access_token, code)
end

def test_callback_rejects_invalid_hmac
@secret = 'wrong_secret'
response = callback(sign_params(shop: 'snowdevil.myshopify.com', code: SecureRandom.hex(16)))
Expand Down