Sha256: 9f578d0c2a93c705b16e90d5da4768aa1ac27734b676360699ec9b5b706c2a17

Contents?: true

Size: 1.85 KB

Versions: 20

Compression:

Stored size: 1.85 KB

Contents

module ShopifyCli
  class OAuth
    class Servlet < WEBrick::HTTPServlet::AbstractServlet
      TEMPLATE = %{<!DOCTYPE html>
        <html>
        <head>
          <title>%{title}</title>
        </head>
        <body>
          <h1 style="color: #%{color};">%{message}</h1>
          %{autoclose}
        </body>
        </html>
      }
      AUTOCLOSE_TEMPLATE = %{
        <script>
          setTimeout(function() { window.close(); }, 3000)
        </script>
      }

      def initialize(server, oauth, token)
        super
        @server = server
        @oauth = oauth
        @state_token = token
      end

      def do_GET(req, res) # rubocop:disable Naming/MethodName
        if !req.query['error'].nil?
          respond_with(
            res,
            400,
            Context.message('core.oauth.servlet.invalid_request_response', req.query['error_description'])
          )
        elsif req.query['state'] != @state_token
          response_message = Context.message('core.oauth.servlet.invalid_state_response')
          req.query.merge!('error' => 'invalid_state', 'error_description' => response_message)
          respond_with(res, 403, response_message)
        else
          respond_with(res, 200, Context.message('core.oauth.servlet.success_response'))
        end
        @oauth.response_query = req.query
        @server.shutdown
      end

      def respond_with(response, status, message)
        successful = status == 200
        locals = {
          status: status,
          message: message,
          color: successful ? 'black' : 'red',
          title:
            Context.message(successful ? 'core.oauth.servlet.authenticated' : 'core.oauth.servlet.not_authenticated'),
          autoclose: successful ? AUTOCLOSE_TEMPLATE : '',
        }
        response.status = status
        response.body = format(TEMPLATE, locals)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
shopify-cli-1.6.0 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.5.0 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.4.1 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.4.0 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.3.1 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.3.0 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.2.0 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.1.2 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.1.1 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.1.0 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.0.5 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.0.4 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.0.3 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.0.2 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.0.1 lib/shopify-cli/oauth/servlet.rb
shopify-cli-1.0.0 lib/shopify-cli/oauth/servlet.rb
shopify-cli-0.9.3 lib/shopify-cli/oauth/servlet.rb
shopify-cli-0.9.2 lib/shopify-cli/oauth/servlet.rb
shopify-cli-0.9.1 lib/shopify-cli/oauth/servlet.rb
shopify-cli-0.9.0 lib/shopify-cli/oauth/servlet.rb