Class: OmniAuth::Strategies::Taobao

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

Overview

Authenticate to Renren utilizing OAuth 2.0 and retrieve basic user information.

use OmniAuth::Strategies::TaoBao, 'client_id', 'client_secret'

Examples:

Basic Usage

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) ⇒ Taobao

Returns a new instance of Taobao.

Parameters:

  • app (Rack Application)

    standard middleware application parameter

  • client_id (String) (defaults to: nil)

    the app key at taobao open platform

  • client_secret (String) (defaults to: nil)

    the app secret at taobao open platform

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

    a customizable set of options

Options Hash (options):

  • (String)


19
20
21
22
23
24
25
# File 'lib/omniauth/strategies/oauth2/taobao.rb', line 19

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

Instance Method Details

#auth_hashObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/omniauth/strategies/oauth2/taobao.rb', line 27

def auth_hash
  OmniAuth::Utils.deep_merge(
    super, {
      'uid' => user_data['uid'],
      'user_info' => ,
      'extra' => {
        'user_hash' => user_data,
      },
    }
  )
end

#generate_sign(params) ⇒ Object



73
74
75
76
77
78
# File 'lib/omniauth/strategies/oauth2/taobao.rb', line 73

def generate_sign(params)
  # params.sort.collect { |k, v| "#{k}#{v}" }
  str = client_secret + params.sort {|a,b| "#{a[0]}"<=>"#{b[0]}"}.flatten.join + client_secret
  params['sign'] = Digest::MD5.hexdigest(str).upcase!
  params
end

#request_phaseObject



61
62
63
64
# File 'lib/omniauth/strategies/oauth2/taobao.rb', line 61

def request_phase
  options[:state] ||= '1'
  super
end

#user_dataObject



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

def user_data
  # TODO to be moved in options
  url = 'http://gw.api.taobao.com/router/rest'

  query_param = {
    :app_key => client_id,

    # TODO to be moved in options
    # TODO add more default fields (http://my.open.taobao.com/apidoc/index.htm#categoryId:1-dataStructId:3)
    :fields => 'user_id,uid,nick,sex,buyer_credit,seller_credit,location,created,last_visit,birthday,type,status,alipay_no,alipay_account,alipay_account,email,consumer_protection,alipay_bind',
    :format => 'json',
    :method => 'taobao.user.get',
    :session => @access_token.token,
    :sign_method => 'md5',
    :timestamp   => Time.now.strftime('%Y-%m-%d %H:%M:%S'),
    :v => '2.0'
  }
  query_param = generate_sign(query_param)
  res = Net::HTTP.post_form(URI.parse(url), query_param)
  @data ||= MultiJson.decode(res.body)['user_get_response']['user']
end

#user_infoObject



66
67
68
69
70
71
# File 'lib/omniauth/strategies/oauth2/taobao.rb', line 66

def 
  {
    'name' => user_data['nick'],
    'email' => (user_data['email'] if user_data['email']),
  }
end