Class: Sorcery::Providers::Vk

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth2
Defined in:
lib/sorcery/providers/vk.rb

Overview

This class adds support for OAuth with vk.com.

config.vk.key = <key>
config.vk.secret = <secret>
...

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#authorize_url, #build_client, #get_access_token, #oauth_version

Methods inherited from Base

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializeVk

Returns a new instance of Vk.



14
15
16
17
18
19
20
21
22
# File 'lib/sorcery/providers/vk.rb', line 14

def initialize
  super

  @site           = 'https://oauth.vk.com/'
  @user_info_url  = 'https://api.vk.com/method/getProfiles'
  @auth_path      = '/authorize'
  @token_path     = '/access_token'
  @scope          = 'email'
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



12
13
14
# File 'lib/sorcery/providers/vk.rb', line 12

def api_version
  @api_version
end

#auth_pathObject

Returns the value of attribute auth_path.



12
13
14
# File 'lib/sorcery/providers/vk.rb', line 12

def auth_path
  @auth_path
end

#scopeObject

Returns the value of attribute scope.



12
13
14
# File 'lib/sorcery/providers/vk.rb', line 12

def scope
  @scope
end

#token_pathObject

Returns the value of attribute token_path.



12
13
14
# File 'lib/sorcery/providers/vk.rb', line 12

def token_path
  @token_path
end

#user_info_urlObject

Returns the value of attribute user_info_url.



12
13
14
# File 'lib/sorcery/providers/vk.rb', line 12

def 
  @user_info_url
end

Instance Method Details

#get_user_hash(access_token) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sorcery/providers/vk.rb', line 24

def get_user_hash(access_token)
  user_hash = auth_hash(access_token)

  params = {
    access_token: access_token.token,
    uids:         access_token.params['user_id'],
    fields:       .values.join(','),
    scope:        scope,
    v:            api_version.to_s
  }

  response = access_token.get(, params: params)
  if (user_hash[:user_info] = JSON.parse(response.body))
    user_hash[:user_info] = user_hash[:user_info]['response'][0]
    user_hash[:user_info]['full_name'] = [user_hash[:user_info]['first_name'], user_hash[:user_info]['last_name']].join(' ')

    user_hash[:uid] = user_hash[:user_info]['id']
    user_hash[:user_info]['email'] = access_token.params['email']
  end
  user_hash
end

#login_url(_params, _session) ⇒ Object

calculates and returns the url to which the user should be redirected, to get authenticated at the external provider’s site.



48
49
50
# File 'lib/sorcery/providers/vk.rb', line 48

def (_params, _session)
  authorize_url(authorize_url: auth_path)
end

#process_callback(params, _session) ⇒ Object

tries to login the user from access token



53
54
55
56
57
58
59
# File 'lib/sorcery/providers/vk.rb', line 53

def process_callback(params, _session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end

  get_access_token(args, token_url: token_path, token_method: :post)
end