Class: OmniAuth::Strategies::Vkontakte

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

Overview

Authenticate to Vkontakte utilizing OAuth 2.0 and retrieve basic user information. documentation available here: http://vkontakte.ru/developers.php?o=-17680044&p=Authorization&s=0

Examples:

Basic Usage

use OmniAuth::Strategies::Vkontakte, 'API Key', 'Secret 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) ⇒ Vkontakte

Returns a new instance of Vkontakte.

Parameters:

  • app (Rack Application)

    standard middleware application parameter

  • client_id (String) (defaults to: nil)

    the application id as [registered in Vkontakte]

  • client_secret (String) (defaults to: nil)

    the application secret as [registered in Vkontakte]



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

def initialize(app, client_id=nil, client_secret=nil, options={}, &block)
  client_options = {
    :authorize_url => 'http://api.vkontakte.ru/oauth/authorize',
    :token_url => 'https://api.vkontakte.ru/oauth/token',
  }
  super(app, :vkontakte, client_id, client_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



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

def auth_hash
  # process user's info
  user_data
  OmniAuth::Utils.deep_merge(
    super, {
      'uid' => @data['uid'],
      'user_info' => ,
      'extra' => user_hash,
    }
  )
end

#build_access_tokenObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/omniauth/strategies/oauth2/vkontakte.rb', line 65

def build_access_token
  token = super
  # indicates that `offline` permission was granted, no need to the token refresh
  if token.expires_in == 0
    ::OAuth2::AccessToken.new(token.client, token.token,
      token.params.reject{|k,_| [:refresh_token, :expires_in, :expires_at, :expires].include? k.to_sym}
    )
  else
    token
  end
end

#request_phaseObject



60
61
62
63
# File 'lib/omniauth/strategies/oauth2/vkontakte.rb', line 60

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

#user_dataObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/omniauth/strategies/oauth2/vkontakte.rb', line 37

def user_data
  # http://vkontakte.ru/developers.php?o=-17680044&p=Description+of+Fields+of+the+fields+Parameter
  @fields ||= ['uid', 'first_name', 'last_name', 'nickname', 'domain', 'sex', 'bdate', 'city', 'country', 'timezone', 'photo', 'photo_big']

  # http://vkontakte.ru/developers.php?o=-1&p=getProfiles
  response = @access_token.get('https://api.vkontakte.ru/method/getProfiles',
    :params => {:uid => @access_token['user_id'], :fields => @fields.join(',')}, :parse => :json)
  @data ||= response.parsed['response'][0]

  # we need these 2 additional requests since vkontakte returns only ids of the City and Country
  # http://vkontakte.ru/developers.php?o=-17680044&p=getCities
  response = @access_token.get('https://api.vkontakte.ru/method/getCities',
    :params => {:cids => @data['city']}, :parse => :json)
  cities = response.parsed['response']
  @city ||= cities.first['name'] if cities && cities.first

  # http://vkontakte.ru/developers.php?o=-17680044&p=getCountries
  response = @access_token.get('https://api.vkontakte.ru/method/getCountries',
    :params => {:cids => @data['country']}, :parse => :json)
  countries = response.parsed['response']
  @country ||= countries.first['name'] if countries && countries.first
end

#user_hashObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/omniauth/strategies/oauth2/vkontakte.rb', line 92

def user_hash
  {
    'user_hash' => {
      'gender' => @data['sex'],
      'timezone' => @data['timezone'],
      # 200px maximum resolution of the avatar (http://vkontakte.ru/developers.php?o=-17680044&p=Description+of+Fields+of+the+fields+Parameter)
      'photo_big' => @data['photo_big'],
    }
  }
end

#user_infoObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/omniauth/strategies/oauth2/vkontakte.rb', line 77

def 
  {
    'first_name' => @data['first_name'],
    'last_name' => @data['last_name'],
    'name' => "#{@data['first_name']} #{@data['last_name']}".strip,
    'nickname' => @data['nickname'],
    'birth_date' => @data['bdate'],
    'image' => @data['photo'],
    'location' => "#{@country}, #{@city}",
    'urls' => {
      'Vkontakte' => "http://vkontakte.ru/#{@data['domain']}",
    },
  }
end