Class: OmniAuth::Strategies::TB

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

Overview

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

use OmniAuth::Strategies::TB, '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) ⇒ TB

Returns a new instance of TB.

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
26
27
# File 'lib/omniauth/strategies/taobao.rb', line 19

def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
  client_options = {
    :site => "https://oauth.taobao.com/",
    :authorize_url => "/authorize",
    :access_token_url => "/token"
  }

  super(app, :tb, client_id, client_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



63
64
65
66
67
68
69
# File 'lib/omniauth/strategies/taobao.rb', line 63

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

#generate_sign(params) ⇒ Object



71
72
73
74
75
# File 'lib/omniauth/strategies/taobao.rb', line 71

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

#request_phaseObject



51
52
53
54
# File 'lib/omniauth/strategies/taobao.rb', line 51

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

#user_dataObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/omniauth/strategies/taobao.rb', line 29

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



56
57
58
59
60
61
# File 'lib/omniauth/strategies/taobao.rb', line 56

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