class Confy::HttpClient::AuthHandler

AuthHandler takes care of devising the auth type and using it

Constants

HTTP_PASSWORD

Public Class Methods

new(app, auth = {}, options = {}) click to toggle source
Calls superclass method
# File lib/confy/http_client/auth_handler.rb, line 12
def initialize(app, auth = {}, options = {})
  @auth = auth
  super(app)
end

Public Instance Methods

call(env) click to toggle source
# File lib/confy/http_client/auth_handler.rb, line 17
def call(env)
  if !@auth.empty?
    auth = get_auth_type
    flag = false

    if auth == HTTP_PASSWORD
      env = http_password(env)
      flag = true
    end

    if !flag
      raise StandardError.new "Unable to calculate authorization method. Please check"
    end
  end

  @app.call(env)
end
get_auth_type() click to toggle source

Calculating the Authentication Type

# File lib/confy/http_client/auth_handler.rb, line 36
def get_auth_type()

  if @auth.has_key?(:username) and @auth.has_key?(:password)
    return HTTP_PASSWORD
  end

  return -1
end
http_password(env) click to toggle source

Basic Authorization with username and password

# File lib/confy/http_client/auth_handler.rb, line 46
def http_password(env)
  code = Base64.strict_encode64 "#{@auth[:username]}:#{@auth[:password]}"

  env[:request_headers]["Authorization"] = "Basic #{code}"

  return env
end
merge_query(env, query) click to toggle source
# File lib/confy/http_client/auth_handler.rb, line 62
def merge_query(env, query)
  query = query.update query_params(env[:url])

  env[:url].query = Faraday::Utils.build_query(query)

  return env
end
query_params(url) click to toggle source
# File lib/confy/http_client/auth_handler.rb, line 54
def query_params(url)
  if url.query.nil? or url.query.empty?
    {}
  else
    Faraday::Utils.parse_query(url.query)
  end
end