class OmniAuth::Strategies::Sberbank

Authenticate to Sberbank utilizing OAuth 2.0 and retrieve basic user information. documentation available here: developer.sberbank.ru/doc/v1/sberbank-id/info

provider :sberbank, client_id: '11111111-1111-1111-1111-1111111111111111', client_secret: 'YOURSECRET', response_type: 'code', client_type: 'PRIVATE', client_options: { ssl: { client_key: client_key, client_cert: client_cert } }, scope: 'openid name email mobile', callback_path: '/callback', grant_type: 'client_credentials'

Constants

API_VERSION
DEFAULT_SCOPE

Public Instance Methods

authorize_params() click to toggle source

developer.sberbank.ru/doc/v1/sberbank-id/authcodereq

Calls superclass method
# File lib/omniauth/strategies/sberbank.rb, line 80
def authorize_params
  super.tap do |params|
    %w[state scope response_type client_type client_id nonce].each do |v|
      next unless request.params[v]

      params[v.to_sym] = request.params[v]
    end
    params[:scope] ||= DEFAULT_SCOPE
    # if you want redirect to other host and save old host
    state = session['omniauth.origin'] || env['HTTP_REFERER']
    params[:state] = state
    session['omniauth.state'] = state
    params[:nonce] = SecureRandom.hex(16)
  end
end
raw_info() click to toggle source

developer.sberbank.ru/doc/v1/sberbank-id/datareq

# File lib/omniauth/strategies/sberbank.rb, line 65
def raw_info
  access_token.options[:mode] = :header
  @raw_info ||= begin
    state = request.params['state']
    result = access_token.get('/ru/prod/sberbankid/v2.1/userinfo', headers: info_headers).parsed
    unless result['aud'] == options.client_id
      raise ArgumentError, "aud in Sber response not equal clien_id. aud = #{result['aud']}"
    end

    result['state'] = state
    result
  end
end
token_params() click to toggle source
Calls superclass method
# File lib/omniauth/strategies/sberbank.rb, line 96
def token_params
  super.tap do |params|
    params[:scope] ||= DEFAULT_SCOPE
  end
end

Private Instance Methods

access_token_headers() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 170
def access_token_headers
  OmniAuth.logger.send(:debug, "YOUR RQUID #{rquid}")
  {
    'rquid' => rquid,
    'x-ibm-client-id' => options.client_id,
    'accept' => 'application/json'
  }
end
build_access_token() click to toggle source

developer.sberbank.ru/doc/v1/sberbank-id/accessidtokens

Calls superclass method
# File lib/omniauth/strategies/sberbank.rb, line 136
def build_access_token
  options.token_params.update(headers: access_token_headers)
  super
end
callback_phase() click to toggle source
Calls superclass method
# File lib/omniauth/strategies/sberbank.rb, line 164
def callback_phase
  super
rescue NoRawData => e
  fail!(:no_raw_data, e)
end
callback_url() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 113
def callback_url
  options.redirect_url || (full_host + script_name + callback_path)
end
https_option() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 131
def https_option
  options[:https] || 0
end
image_url() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 141
def image_url
  case options[:image_size]
  when 'mini'
    raw_info['photo_50']
  when 'bigger'
    raw_info['photo_100']
  when 'bigger_x2'
    raw_info['photo_200']
  when 'original'
    raw_info['photo_200_orig']
  when 'original_x2'
    raw_info['photo_400_orig']
  else
    raw_info['photo_50']
  end
end
info_headers() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 179
def info_headers
  {
    'x-introspect-rquid' => rquid,
    'x-ibm-client-id' => options.client_id,
    'accept' => 'application/json',
    'Authorization' => "Bearer #{access_token.token}"
  }
end
info_options() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 117
def info_options
  # https://developer.sberbank.ru/doc/v1/sberbank-id/dataanswerparametrs
  fields = %w[
    sub family_name given_name middle_name birthdate email phone_number
    address_reg identification inn snils gender
  ]
  fields.concat(options[:info_fields].split(',')) if options[:info_fields]
  fields.join(',')
end
lang_option() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 127
def lang_option
  options[:lang] || ''
end
location() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 158
def location
  country = raw_info.fetch('country', {})['title']
  city = raw_info.fetch('city', {})['title']
  @location ||= [country, city].compact.join(', ')
end
params() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 104
def params
  {
    fields: info_options,
    lang: lang_option,
    https: https_option,
    v: API_VERSION
  }
end
rquid() click to toggle source
# File lib/omniauth/strategies/sberbank.rb, line 188
def rquid
  @rquid ||= SecureRandom.hex(16)
end