Skip to content
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

Add AppBridgeMiddleware #1345

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def self.use_webpacker?
require 'shopify_app/managers/scripttags_manager'

# middleware
require 'shopify_app/middleware/app_bridge_middleware'
require 'shopify_app/middleware/jwt_middleware'
require 'shopify_app/middleware/same_site_cookie_middleware'

Expand Down
3 changes: 2 additions & 1 deletion lib/shopify_app/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class Engine < Rails::Engine

initializer "shopify_app.middleware" do |app|
app.config.middleware.insert_after(::Rack::Runtime, ShopifyApp::SameSiteCookieMiddleware)
app.config.middleware.insert_after(ShopifyApp::SameSiteCookieMiddleware, ShopifyApp::AppBridgeMiddleware)

if ShopifyApp.configuration.allow_jwt_authentication
app.config.middleware.insert_after(ShopifyApp::SameSiteCookieMiddleware, ShopifyApp::JWTMiddleware)
app.config.middleware.insert_after(ShopifyApp::AppBridgeMiddleware, ShopifyApp::JWTMiddleware)
end
end

Expand Down
20 changes: 20 additions & 0 deletions lib/shopify_app/middleware/app_bridge_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true
module ShopifyApp
class AppBridgeMiddleware
def initialize(app)
@app = app
end

def call(env)
request = Rack::Request.new(env)

if request.params.has_key?("shop") && !request.params.has_key?("host")
shop = request.params["shop"]
host = Base64.urlsafe_encode64("#{shop}/admin", padding: false)
request.update_param("host", host)
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding, the host parameter is subject to change; it's not always going to be the shop Base64 encoded.

end

@app.call(env)
end
end
end
22 changes: 22 additions & 0 deletions test/shopify_app/middleware/app_bridge_middleware_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'test_helper'

class ShopifyApp::AppBridgeMiddlewareTest < ActiveSupport::TestCase
def simple_app
lambda { |env|
[200, { "Content-Type" => "text/plain" }, ["OK"]]
}
end

def app
Rack::Lint.new(ShopifyApp::AppBridgeMiddleware.new(simple_app))
end

test "adds missing host params" do
env = Rack::MockRequest.env_for('https://example.com', params: { shop: "test-shop.myshopify.com" })

app.call(env)

assert_equal "dGVzdC1zaG9wLm15c2hvcGlmeS5jb20vYWRtaW4", env["rack.request.query_hash"]["host"]
end
end