-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
75 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,79 @@ | ||
require 'omniauth_china/oauth_china' | ||
require 'omniauth/oauth' | ||
require 'multi_json' | ||
|
||
module OmniAuth | ||
module Strategies | ||
class Renren | ||
include OmniAuth::Strategy | ||
|
||
# Authenticate to Renren utilizing OAuth 2.0 and retrieve | ||
# basic user information. | ||
# | ||
# @example Basic Usage | ||
# use OmniAuth::Strategies::Renren, 'client_id', 'client_secret' | ||
class Renren < OAuth2 | ||
autoload :Session, 'omniauth_china/strategies/renren/session' | ||
autoload :Service, 'omniauth_china/strategies/renren/service' | ||
autoload :Helper, 'omniauth_china/strategies/renren/helper' | ||
|
||
|
||
class << self | ||
def api_key | ||
@@api_key | ||
end | ||
|
||
def secret_key | ||
@@secret_key | ||
end | ||
def api_key; @@api_key; end | ||
def secret_key; @@secret_key; end | ||
end | ||
|
||
def initialize(app, api_key, secret_key, options = {}) | ||
@@api_key = api_key | ||
@@secret_key = secret_key | ||
|
||
super(app, :renren, options) | ||
|
||
# @param [Rack Application] app standard middleware application parameter | ||
# @param [String] client_id the application id as [registered on Renren](http://www.renren.com/developers/) | ||
# @param [String] client_secret the application secret as registered on Renren | ||
# @option options [String] :scope ('email') comma-separated extended permissions such as `email` and `manage_pages` | ||
def initialize(app, client_id = nil, client_secret = nil, options = {}, &block) | ||
@@api_key = client_id | ||
@@secret_key = client_secret | ||
super(app, :renren, client_id, client_secret, {:site => 'https://graph.renren.com/', :access_token_path => "/oauth/token"}, options, &block) | ||
end | ||
|
||
|
||
def user_hash | ||
@data ||= MultiJson.decode(@access_token.get('/renren_api/session_key', { :oauth_token => @access_token.token }, { "Accept-Language" => "zh;"})) | ||
@renren_session ||= Renren::Session.new(@data) | ||
end | ||
|
||
def request_phase | ||
@response.finish | ||
options[:scope] ||= "email" | ||
options[:response_type] ||= "code" | ||
super | ||
end | ||
|
||
|
||
# need to have :grant_type=>"authorization_code" for renren to work | ||
def callback_phase | ||
@renren_session = Renren::Session.new(request.cookies) | ||
super | ||
if request.params['error'] || request.params['error_reason'] | ||
raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri']) | ||
end | ||
verifier = request.params['code'] | ||
@access_token = client.web_server.get_access_token(verifier, {:redirect_uri => callback_url, :grant_type=>"authorization_code" }) | ||
@env['omniauth.auth'] = auth_hash | ||
call_app! | ||
rescue ::OAuth2::HTTPError, ::OAuth2::AccessDenied, CallbackError => e | ||
fail!(:invalid_credentials, e) | ||
end | ||
|
||
def auth_hash | ||
OmniAuth::Utils.deep_merge(super, { | ||
'uid' => @renren_session.uid, | ||
'user_info' => @renren_session.user, | ||
'uid' => user_hash.uid, | ||
'user_info' => user_info, | ||
'extra' => { | ||
'renren_session' => @renren_session | ||
'user_hash' => user_hash.user | ||
} | ||
}) | ||
end | ||
|
||
def user_info | ||
user_hash = self.user_hash.user | ||
{ | ||
'username' => user_hash['name'], | ||
'name' => user_hash['name'], | ||
'image' => user_hash['tinyurl'], | ||
'vip' => user_hash['vip'], | ||
'headurl' => user_hash['headurl'] | ||
} | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module OmniauthChina | ||
VERSION = "0.0.6" | ||
VERSION = "0.0.7" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') | ||
|
||
describe OmniAuth::Strategies::Renren do | ||
it_should_behave_like "an oauth2 strategy" | ||
end |