Class: Helpful::HttpClient::AuthHandler

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/helpful/http_client/auth_handler.rb

Overview

AuthHandler takes care of devising the auth type and using it

Constant Summary collapse

HTTP_PASSWORD =
0
URL_SECRET =
2
URL_TOKEN =
3

Instance Method Summary collapse

Constructor Details

#initialize(app, auth = {}, options = {}) ⇒ AuthHandler

Returns a new instance of AuthHandler.



15
16
17
18
# File 'lib/helpful/http_client/auth_handler.rb', line 15

def initialize(app, auth = {}, options = {})
  @auth = auth
  super(app)
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/helpful/http_client/auth_handler.rb', line 20

def call(env)
  if !@auth.empty?
    auth = get_auth_type
    flag = false

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

    if auth == URL_SECRET
      env = url_secret(env)
      flag = true
    end

    if auth == URL_TOKEN
      env = url_token(env)
      flag = true
    end

    if !flag
      raise StandardError.new "Unable to calculate authorization method. Please check"
    end
  else
    raise StandardError.new "Server requires authentication to proceed further. Please check"
  end

  @app.call(env)
end

#get_auth_typeObject

Calculating the Authentication Type



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/helpful/http_client/auth_handler.rb', line 51

def get_auth_type()

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

  if @auth.has_key?(:client_id) and @auth.has_key?(:client_secret)
    return URL_SECRET
  end

  if @auth.has_key?(:access_token)
    return URL_TOKEN
  end

  return -1
end

#http_password(env) ⇒ Object

Basic Authorization with username and password



69
70
71
72
73
74
75
# File 'lib/helpful/http_client/auth_handler.rb', line 69

def http_password(env)
  code = Base64.encode64 "#{@auth[:username]}:#{@auth[:password]}"

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

  return env
end

#merge_query(env, query) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/helpful/http_client/auth_handler.rb', line 102

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) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/helpful/http_client/auth_handler.rb', line 94

def query_params(url)
  if url.query.nil? or url.query.empty?
    {}
  else
    Faraday::Utils.parse_query(url.query)
  end
end

#url_secret(env) ⇒ Object

OAUTH2 Authorization with client secret



78
79
80
81
82
83
84
85
# File 'lib/helpful/http_client/auth_handler.rb', line 78

def url_secret(env)
  query = {
    :client_id => @auth[:client_id],
    :client_secret => @auth[:client_secret]
  }

  merge_query(env, query)
end

#url_token(env) ⇒ Object

OAUTH2 Authorization with access token



88
89
90
91
92
# File 'lib/helpful/http_client/auth_handler.rb', line 88

def url_token(env)
  query = { :access_token => @auth[:access_token] }

  merge_query(env, query)
end