Sha256: 6909bcdcd686a5667b1eed20d790dda9bc77966ac9fd7cfc4daa0aa97c27cd25

Contents?: true

Size: 886 Bytes

Versions: 11

Compression:

Stored size: 886 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

11 entries across 11 versions & 2 rubygems

Version Path
ruby_shopify_app-1.3.3 lib/ruby_shopify_app/middleware/jwt_middleware.rb
ruby_shopify_app-1.3.2 lib/ruby_shopify_app/middleware/jwt_middleware.rb
ruby_shopify_app-1.3.1 lib/ruby_shopify_app/middleware/jwt_middleware.rb
ruby_shopify_app-1.3.0 lib/ruby_shopify_app/middleware/jwt_middleware.rb
ruby_shopify_app-1.2.0 lib/ruby_shopify_app/middleware/jwt_middleware.rb
ruby_shopify_app-1.1.0 lib/ruby_shopify_app/middleware/jwt_middleware.rb
ruby_shopify_app-1.0.0 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-18.1.3 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-18.1.2 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-18.1.1 lib/shopify_app/middleware/jwt_middleware.rb
shopify_app-18.1.0 lib/shopify_app/middleware/jwt_middleware.rb