Class: OmniAuth::Strategies::Tsina

Inherits:
OAuth
  • Object
show all
Defined in:
lib/omniauth_china/strategies/tsina.rb

Overview

Authenticate to TSina via OAuth and retrieve basic user information.

Usage:

use OmniAuth::Strategies::TSina, 'APIKey', 'APIKeySecret'

Instance Method Summary collapse

Constructor Details

#initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block) ⇒ Tsina

Returns a new instance of Tsina.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/omniauth_china/strategies/tsina.rb', line 16

def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
  @api_key = consumer_key
  
  client_options = {
    :site               => 'http://api.t.sina.com.cn',
    :request_token_path => '/oauth/request_token',
    :access_token_path  => '/oauth/access_token',
    :authorize_path     => '/oauth/authorize',
    :realm              => 'OmniAuth'
  }

  super(app, :tsina, consumer_key, consumer_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



30
31
32
33
34
35
36
# File 'lib/omniauth_china/strategies/tsina.rb', line 30

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

#request_phaseObject

MonkeyPatch session[‘tsina’] to true



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/omniauth_china/strategies/tsina.rb', line 53

def request_phase
  request_token = consumer.get_request_token(:oauth_callback => callback_url)
  session['oauth'] ||= {}
  session['oauth'][name.to_s] = {'callback_confirmed' => true, 'request_token' => request_token.token, 'request_secret' => request_token.secret}
  r = Rack::Response.new

  if request_token.callback_confirmed?
    r.redirect(request_token.authorize_url)
  else
    r.redirect(request_token.authorize_url(:oauth_callback => callback_url))
  end

  r.finish
rescue ::Timeout::Error => e
  fail!(:timeout, e)
end

#user_hashObject



70
71
72
73
74
75
76
# File 'lib/omniauth_china/strategies/tsina.rb', line 70

def user_hash
  # http://api.t.sina.com.cn/users/show/:id.json?source=appkey
  # @access_token.params[:user_id] is the UID
  # @api_key is the appkey
  uid = @access_token.params[:user_id]
  @user_hash ||= MultiJson.decode(@access_token.get("http://api.t.sina.com.cn/users/show/#{uid}.json?source=#{@api_key}").body)
end

#user_infoObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/omniauth_china/strategies/tsina.rb', line 38

def 
  user_hash = self.user_hash
  {
    'username' => user_hash['screen_name'],
    'name' => user_hash['name'],
    'location' => user_hash['location'],
    'image' => user_hash['profile_image_url'],
    'description' => user_hash['description'],
    'urls' => {
      'Tsina' => user_hash['url']
    }
  }
end