Skip to content

Commit

Permalink
Use HttpClient instead of HTTParty in OAuth.validate_auth_callback (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brendo committed Nov 8, 2023
1 parent bf02f13 commit abf3834
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
## Unreleased
- [#1241](https://github.com/Shopify/shopify-api-ruby/pull/1241) Add `api_host` to `ShopifyAPI::Context.setup`, allowing the API host to be overridden in `ShopifyAPI::Clients::HttpClient`. This context option is intended for internal Shopify use only.
- [#1237](https://github.com/Shopify/shopify-api-ruby/pull/1237) Skip mandatory webhook topic registration/unregistrations
- [#1239](https://github.com/Shopify/shopify-api-ruby/pull/1239) Update `OAuth.validate_auth_callback` to use `ShopifyApi::Clients::HttpClient`.

## 13.2.0

Expand Down
20 changes: 15 additions & 5 deletions lib/shopify_api/auth/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,25 @@ def validate_auth_callback(cookies:, auth_query:)
raise Errors::InvalidOauthError,
"Invalid state in OAuth callback." unless state == auth_query.state

# TODO: replace this call with the HTTP client once it is built
null_session = Auth::Session.new(shop: auth_query.shop)
body = { client_id: Context.api_key, client_secret: Context.api_secret_key, code: auth_query.code }
response = HTTParty.post("https://#{auth_query.shop}/admin/oauth/access_token", body: body)
unless response.ok?

client = Clients::HttpClient.new(session: null_session, base_path: "/admin/oauth")
response = begin
client.request(
Clients::HttpRequest.new(
http_method: :post,
path: "access_token",
body: body,
body_type: "application/json",
),
)
rescue ShopifyAPI::Errors::HttpResponseError => e
raise Errors::RequestAccessTokenError,
"Cannot complete OAuth process. Received a #{response.code} error while requesting access token."
"Cannot complete OAuth process. Received a #{e.code} error while requesting access token."
end
session_params = response.to_h

session_params = T.cast(response.body, T::Hash[String, T.untyped]).to_h
session = create_new_session(session_params, auth_query.shop)

cookie = if Context.embedded?
Expand Down

0 comments on commit abf3834

Please sign in to comment.