Class: Opera::MobileStoreSDK::FaradayMiddleware::Authentication
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Opera::MobileStoreSDK::FaradayMiddleware::Authentication
- Defined in:
- lib/opera/mobile_store_sdk/faraday_middleware/authentication.rb
Overview
Adds the Opera Mobile Store Partner authentication parameters to the request
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#get_signature(env) ⇒ Object
Generates the required HMAC Hash using the request’s GET params processed using Opera’s “special algorithm”:.
-
#initialize(app, username, password, options = {}) ⇒ Authentication
constructor
Public: Initialize the middleware.
-
#signature_key ⇒ Object
Uses the partner password to generate the HMAC SHA1 key:.
Constructor Details
#initialize(app, username, password, options = {}) ⇒ Authentication
Public: Initialize the middleware.
app - the Faraday app to wrap username - The Partner’s username password - The Partner’s username options - (optional)
:authentication - (:password | :signature)
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/opera/mobile_store_sdk/faraday_middleware/authentication.rb', line 15 def initialize(app, username, password, = {}) super(app) @username, @password = username, password = .with_indifferent_access @authentication = if .key?(:authentication) && [:password, :signature].include?([:authentication]) [:authentication] else :signature end end |
Instance Method Details
#call(env) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/opera/mobile_store_sdk/faraday_middleware/authentication.rb', line 29 def call(env) auth_params = { login: @username } case @authentication when :password then auth_params[:pwd] = @password else auth_params[:sign] = get_signature(env) end # Add the generated params to the url query: auth_query = auth_params.inject([]) { |arr, kv| arr + [kv.join('=')] }.join('&') env.url.query = [auth_query, env.url.query].join('&') env[:url] = env.url @app.call(env) end |
#get_signature(env) ⇒ Object
Generates the required HMAC Hash using the request’s GET params processed using Opera’s “special algorithm”:
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/opera/mobile_store_sdk/faraday_middleware/authentication.rb', line 56 def get_signature(env) params_string = env.url.query.split('&').inject({}) do |hash, keyval| key, val = keyval.split('=') hash[key] = val hash end.except("login").sort.inject "" do |concatenated_string, keyval| concatenated_string += keyval.map(&:downcase).join end # Return the SHA1 HMAC Hash: OpenSSL::HMAC.hexdigest( OpenSSL::Digest.new('sha1'), signature_key, params_string ) end |
#signature_key ⇒ Object
Uses the partner password to generate the HMAC SHA1 key:
50 51 52 |
# File 'lib/opera/mobile_store_sdk/faraday_middleware/authentication.rb', line 50 def signature_key @_signature_key ||= [@password].pack("H*") end |