Class: OmniAuth::Strategies::Mailru

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/omniauth/strategies/oauth2/mailru.rb

Overview

Authenticate to Vkontakte utilizing OAuth 2.0 and retrieve basic user information. documentation available here: http://api.mail.ru/docs/guides/oauth/sites/

Examples:

Basic Usage

use OmniAuth::Strategies::Mailru, 'API Key', 'Secret Key', :private_key => 'Private Key'

Instance Attribute Summary

Attributes inherited from OAuth2

#client_id, #client_options, #client_secret, #options

Instance Method Summary collapse

Methods inherited from OAuth2

#callback_url, #client

Constructor Details

#initialize(app, client_id = nil, client_secret = nil, options = {}, &block) ⇒ Mailru

Returns a new instance of Mailru.

Parameters:

  • app (Rack Application)

    standard middleware application parameter

  • client_id (String) (defaults to: nil)

    the application id as [registered in Mailru]

  • client_secret (String) (defaults to: nil)

    the application secret as [registered in Mailru]



17
18
19
20
21
22
23
24
# File 'lib/omniauth/strategies/oauth2/mailru.rb', line 17

def initialize(app, client_id=nil, client_secret=nil, options={}, &block)
  client_options = {
    :authorize_url => 'https://connect.mail.ru/oauth/authorize',
    :token_url => 'https://connect.mail.ru/oauth/token',
  }
  @private_key  = options[:private_key]
  super(app, :mailru, client_id, client_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/omniauth/strategies/oauth2/mailru.rb', line 26

def auth_hash
  OmniAuth::Utils.deep_merge(
    super, {
      'uid' => user_data['uid'],
      'user_info' => ,
      'extra' => {
        'user_hash' => user_data,
      },
    }
  )
end

#calculate_signature(params) ⇒ Object



43
44
45
46
# File 'lib/omniauth/strategies/oauth2/mailru.rb', line 43

def calculate_signature(params)
  str = params['uids'] + (params.sort.collect { |c| "#{c[0]}=#{c[1]}" }).join('') + @private_key
  Digest::MD5.hexdigest(str)
end

#request_phaseObject



38
39
40
41
# File 'lib/omniauth/strategies/oauth2/mailru.rb', line 38

def request_phase
  options[:response_type] ||= 'code'
  super
end

#user_dataObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/omniauth/strategies/oauth2/mailru.rb', line 48

def user_data
  request_params = {
    'method' => 'users.getInfo',
    'app_id' => client_id,
    'session_key' => @access_token.token,
    'uids' => @access_token['x_mailru_vid']
  }

  request_params.merge!('sig' => calculate_signature(request_params))
  @data ||= MultiJson.decode(client.request(:get, 'http://www.appsmail.ru/platform/api', request_params))[0]
end

#user_infoObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/omniauth/strategies/oauth2/mailru.rb', line 60

def 
  {
    'nickname' => user_data['nick'],
    'email' =>  user_data['email'],
    'first_name' => user_data['first_name'],
    'last_name' => user_data['last_name'],
    'name' => "#{user_data['first_name']} #{user_data['last_name']}".strip,
    'image' => @data['pic'],
    'urls' => {
      'Mailru' => user_data['link'],
    },
  }
end