Class: OmniAuth::Strategies::Vkontakte

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

Defined Under Namespace

Classes: ViewHelper

Instance Method Summary collapse

Constructor Details

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

def initialize(app, app_id, app_key, options = {}) @options = options OmniAuth.config.vkontakte_app_id = app_id OmniAuth.config.vkontakte_app_key = app_key super(app, :vkontakte) end



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

def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
  OmniAuth.config.vkontakte_app_id = client_id
  OmniAuth.config.vkontakte_app_secret = client_secret
  
  client_options = {
    :site => 'http://api.vkontakte.ru',
    :authorize_url => 'http://api.vkontakte.ru/oauth/authorize',
    :access_token_url => 'https://api.vkontakte.ru/oauth/access_token'
  }
  super(app, :vkontakte, client_id, client_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject

def callback_phase app_cookie = request.cookies return fail!(:invalid_credentials) unless app_cookie args = app_cookie.split("&") sig_index = args.index { |arg| arg =~ /^sig=/ } return fail!(:invalid_credentials) unless sig_index sig = args.delete_at(sig_index) puts Digest::MD5.new.hexdigest(args.sort.join('') + OmniAuth.config.vkontakte_app_key) puts sig return fail!(:invalid_credentials) unless Digest::MD5.new.hexdigest(args.sort.join('') + OmniAuth.config.vkontakte_app_key) == sig super end



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/omniauth/strategies/vkontakte.rb', line 94

def auth_hash
  OmniAuth::Utils.deep_merge(super(), {
    'uid' => request[:uid],
    'user_info' => {
      'nickname' => request[:nickname],
      'name' => "#{request[:first_name]} #{request[:last_name]}",
      'first_name' => request[:first_name],
      'last_name' => request[:last_name],
      'image' => request[:photo],
      'urls' => { 'Page' => 'http://vkontakte.ru/id' + request[:uid] }
    }
  })
end

#callback_phaseObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/omniauth/strategies/vkontakte.rb', line 50

def callback_phase
  if request.params['error'] || request.params['error_reason']
    raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
  end
  
  verifier = request.params['code']
  puts "!!!!!!!!!"
  puts client.access_token_url
  # @access_token = client.web_server.get_access_token(verifier, :redirect_uri => callback_url)
  response = client.request(:get, client.access_token_url, {'client_id' => OmniAuth.config.vkontakte_app_id, 'client_secret' => OmniAuth.config.vkontakte_app_secret, 'code' => verifier})
  
  puts "!!!!!"
  puts  MultiJson.decode(response)
  
#   if @access_token.expires? && @access_token.expires_in <= 0
#     client.request(:post, client.access_token_url, { 
#         'client_id' => client_id,
#         'grant_type' => 'refresh_token', 
#         'client_secret' => client_secret,
#         'refresh_token' => @access_token.refresh_token 
#       })
#     @access_token = client.web_server.get_access_token(verifier, :redirect_uri => callback_url)
#   end
#   
#   super
# rescue ::OAuth2::HTTPError, ::OAuth2::AccessDenied, CallbackError => e
#   fail!(:invalid_credentials, e)
# rescue ::MultiJson::DecodeError => e
#   fail!(:invalid_response, e)
end

#request_phaseObject

def request_phase Rack::Response.new(vkontakte_login_page).finish end



45
46
47
48
# File 'lib/omniauth/strategies/vkontakte.rb', line 45

def request_phase
  options[:scope] ||= "offline"
  super
end

#user_dataObject

attr_reader :app_id



38
39
40
# File 'lib/omniauth/strategies/vkontakte.rb', line 38

def user_data
  @data ||= MultiJson.decode(@access_token.get('/me', {}, { "Accept-Language" => "en-us,en;"}))
end