Class: AquaIo::HttpClient::AuthHandler
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- AquaIo::HttpClient::AuthHandler
- Defined in:
- lib/aqua_io/http_client/auth_handler.rb
Overview
AuthHandler takes care of devising the auth type and using it
Constant Summary collapse
- URL_SECRET =
2
- URL_TOKEN =
3
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#get_auth_type ⇒ Object
Calculating the Authentication Type.
-
#initialize(app, auth = {}, options = {}) ⇒ AuthHandler
constructor
A new instance of AuthHandler.
- #merge_query(env, query) ⇒ Object
- #query_params(url) ⇒ Object
-
#url_secret(env) ⇒ Object
OAUTH2 Authorization with client secret.
-
#url_token(env) ⇒ Object
OAUTH2 Authorization with access token.
Constructor Details
#initialize(app, auth = {}, options = {}) ⇒ AuthHandler
Returns a new instance of AuthHandler.
13 14 15 16 |
# File 'lib/aqua_io/http_client/auth_handler.rb', line 13 def initialize(app, auth = {}, = {}) @auth = auth super(app) end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/aqua_io/http_client/auth_handler.rb', line 18 def call(env) if !@auth.empty? auth = get_auth_type flag = false 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 end @app.call(env) end |
#get_auth_type ⇒ Object
Calculating the Authentication Type
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aqua_io/http_client/auth_handler.rb', line 42 def get_auth_type() 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 |
#merge_query(env, query) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/aqua_io/http_client/auth_handler.rb', line 80 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
72 73 74 75 76 77 78 |
# File 'lib/aqua_io/http_client/auth_handler.rb', line 72 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
56 57 58 59 60 61 62 63 |
# File 'lib/aqua_io/http_client/auth_handler.rb', line 56 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
66 67 68 69 70 |
# File 'lib/aqua_io/http_client/auth_handler.rb', line 66 def url_token(env) query = { :access_token => @auth[:access_token] } merge_query(env, query) end |