Permalink
Browse files

Added Taobao strategy

  • Loading branch information...
1 parent 07b8b45 commit c3d474a7538013f89c8850d6b3fe1720a46d15e3 @l4u l4u committed May 20, 2011
View
@@ -49,6 +49,7 @@ OmniAuth currently supports the following external providers:
* SmugMug (credit: [pchilton](https://github.com/pchilton))
* SoundCloud (credit: [leemartin](https://github.com/leemartin))
* T163 (credit: [quake](https://github.com/quake))
+ * Taobao (credit: [l4u](https://github.com/l4u))
* TeamBox (credit [jrom](https://github.com/jrom))
* Tqq (credit: [quake](https://github.com/quake))
* TradeMe (credit: [pchilton](https://github.com/pchilton))
@@ -35,6 +35,7 @@ module Strategies
autoload :SmugMug, 'omniauth/strategies/smug_mug'
autoload :SoundCloud, 'omniauth/strategies/sound_cloud'
autoload :T163, 'omniauth/strategies/t163'
+ autoload :Taobao, 'omniauth/strategies/taobao'
autoload :Teambox, 'omniauth/strategies/teambox'
autoload :ThirtySevenSignals, 'omniauth/strategies/thirty_seven_signals'
autoload :Tqq, 'omniauth/strategies/tqq'
@@ -0,0 +1,79 @@
+require 'omniauth/oauth'
+require 'multi_json'
+require 'digest/md5'
+require 'net/http'
+
+module OmniAuth
+ module Strategies
+ # Authenticate to Renren utilizing OAuth 2.0 and retrieve
+ # basic user information.
+ #
+ # @example Basic Usage
+ # use OmniAuth::Strategies::Taobao, 'client_id', 'client_secret'
+ class Taobao < OAuth2
+ # @param [Rack Application] app standard middleware application parameter
+ # @param [String] client_id the app key at taobao open platform
+ # @param [String] client_secret the app secret at taobao open platform
+ # @option options [String]
+
+ 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, :taobao, client_id, client_secret, client_options, options, &block)
+ end
+
+ 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
+
+ def request_phase
+ options[:state] ||= '1'
+ super
+ end
+
+ def user_info
+ {
+ 'name' => user_data["nick"],
+ 'email' => (user_data["email"] if user_data["email"]),
+ }
+ end
+
+ def auth_hash
+ OmniAuth::Utils.deep_merge(super, {
+ 'uid' => user_data['uid'],
+ 'user_info' => user_info,
+ 'extra' => {'user_hash' => user_data}
+ })
+ end
+
+ 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
+
+ end
+ end
+end
@@ -0,0 +1,5 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe OmniAuth::Strategies::Taobao do
+ it_should_behave_like "an oauth2 strategy"
+end

0 comments on commit c3d474a

Please sign in to comment.