Class: Summon::FaradayMiddleware::SummonAuthentication
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Summon::FaradayMiddleware::SummonAuthentication
- Defined in:
- lib/summon/faraday_middleware/summon_authentication.rb
Constant Summary collapse
- AUTH_HEADER =
'Authorization'.freeze
- HOST_HEADER =
'Host'- SUMMON_PREFIX =
'Summon'.freeze
- SUMMON_DATE =
'x-summon-date'.freeze
- SUMMON_SESSION =
'x-summon-session-id'.freeze
- CONTENT_TYPE =
'Content-Type'.freeze
- ACCEPT_HEADER =
'Accept'.freeze
- TYPE_URLENCODED =
'application/x-www-form-urlencoded; charset=utf8'.freeze
- Error =
Class.new(StandardError)
- AuthenticationError =
Class.new(Error)
Instance Attribute Summary collapse
-
#access_id ⇒ Object
Returns the value of attribute access_id.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
Instance Method Summary collapse
- #authorization(env) ⇒ Object
- #call(env) ⇒ Object
- #client_key ⇒ Object
- #digest(env) ⇒ Object
- #httpdate ⇒ Object
-
#initialize(app, options = {}) ⇒ SummonAuthentication
constructor
A new instance of SummonAuthentication.
- #session_id ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ SummonAuthentication
Returns a new instance of SummonAuthentication.
34 35 36 37 38 39 40 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 34 def initialize(app, = {}) super(app) @access_id = .fetch(:access_id) { |name| handle_missing_parameter(name) } @secret_key = .fetch(:secret_key) { |name| handle_missing_parameter(name) } @client_key = [:client_key] @session_id = [:session_id] end |
Instance Attribute Details
#access_id ⇒ Object
Returns the value of attribute access_id.
21 22 23 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 21 def access_id @access_id end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
21 22 23 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 21 def secret_key @secret_key end |
Instance Method Details
#authorization(env) ⇒ Object
54 55 56 57 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 54 def (env) value = [access_id, client_key, digest(env)].compact.join(';') "#{SUMMON_PREFIX} #{value}" end |
#call(env) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 23 def call(env) uri = env[:url] headers = env[:request_headers] headers[CONTENT_TYPE] = TYPE_URLENCODED headers[HOST_HEADER] = "#{uri.host}:#{uri.port}" headers[SUMMON_DATE] = httpdate headers[SUMMON_SESSION] = session_id headers[AUTH_HEADER] = (env) @app.call(env) end |
#client_key ⇒ Object
50 51 52 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 50 def client_key @client_key.upcase if @client_key end |
#digest(env) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 59 def digest(env) uri = env[:url] headers = env[:request_headers] data = [] data << headers.fetch(ACCEPT_HEADER) { |name| handle_missing_header(name) } data << headers.fetch(SUMMON_DATE) { |name| handle_missing_header(name) } data << headers.fetch(HOST_HEADER) { |name| handle_missing_header(name) } data << uri.path data << sort_query_params(uri.query) data << '' Digest::HMAC.base64digest(data.join("\n"), @secret_key, Digest::SHA1).chomp end |
#httpdate ⇒ Object
42 43 44 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 42 def httpdate Time.now.httpdate end |
#session_id ⇒ Object
46 47 48 |
# File 'lib/summon/faraday_middleware/summon_authentication.rb', line 46 def session_id @session_id ||= generate_session_id end |