Class: OmniAuth::Strategies::Renren

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/omniauth_china/strategies/renren.rb

Overview

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

Examples:

Basic Usage

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

Instance Method Summary collapse

Constructor Details

#initialize(app, client_id = nil, client_secret = nil, options = {}, &block) ⇒ Renren

Returns a new instance of Renren.

Parameters:

  • app (Rack Application)

    standard middleware application parameter

  • client_id (String) (defaults to: nil)

    the application id as [registered on Renren](dev.renren.com/)

  • client_secret (String) (defaults to: nil)

    the application secret as registered on Renren

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

    a customizable set of options

Options Hash (options):

  • :scope (String) — default: 'publish_feed,status_update'

    comma-separated extended permissions such as ‘publish_feed` and `status_update`



18
19
20
21
22
23
24
25
26
# File 'lib/omniauth_china/strategies/renren.rb', line 18

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

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

Instance Method Details

#auth_hashObject



78
79
80
81
82
83
84
# File 'lib/omniauth_china/strategies/renren.rb', line 78

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

#build_access_tokenObject



54
55
56
57
58
59
60
# File 'lib/omniauth_china/strategies/renren.rb', line 54

def build_access_token
  if renren_session.nil? || renrensession.empty?
    super
  else
    @access_token = ::OAuth2::AccessToken.new(client, renren_session['access_token'])
  end
end

#renren_sessionObject



62
63
64
65
66
67
68
69
# File 'lib/omniauth_china/strategies/renren.rb', line 62

def renren_session
  session_cookie = request.cookies["rrs_#{client.id}"]
  if session_cookie
    @renren_session ||= Rack::Utils.parse_query(request.cookies["rrs_#{client.id}"].gsub('"', ''))
  else
    nil
  end
end

#request_phaseObject



49
50
51
52
# File 'lib/omniauth_china/strategies/renren.rb', line 49

def request_phase
  options[:scope] ||= "publish_feed"
  super
end

#session_keyObject



45
46
47
# File 'lib/omniauth_china/strategies/renren.rb', line 45

def session_key
  @session_key ||= MultiJson.decode(@access_token.get('/renren_api/session_key'))
end

#signed_paramsObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/omniauth_china/strategies/renren.rb', line 32

def signed_params
  params = {}
  params[:api_key] = client.id
  params[:method] = "users.getInfo"
  params[:call_id] = Time.now.to_i
  params[:format] = "json"
  params[:v] = "1.0"
  params[:uids] = session_key["user"]["id"]
  params[:session_key] = session_key["renren_token"]["session_key"]
  params[:sig] = Digest::MD5.hexdigest(params.map {|k,v| "#{k}=#{v}"}.sort.join("") + client.secret)
  params
end

#user_dataObject



28
29
30
# File 'lib/omniauth_china/strategies/renren.rb', line 28

def user_data
  @data ||= MultiJson.decode(Net::HTTP.post_form(URI.parse('http://api.renren.com/restserver.do'), signed_params).body)[0]
end

#user_infoObject



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

def 
  {
    'name' => user_data["name"],
    'image' => user_data["tinyurl"]
  }
end