From 6639607fbe136483764893a27c64779d785b0e49 Mon Sep 17 00:00:00 2001 From: Kevin O'Sullivan Date: Tue, 2 Aug 2022 14:06:35 -0400 Subject: [PATCH] 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) --- CHANGELOG.md | 2 +- lib/shopify_api/auth.rb | 26 ++++++++++++++ .../errors/missing_required_argument_error.rb | 9 +++++ test/auth_test.rb | 35 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 lib/shopify_api/auth.rb create mode 100644 lib/shopify_api/errors/missing_required_argument_error.rb create mode 100644 test/auth_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a4fe660e..847db3b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api ## Unreleased -N/A +- [#1002](https://github.com/Shopify/shopify-api-ruby/pull/1002) Add new method to construct the host app URL ## Version 11.0.1 diff --git a/lib/shopify_api/auth.rb b/lib/shopify_api/auth.rb new file mode 100644 index 000000000..2fd16babb --- /dev/null +++ b/lib/shopify_api/auth.rb @@ -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 diff --git a/lib/shopify_api/errors/missing_required_argument_error.rb b/lib/shopify_api/errors/missing_required_argument_error.rb new file mode 100644 index 000000000..d5fedfb06 --- /dev/null +++ b/lib/shopify_api/errors/missing_required_argument_error.rb @@ -0,0 +1,9 @@ +# typed: strict +# frozen_string_literal: true + +module ShopifyAPI + module Errors + class MissingRequiredArgumentError < StandardError + end + end +end diff --git a/test/auth_test.rb b/test/auth_test.rb new file mode 100644 index 000000000..13dc605cb --- /dev/null +++ b/test/auth_test.rb @@ -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