Skip to content

Commit c51c59d

Browse files
committed
Allow custom scopes during the auth process
1 parent 5cb4129 commit c51c59d

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44

55
## Unreleased
66

7+
- [#1023](https://github.com/Shopify/shopify-api-ruby/pull/1023) Allow custom scopes during the OAuth process
8+
79
## Version 12.1.0
810

911
- [#1017](https://github.com/Shopify/shopify-api-ruby/pull/1017) Add support for `http` with localhost development without using a TLS tunnel

lib/shopify_api/auth/oauth.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ class << self
1616
shop: String,
1717
redirect_path: String,
1818
is_online: T.nilable(T::Boolean),
19+
scope: ShopifyAPI::Auth::AuthScopes,
1920
).returns(T::Hash[Symbol, T.any(String, SessionCookie)])
2021
end
21-
def begin_auth(shop:, redirect_path:, is_online: true)
22+
def begin_auth(shop:, redirect_path:, is_online: true, scope: ShopifyAPI::Context.scope)
2223
unless Context.setup?
2324
raise Errors::ContextNotSetupError, "ShopifyAPI::Context not setup, please call ShopifyAPI::Context.setup"
2425
end
@@ -30,7 +31,7 @@ def begin_auth(shop:, redirect_path:, is_online: true)
3031

3132
query = {
3233
client_id: ShopifyAPI::Context.api_key,
33-
scope: ShopifyAPI::Context.scope.to_s,
34+
scope: scope.to_s,
3435
redirect_uri: "#{ShopifyAPI::Context.host}#{redirect_path}",
3536
state: state,
3637
"grant_options[]": is_online ? "per-user" : "",

test/auth/oauth_test.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ def test_begin_auth_online
8585
verify_oauth_begin(auth_route: result[:auth_route], cookie: result[:cookie], is_online: true)
8686
end
8787

88+
def test_custom_scope
89+
result = ShopifyAPI::Auth::Oauth.begin_auth(shop: @shop, redirect_path: "/redirect",
90+
scope: ShopifyAPI::Auth::AuthScopes.new("read_orders,write_products"))
91+
verify_oauth_begin(auth_route: result[:auth_route], cookie: result[:cookie], is_online: true,
92+
scope: "read_orders,write_products")
93+
end
94+
8895
def test_begin_auth_context_not_setup
8996
modify_context(api_key: "", api_secret_key: "", host_name: "")
9097

@@ -280,10 +287,10 @@ def test_validate_auth_callback_save_session_fails
280287

281288
private
282289

283-
def verify_oauth_begin(auth_route:, cookie:, is_online:)
290+
def verify_oauth_begin(auth_route:, cookie:, is_online:, scope: ShopifyAPI::Context.scope)
284291
expected_query_params = {
285292
client_id: ShopifyAPI::Context.api_key,
286-
scope: ShopifyAPI::Context.scope.to_s,
293+
scope: scope.to_s,
287294
redirect_uri: "https://#{ShopifyAPI::Context.host_name}/redirect",
288295
"grant_options[]": is_online ? "per-user" : "",
289296
}

0 commit comments

Comments
 (0)