Class: OmniAuth::Strategies::Tsina

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

Overview

Authenticate to TSina via OAuth and retrieve basic user information.

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

Instance Attribute Summary

Attributes inherited from OAuth

#consumer_key, #consumer_options, #consumer_secret, #name

Instance Method Summary collapse

Methods inherited from OAuth

#consumer, #unique_id

Constructor Details

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

Returns a new instance of Tsina.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/omniauth/strategies/oauth/tsina.rb', line 13

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

Instance Method Details

#auth_hashObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/omniauth/strategies/oauth/tsina.rb', line 25

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

#callback_phaseObject

MonkeyPath to symbolize tina parameters



70
71
72
73
74
# File 'lib/omniauth/strategies/oauth/tsina.rb', line 70

def callback_phase
  session[:oauth].stringify_keys!
  session[:oauth][name.to_s].stringify_keys! if session[:oauth][name.to_s]
  super
end

#request_phaseObject

MonkeyPatch session['oauth']['tsina']['callback_confirmed'] to true



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

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



76
77
78
79
80
81
82
# File 'lib/omniauth/strategies/oauth/tsina.rb', line 76

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



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

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