class Opera::MobileStoreSDK::FaradayMiddleware::Authentication

Adds the Opera Mobile Store Partner authentication parameters to the request

Public Class Methods

new(app, username, password, options = {}) click to toggle source

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)
Calls superclass method
# File lib/opera/mobile_store_sdk/faraday_middleware/authentication.rb, line 15
def initialize(app, username, password, options = {})
  super(app)

  @username, @password = username, password

  options = options.with_indifferent_access

  @authentication = if options.key?(:authentication) && [:password, :signature].include?(options[:authentication])
    options[:authentication]
  else
    :signature
  end
end

Public Instance Methods

call(env) click to toggle source
# 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) click to toggle source

Generates the required HMAC Hash using the request’s GET params processed using Opera’s “special algorithm”:

# 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() click to toggle source

Uses the partner password to generate the HMAC SHA1 key:

# File lib/opera/mobile_store_sdk/faraday_middleware/authentication.rb, line 50
def signature_key
  @_signature_key ||= [@password].pack("H*")
end