Class: OmniAuth::Strategies::Renren

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/omniauth/strategies/oauth2/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 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) ⇒ 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

  • 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
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 18

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

Instance Method Details

#auth_hashObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 26

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

#build_access_tokenObject



64
65
66
67
68
69
70
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 64

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



72
73
74
75
76
77
78
79
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 72

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



59
60
61
62
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 59

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

#session_keyObject



55
56
57
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 55

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

#signed_paramsObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 42

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



38
39
40
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 38

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



81
82
83
84
85
86
# File 'lib/omniauth/strategies/oauth2/renren.rb', line 81

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