Sha256: af237824cacdefa4dd0c2a6e2930de4bf7313479f07cec3ddff8aae76f8193dc

Contents?: true

Size: 887 Bytes

Versions: 28

Compression:

Stored size: 887 Bytes

Contents

# frozen_string_literal: true

module ShopifyApp
  class JWTMiddleware
    TOKEN_REGEX = /^Bearer\s+(.*?)$/

    def initialize(app)
      @app = app
    end

    def call(env)
      return call_next(env) unless authorization_header(env)

      token = extract_token(env)
      return call_next(env) unless token

      set_env_variables(token, env)
      call_next(env)
    end

    private

    def call_next(env)
      @app.call(env)
    end

    def authorization_header(env)
      env["HTTP_AUTHORIZATION"]
    end

    def extract_token(env)
      match = authorization_header(env).match(TOKEN_REGEX)
      match && match[1]
    end

    def set_env_variables(token, env)
      jwt = ShopifyApp::JWT.new(token)

      env["jwt.shopify_domain"] = jwt.shopify_domain
      env["jwt.shopify_user_id"] = jwt.shopify_user_id
      env["jwt.expire_at"] = jwt.expire_at
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
shopify_app-22.1.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-22.0.1 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-22.00.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.10.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.9.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.8.1 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.8.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.7.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.6.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.5.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.4.1 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.4.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.3.1 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.3.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.2.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.1.1 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.1.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-21.0.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-20.2.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-20.1.1 lib/shopify_app/middleware/jwt_middleware.rb