Module: VkontakteApi::Authorization

Included in:
VkontakteApi
Defined in:
lib/vkontakte_api/authorization.rb

Overview

Note:

VkontakteApi::Authorization extends VkontakteApi so these methods should be called from the latter.

A module containing the methods for authorization.

Constant Summary collapse

OPTIONS =

Authorization options.

{
  client: {
    site:          'https://oauth.vk.com',
    authorize_url: '/authorize',
    token_url:     '/access_token'
  },
  client_credentials: {
    'auth_scheme' => 'request_body'
  }
}

Instance Method Summary collapse

Instance Method Details

#authorization_url(options = {}) ⇒ String

URL for redirecting the user to VK where he gives the application all the requested access rights.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :type (Symbol)

    The type of authorization being used (:site and :client supported).

  • :redirect_uri (String)

    URL for redirecting the user back to the application (overrides the global configuration value).

  • :scope (Array)

    An array of requested access rights (each represented by a symbol or a string).

Returns:

  • (String)

    URL to redirect the user to.

Raises:

  • (ArgumentError)

    raises after receiving an unknown authorization type.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vkontakte_api/authorization.rb', line 24

def authorization_url(options = {})
  type = options.delete(:type) || :site
  # redirect_uri passed in options overrides the global setting
  options[:redirect_uri] ||= VkontakteApi.redirect_uri
  options[:scope] = VkontakteApi::Utils.flatten_argument(options[:scope]) if options[:scope]
  
  case type
  when :site
    client.auth_code.authorize_url(options)
  when :client
    client.implicit.authorize_url(options)
  else
    raise ArgumentError, "Unknown authorization type #{type.inspect}"
  end
end

#authorize(options = {}) ⇒ VkontakteApi::Client

Authorization (getting the access token and building a VkontakteApi::Client with it).

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :type (Symbol)

    The type of authorization being used (:site and :app_server supported).

  • :code (String)

    The code to exchange for an access token (for :site authorization type).

Returns:

Raises:

  • (ArgumentError)

    raises after receiving an unknown authorization type.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vkontakte_api/authorization.rb', line 45

def authorize(options = {})
  type = options.delete(:type) || :site
  
  options[:redirect_uri] ||= VkontakteApi.redirect_uri
  
  case type
  when :site
    code  = options.delete(:code)
    token = client.auth_code.get_token(code, options)
  when :app_server
    token = client.client_credentials.get_token(options, OPTIONS[:client_credentials].dup)
  else
    raise ArgumentError, "Unknown authorization type #{type.inspect}"
  end
  
  Client.new(token)
end