Sha256: b7957902eaa9c470f9fd45b3e86260c1c9c8e14f339719a4636a694871b1f4f3

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

module Sorcery
  module Providers
    # This class adds support for OAuth with vk.com.
    #
    #   config.vk.key = <key>
    #   config.vk.secret = <secret>
    #   ...
    #
    class Vk < Base
      include Protocols::Oauth2

      attr_accessor :auth_path, :token_path, :user_info_url, :scope, :api_version

      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

      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:       user_info_mapping.values.join(','),
          scope:        scope,
          v:            api_version.to_s
        }

        response = access_token.get(user_info_url, 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

      # calculates and returns the url to which the user should be redirected,
      # to get authenticated at the external provider's site.
      def login_url(_params, _session)
        authorize_url(authorize_url: auth_path)
      end

      # tries to login the user from access token
      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
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sorcery-0.17.0 lib/sorcery/providers/vk.rb
sorcery-0.16.5 lib/sorcery/providers/vk.rb
sorcery-0.16.4 lib/sorcery/providers/vk.rb
sorcery-0.16.3 lib/sorcery/providers/vk.rb
sorcery-0.16.2 lib/sorcery/providers/vk.rb
sorcery-0.16.1 lib/sorcery/providers/vk.rb
sorcery-0.15.1 lib/sorcery/providers/vk.rb
sorcery-0.16.0 lib/sorcery/providers/vk.rb
sorcery-0.15.0 lib/sorcery/providers/vk.rb