-
Notifications
You must be signed in to change notification settings - Fork 35
OAuth and webhooks when using Shopify CLI #81
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
Open
NexPB
wants to merge
15
commits into
ericdude4:v3
Choose a base branch
from
NexPB:nexpb/shopify-cli-managed-webhook
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a5cbaa6
feat: optional webhook topics
NexPB 45aea48
feat: single callback url
NexPB d208305
fix: params to atom keys
NexPB 8debd4a
fix: use recommended redirect
NexPB 3b9ce3a
fix: args rename
NexPB 143571d
refactor: if else before quote
NexPB c3f040f
fix: 143571d74d6ebc4c69ce2060c23f19b4455451e3
NexPB e627aef
fix: use the correct plug in callback
NexPB 638534a
feat: redirect using shopify app bridge
NexPB 05338df
refactor: oauth flow
NexPB 20992c7
refactor: pr feedback - move web-layer fns
NexPB 8d3da88
chore: readme shopify cli setup
NexPB f53ce4c
refactor: rename fn to exchange_code_for_access_token
NexPB 7f89f07
refactor: rename param to shop_url
NexPB a44da25
docs: readme referrence shopify cli
NexPB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # Used by "mix format" | ||
| [ | ||
| import_deps: [:ecto, :ecto_sql, :phoenix], | ||
| inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| defmodule Shopifex.OAuth do | ||
| @moduledoc """ | ||
| Shopify OAuth related functions. | ||
| """ | ||
|
|
||
| @doc """ | ||
| Returns an url to redirect the user to the Shopify OAuth page. | ||
| Shopify docs: <https://shopify.dev/docs/apps/build/authentication-authorization/access-tokens/authorization-code-grant#redirect-to-the-authorization-code-flow> | ||
| """ | ||
| @spec oauth_redirect_url(String.t(), Keyword.t()) :: String.t() | ||
| def oauth_redirect_url(shop_url, opts \\ []) do | ||
| redirect_uri = | ||
| Keyword.get(opts, :redirect_uri, Application.fetch_env!(:shopifex, :redirect_uri)) | ||
|
|
||
| query_params = | ||
| URI.encode_query(%{ | ||
| client_id: Application.fetch_env!(:shopifex, :api_key), | ||
| scope: Application.fetch_env!(:shopifex, :scopes), | ||
| redirect_uri: redirect_uri, | ||
| state: Keyword.get(opts, :state, "") | ||
| }) | ||
|
|
||
| "https://#{shop_url}/admin/oauth/authorize" | ||
| |> URI.new!() | ||
| |> URI.append_query(query_params) | ||
| |> URI.to_string() | ||
| end | ||
|
|
||
| @doc """ | ||
| Calls the Shopify OAuth endpoint to get the access token. | ||
| Shopify docs: <https://shopify.dev/docs/apps/build/authentication-authorization/access-tokens/authorization-code-grant#step-4-get-an-access-token> | ||
| """ | ||
| @spec post_access_token(String.t(), String.t()) :: | ||
| {:ok, HTTPoison.Response.t()} | {:error, HTTPoison.Error.t()} | ||
| def post_access_token(shop_domain, code) do | ||
NexPB marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| headers = [ | ||
| "Content-Type": "application/json", | ||
| Accept: "application/json" | ||
| ] | ||
|
|
||
| body = %{ | ||
| client_id: Application.fetch_env!(:shopifex, :api_key), | ||
| client_secret: Application.fetch_env!(:shopifex, :secret), | ||
| code: code | ||
| } | ||
|
|
||
| "https://#{shop_domain}/admin/oauth/access_token" | ||
| |> URI.new!() | ||
| |> URI.to_string() | ||
| |> HTTPoison.post(Jason.encode!(body), headers) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,9 @@ defmodule Shopifex.Plug do | |
| def current_shopify_host(%Plug.Conn{private: %{shopifex: %{shopify_host: shopify_host}}}), | ||
| do: shopify_host | ||
|
|
||
| def current_shopify_host(_), do: nil | ||
| def current_shopify_host(conn) do | ||
| Map.get(conn.params, "host") | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will allow the app bridge to render during install redirect as |
||
| end | ||
|
|
||
| @doc """ | ||
| Returns the token for the current session in a plug which has | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,8 +38,7 @@ defmodule Shopifex.Plug.ShopifySession do | |
| received_hmac = Shopifex.Plug.get_hmac(conn) | ||
|
|
||
| if expected_hmac == received_hmac do | ||
| conn | ||
| |> do_new_session() | ||
| do_new_session(conn) | ||
| else | ||
| Logger.info("Invalid HMAC, expected #{expected_hmac}") | ||
| respond_invalid(conn) | ||
|
|
@@ -49,7 +48,9 @@ defmodule Shopifex.Plug.ShopifySession do | |
| defp do_new_session(conn = %{params: %{"shop" => shop_url}}) do | ||
| case Shopifex.Shops.get_shop_by_url(shop_url) do | ||
| nil -> | ||
| redirect_to_install(conn, shop_url) | ||
| conn | ||
| |> redirect(to: "/initialize-installation?#{conn.query_string}") | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding this redirect doesn't feel quite right |
||
| |> halt() | ||
|
|
||
| shop -> | ||
| locale = get_locale(conn) | ||
|
|
@@ -59,17 +60,6 @@ defmodule Shopifex.Plug.ShopifySession do | |
| end | ||
| end | ||
|
|
||
| defp redirect_to_install(conn, shop_url) do | ||
| Logger.info("Initiating shop installation for #{shop_url}") | ||
|
|
||
| install_url = | ||
| "https://#{shop_url}/admin/oauth/authorize?client_id=#{Application.fetch_env!(:shopifex, :api_key)}&scope=#{Application.fetch_env!(:shopifex, :scopes)}&redirect_uri=#{Application.fetch_env!(:shopifex, :redirect_uri)}" | ||
|
|
||
| conn | ||
| |> redirect(external: install_url) | ||
| |> halt() | ||
| end | ||
|
|
||
| defp respond_invalid(%Plug.Conn{private: %{phoenix_format: "json"}} = conn) do | ||
| conn | ||
| |> put_status(:forbidden) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.