-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Auth module, method to construct host app URL
Use Base64.strict_encode64 (to avoid adding newlines), but Base64.decode64 (using strict_decode64 breaks with what's provided by Shopify)
- Loading branch information
1 parent
bcd5862
commit 6639607
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,26 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module ShopifyAPI | ||
module Auth | ||
extend T::Sig | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { params(host: T.nilable(String)).returns(String) } | ||
def embedded_app_url(host) | ||
unless Context.setup? | ||
raise Errors::ContextNotSetupError, "ShopifyAPI::Context not setup, please call ShopifyAPI::Context.setup" | ||
end | ||
|
||
unless host | ||
raise Errors::MissingRequiredArgumentError, "host argument is required" | ||
end | ||
|
||
decoded_host = Base64.decode64(host) | ||
"https://#{decoded_host}/apps/#{Context.api_key}" | ||
end | ||
end | ||
end | ||
end |
This file contains 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,9 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module ShopifyAPI | ||
module Errors | ||
class MissingRequiredArgumentError < StandardError | ||
end | ||
end | ||
end |
This file contains 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,35 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require_relative "./test_helper" | ||
|
||
module ShopifyAPITest | ||
class AuthTest < Test::Unit::TestCase | ||
def setup | ||
super | ||
@host = "some-shopify-store.myshopify.com/admin" | ||
@encoded_host = Base64.strict_encode64(@host) | ||
end | ||
|
||
def test_valid_host | ||
assert_equal( | ||
ShopifyAPI::Auth.embedded_app_url(@encoded_host), | ||
"https://#{@host}/apps/#{ShopifyAPI::Context.api_key}" | ||
) | ||
end | ||
|
||
def test_no_host | ||
assert_raises(ShopifyAPI::Errors::MissingRequiredArgumentError) do | ||
ShopifyAPI::Auth.embedded_app_url(nil) | ||
end | ||
end | ||
|
||
def test_context_not_setup | ||
modify_context(api_key: "", api_secret_key: "", host_name: "") | ||
|
||
assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do | ||
ShopifyAPI::Auth.embedded_app_url(@encoded_host) | ||
end | ||
end | ||
end | ||
end |