From 224a7332ed5d69949af92c114bad5d7f545f054a Mon Sep 17 00:00:00 2001 From: GuillauM Aug Date: Thu, 18 Aug 2011 05:02:29 +0200 Subject: [PATCH] added Viadeo through OAuth2.0 --- README.md | 1 + oa-oauth/lib/omniauth/oauth.rb | 1 + .../lib/omniauth/strategies/oauth2/viadeo.rb | 72 +++++++++++++++++++ .../omniauth/strategies/oauth2/viadeo_spec.rb | 5 ++ 4 files changed, 79 insertions(+) create mode 100644 oa-oauth/lib/omniauth/strategies/oauth2/viadeo.rb create mode 100644 oa-oauth/spec/omniauth/strategies/oauth2/viadeo_spec.rb diff --git a/README.md b/README.md index c0abff0..c83985e 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ OmniAuth currently supports the following external providers: * Tsohu (credit: [quake](https://github.com/quake)) * Tumblr (credit: [jamiew](https://github.com/jamiew)) * Twitter (credit: [mbleigh](https://github.com/mbleigh)) + * Viadeo (credit: [guillaug](https://github.com/guillaug)) * Vimeo (credit: [jamiew](https://github.com/jamiew)) * Vkontakte (credit: [german](https://github.com/german)) * WePay (credit: [ryanwood](https://github.com/ryanwood)) diff --git a/oa-oauth/lib/omniauth/oauth.rb b/oa-oauth/lib/omniauth/oauth.rb index 14dcf5d..23d8044 100644 --- a/oa-oauth/lib/omniauth/oauth.rb +++ b/oa-oauth/lib/omniauth/oauth.rb @@ -60,6 +60,7 @@ module OmniAuth autoload :TaoBao, 'omniauth/strategies/oauth2/taobao' autoload :Teambox, 'omniauth/strategies/oauth2/teambox' autoload :ThirtySevenSignals, 'omniauth/strategies/oauth2/thirty_seven_signals' + autoload :Viadeo, 'omniauth/strategies/oauth2/viadeo' autoload :Vkontakte, 'omniauth/strategies/oauth2/vkontakte' autoload :WePay, 'omniauth/strategies/oauth2/we_pay' diff --git a/oa-oauth/lib/omniauth/strategies/oauth2/viadeo.rb b/oa-oauth/lib/omniauth/strategies/oauth2/viadeo.rb new file mode 100644 index 0000000..dd99077 --- /dev/null +++ b/oa-oauth/lib/omniauth/strategies/oauth2/viadeo.rb @@ -0,0 +1,72 @@ +require 'omniauth/oauth' +require 'multi_json' + +module OmniAuth + module Strategies + # Authenticate to Viadeo utilizing OAuth 2.0 and retrieve + # basic user information. + # + # @example Basic Usage + # use OmniAuth::Strategies::Viadeo, 'client_id', 'client_secret' + class Viadeo < OmniAuth::Strategies::OAuth2 + # @param [Rack Application] app standard middleware application parameter + # @param [String] client_id the application id as [registered on Viadeo](http://dev.viadeo.com/) + # @param [String] client_secret the application secret as registered on Facebook + def initialize(app, client_id=nil, client_secret=nil, options = {}, &block) + client_options = { + :site => 'https://api.viadeo.com/', + :authorize_url => 'https://secure.viadeo.com/oauth-provider/authorize2', + :token_url => 'https://secure.viadeo.com/oauth-provider/access_token2' + } + super(app, :viadeo, client_id, client_secret, client_options, options, &block) + end + + def auth_hash + OmniAuth::Utils.deep_merge( + super, { + 'uid' => user_data['id'], + 'user_info' => user_info, + 'extra' => { + 'user_hash' => user_data, + }, + } + ) + end + + def user_data + @data ||= MultiJson.decode(@access_token.get('/me').body) + end + + def request_phase + options[:response_type] ||= 'code' + super + end + + def callback_phase + options[:grant_type] ||= 'authorization_code' + super + end + + def user_info + { + 'name' => user_data['name'], + 'link' => user_data['link'], + 'first_name' => user_data['first_name'], + 'last_name' => user_data['last_name'], + 'gender' => user_data['gender'], + 'nickname' => user_data['nickname'], + 'has_picture' => user_data['has_picture'] , + 'picture_small' => user_data['picture_small'], + 'picture_large' => user_data['picture_large'], + 'headline' => user_data['headline'], + 'introduction' => user_data['introduction'], + 'interests' => user_data['interests'], + 'location' => user_data['location'], + 'is_premium' => user_data['is_premium'], + 'premium_since' => user_data['premium_since'] + } + end + + end + end +end diff --git a/oa-oauth/spec/omniauth/strategies/oauth2/viadeo_spec.rb b/oa-oauth/spec/omniauth/strategies/oauth2/viadeo_spec.rb new file mode 100644 index 0000000..d692987 --- /dev/null +++ b/oa-oauth/spec/omniauth/strategies/oauth2/viadeo_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe OmniAuth::Strategies::Viadeo do + it_should_behave_like "an oauth2 strategy" +end