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

Added options hash to create_permission_url and makes redirect_uri required #670

Merged
merged 1 commit into from
Jan 20, 2020
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,16 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
shopify_session = ShopifyAPI::Session.new(domain: "SHOP_NAME.myshopify.com", api_version: api_version, token: nil)
```

Then call:
Then call `create_permission_url` with the redirect_uri you've registered for your application:

```ruby
scope = ["write_products"]
permission_url = shopify_session.create_permission_url(scope)
permission_url = shopify_session.create_permission_url(scope, "https://my_redirect_uri.com")
```

or if you want a custom redirect_uri:
You can also pass a state parameter in the options hash as a last argument:

```ruby
permission_url = shopify_session.create_permission_url(scope, "https://my_redirect_uri.com")
permission_url = shopify_session.create_permission_url(scope, "https://my_redirect_uri.com", { state: "My Nonce" })
```

4. Once authorized, the shop redirects the owner to the return URL of your application with a parameter named 'code'. This is a temporary token that the app can exchange for a permanent access token.
Expand Down
6 changes: 3 additions & 3 deletions lib/shopify_api/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def initialize(domain:, token:, api_version:, extra: {})
self.extra = extra
end

def create_permission_url(scope, redirect_uri = nil)
params = {:client_id => api_key, :scope => scope.join(',')}
params[:redirect_uri] = redirect_uri if redirect_uri
def create_permission_url(scope, redirect_uri, options = {})
params = { client_id: api_key, scope: scope.join(','), redirect_uri: redirect_uri }
params[:state] = options[:state] if options[:state]
construct_oauth_url("authorize", params)
end

Expand Down
39 changes: 26 additions & 13 deletions test/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,21 @@ def setup
assert_equal(ShopifyAPI::ApiVersion.new(handle: '2019-01'), ShopifyAPI::Base.api_version)
end

test "create_permission_url returns correct url with single scope no redirect uri" do
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
test "create_permission_url requires redirect_uri" do
ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
session = ShopifyAPI::Session.new(
domain: 'http://localhost.myshopify.com',
token: 'any-token',
api_version: any_api_version
)
scope = ["write_products"]
permission_url = session.create_permission_url(scope)
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products", permission_url
assert_raises(ArgumentError) do
session.create_permission_url(scope)
end
end

test "create_permission_url returns correct url with single scope and redirect uri" do
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
session = ShopifyAPI::Session.new(
domain: 'http://localhost.myshopify.com',
token: 'any-token',
Expand All @@ -174,28 +175,40 @@ def setup
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products&redirect_uri=http://my_redirect_uri.com", permission_url
end

test "create_permission_url returns correct url with dual scope no redirect uri" do
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
test "create_permission_url returns correct url with dual scope" do
ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
session = ShopifyAPI::Session.new(
domain: 'http://localhost.myshopify.com',
token: 'any-token',
api_version: any_api_version
)
scope = ["write_products","write_customers"]
permission_url = session.create_permission_url(scope)
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products,write_customers", permission_url
permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products,write_customers&redirect_uri=http://my_redirect_uri.com", permission_url
end

test "create_permission_url returns correct url with no scope" do
ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
session = ShopifyAPI::Session.new(
domain: 'http://localhost.myshopify.com',
token: 'any-token',
api_version: any_api_version
)
scope = []
permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=&redirect_uri=http://my_redirect_uri.com", permission_url
end

test "create_permission_url returns correct url with no scope no redirect uri" do
ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
test "create_permission_url returns correct url with state" do
ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
session = ShopifyAPI::Session.new(
domain: 'http://localhost.myshopify.com',
token: 'any-token',
api_version: any_api_version
)
scope = []
permission_url = session.create_permission_url(scope)
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=", permission_url
permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com", state: "My nonce")
assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=&redirect_uri=http://my_redirect_uri.com&state=My%20nonce", permission_url
end

test "raise exception if code invalid in request token" do
Expand Down