class Clavem::Server

A class to handle oAuth callbacks on the browser via HTTP.

Constants

TEMPLATE

The template to send to the browser.

Public Class Methods

new(authorizer) click to toggle source

Creates a new server.

@param authorizer [Authorizer] The authorizer of this server.

# File lib/clavem/server.rb, line 26
def initialize(authorizer)
  @authorizer = authorizer
  @i18n = Bovem::I18n.new(root: "clavem", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")

  process_http_request
end

Public Instance Methods

process_http_request() click to toggle source

Save the token and sends a response back to the user.

# File lib/clavem/server.rb, line 34
def process_http_request
  server = create_server
  socket = server.accept

  # Get the request
  request = socket.gets.gsub(/^[A-Z]+\s(.+)\sHTTP.+$/, "\\1")
  querystring = Addressable::URI.parse(("%s%s" % [@authorizer.callback_url, request]).strip).query_values

  # Send the response and close the socket
  send_response(socket)

  # Handle the token
  token = @authorizer.response_handler.call(querystring)

  update_authorizer(token)
  server.close
end