Class: ShopifyCLI::IdentityAuth::Servlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/shopify_cli/identity_auth/servlet.rb

Constant Summary collapse

ERB_FILENAME =
File.join(ROOT, "lib/shopify_cli/assets/post_auth_page/index.html.erb")
CSS_FILENAME =
File.join(ROOT, "lib/shopify_cli/assets/post_auth_page/style.css")

Instance Method Summary collapse

Constructor Details

#initialize(server, identity_auth, token) ⇒ Servlet

Returns a new instance of Servlet.



7
8
9
10
11
12
# File 'lib/shopify_cli/identity_auth/servlet.rb', line 7

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

Instance Method Details

#do_GET(req, res) ⇒ Object

rubocop:disable Naming/MethodName



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shopify_cli/identity_auth/servlet.rb', line 14

def do_GET(req, res) # rubocop:disable Naming/MethodName
  if !req.query["error"].nil?
    respond_with(
      res,
      400,
      Context.message("core.identity_auth.servlet.invalid_request_response", req.query["error_description"])
    )
  elsif req.query["state"] != @state_token
    response_message = Context.message("core.identity_auth.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.identity_auth.servlet.success_response"))
  end
  @identity_auth.response_query = req.query
  @server.shutdown
end

#respond_with(response, status, message) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/shopify_cli/identity_auth/servlet.rb', line 32

def respond_with(response, status, message)
  successful = status == 200
  locals = {
    status: status,
    message: message,
    css: File.read(CSS_FILENAME),
  }
  response.status = status
  response.body = ERB.new(File.read(ERB_FILENAME)).result(binding)
end