Skip to content
Browse files

Add liveid in providers and fix oauth specs

  • Loading branch information...
1 parent 73dd6f3 commit 882411a8dddbf43b99073ad8e8f3e4e75458a631 @mlainez mlainez committed Nov 7, 2011
View
14 lib/generators/sorcery/templates/initializer.rb
@@ -37,11 +37,14 @@
# -- external --
# config.external_providers = [] # What providers are supported by this app,
- # i.e. [:twitter, :facebook, :github, :google] .
+ # i.e. [:twitter, :facebook, :github, :google, :liveid] .
# config.ca_file = 'path/to/ca_file' # Path to ca_file. By default use a internal ca-bundle.crt.
# You can change it by your local ca_file.
# i.e. '/etc/pki/tls/certs/ca-bundle.crt'
+ # Twitter wil not accept any requests nor redirect uri containing localhost,
+ # make sure you use 0.0.0.0:3000 to access your app in development
+ #
# config.twitter.key = "eYVNBjBDi33aa9GkA3w"
# config.twitter.secret = "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8"
# config.twitter.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=twitter"
@@ -61,6 +64,15 @@
# config.google.secret = "4oE6kXqbL_LN-VGcGcg7qgdL"
# config.google.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=google"
# config.google.user_info_mapping = {:email => "email", :username => "name"}
+ #
+ # To use liveid in development mode you have to replace mydomain.com with
+ # a valid domain even in development. To use a valid domain in development
+ # simply add your domain in your /etc/hosts file in front of 127.0.0.1
+ #
+ # config.liveid.key = ""
+ # config.liveid.secret = ""
+ # config.liveid.callback_url = "http://mydomain.com:3000/oauth/callback?provider=liveid"
+ # config.liveid.user_info_mapping = {:username => "name"}
# --- user config ---
config.user_config do |user|
View
1 lib/sorcery.rb
@@ -35,6 +35,7 @@ module Providers
autoload :Facebook, 'sorcery/controller/submodules/external/providers/facebook'
autoload :Github, 'sorcery/controller/submodules/external/providers/github'
autoload :Google, 'sorcery/controller/submodules/external/providers/google'
+ autoload :Liveid, 'sorcery/controller/submodules/external/providers/liveid'
end
end
end
View
90 lib/sorcery/controller/submodules/external/providers/liveid.rb
@@ -0,0 +1,90 @@
+module Sorcery
+ module Controller
+ module Submodules
+ module External
+ module Providers
+ # This module adds support for OAuth with microsoft liveid
+ # When included in the 'config.providers' option, it adds a new option, 'config.liveid'.
+ # Via this new option you can configure LiveId specific settings like your app's key and secret.
+ #
+ # config.liveid.key = <key>
+ # config.liveid.secret = <secret>
+ # ...
+ #
+ module Liveid
+ def self.included(base)
+ base.module_eval do
+ class << self
+ attr_reader :liveid # access to liveid_client.
+
+ def merge_liveid_defaults!
+ @defaults.merge!(:@liveid => LiveidClient)
+ end
+ end
+ merge_liveid_defaults!
+ update!
+ end
+ end
+
+ module LiveidClient
+ class << self
+ attr_accessor :key,
+ :secret,
+ :callback_url,
+ :site,
+ :auth_url,
+ :token_path,
+ :user_info_url,
+ :scope,
+ :user_info_mapping
+
+ include Protocols::Oauth2
+
+ def init
+ @site = "https://oauth.live.com/"
+ @auth_url = "/authorize"
+ @token_path = "/token"
+ @user_info_url = "https://apis.live.net/v5.0/me"
+ @scope = "wl.basic wl.emails wl.offline_access"
+ @user_info_mapping = {}
+ end
+
+ def get_user_hash
+ user_hash = {}
+ @access_token.token_param = "access_token"
+ response = @access_token.get(@user_info_url)
+ user_hash[:user_info] = JSON.parse(response)
+ user_hash[:uid] = user_hash[:user_info]['id']
+ user_hash
+ end
+
+ def has_callback?
+ true
+ end
+
+ # calculates and returns the url to which the user should be redirected,
+ # to get authenticated at the external provider's site.
+ def login_url(params,session)
+ self.authorize_url({:authorize_url => @auth_url})
+ end
+
+ # tries to login the user from access token
+ def process_callback(params,session)
+ args = {}
+ args.merge!({:code => params[:code]}) if params[:code]
+ options = {
+ :access_token_path => @token_path,
+ :access_token_method => :post
+ }
+ @access_token = self.get_access_token(args, options)
+ end
+ end
+ init
+ end
+
+ end
+ end
+ end
+ end
+ end
+end
View
12 spec/rails3/app/controllers/application_controller.rb
@@ -88,6 +88,10 @@ def login_at_test4
login_at(:google)
end
+ def login_at_test5
+ login_at(:liveid)
+ end
+
def test_login_from
if @user = login_from(:twitter)
redirect_to "bla", :notice => "Success!"
@@ -120,6 +124,14 @@ def test_login_from4
end
end
+ def test_login_from5
+ if @user = login_from(:liveid)
+ redirect_to "bla", :notice => "Success!"
+ else
+ redirect_to "blu", :alert => "Failed!"
+ end
+ end
+
def test_create_from_provider
provider = params[:provider]
login_from(provider)
View
55 spec/rails3/spec/controller_oauth2_spec.rb
@@ -2,7 +2,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../../shared_examples/controller_oauth2_shared_examples')
def stub_all_oauth2_requests!
- OAuth2::Strategy::WebServer.any_instance.stub_chain(:get_access_token, :get).and_return({
+ web_server = OAuth2::Strategy::WebServer.any_instance
+ access_token = mock(OAuth2::AccessToken)
+ access_token.stub(:token_param=)
+ access_token.stub(:get).and_return({
"id"=>"123",
"name"=>"Noam Ben Ari",
"first_name"=>"Noam",
@@ -18,13 +21,14 @@ def stub_all_oauth2_requests!
"languages"=>[{"id"=>"108405449189952", "name"=>"Hebrew"}, {"id"=>"106059522759137", "name"=>"English"}, {"id"=>"112624162082677", "name"=>"Russian"}],
"verified"=>true,
"updated_time"=>"2011-02-16T20:59:38+0000"}.to_json)
+ web_server.stub(:get_access_token).and_return(access_token)
end
describe ApplicationController do
before(:all) do
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
sorcery_reload!([:external])
- sorcery_controller_property_set(:external_providers, [:facebook, :github, :google])
+ sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid])
sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
@@ -34,6 +38,9 @@ def stub_all_oauth2_requests!
sorcery_controller_external_property_set(:google, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:google, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:google, :callback_url, "http://blabla.com")
+ sorcery_controller_external_property_set(:liveid, :key, "eYVNBjBDi33aa9GkA3w")
+ sorcery_controller_external_property_set(:liveid, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
+ sorcery_controller_external_property_set(:liveid, :callback_url, "http://blabla.com")
end
after(:all) do
@@ -99,7 +106,7 @@ def stub_all_oauth2_requests!
create_new_user
get :login_at_test4
response.should be_a_redirect
- response.should redirect_to("http://myapi.com/oauth/authorize?client_id=key&redirect_uri=http%3A%2F%2Fblabla.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&response_type=code")
+ response.should redirect_to("https://accounts.google.com/o/oauth2/auth?client_id=#{::Sorcery::Controller::Config.google.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&response_type=code")
end
it "'login_from' logins if user exists (google)" do
@@ -116,6 +123,28 @@ def stub_all_oauth2_requests!
flash[:alert].should == "Failed!"
end
+ # provider: liveid
+ it "login_at redirects correctly (liveid)" do
+ create_new_user
+ get :login_at_test5
+ response.should be_a_redirect
+ response.should redirect_to("https://oauth.live.com/authorize?client_id=#{::Sorcery::Controller::Config.liveid.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=wl.basic%20wl.emails%20wl.offline_access&response_type=code")
+ end
+
+ it "'login_from' logins if user exists (liveid)" do
+ sorcery_model_property_set(:authentications_class, Authentication)
+ create_new_external_user(:liveid)
+ get :test_login_from5
+ flash[:notice].should == "Success!"
+ end
+
+ it "'login_from' fails if user doesn't exist (liveid)" do
+ sorcery_model_property_set(:authentications_class, Authentication)
+ create_new_user
+ get :test_login_from5
+ flash[:alert].should == "Failed!"
+ end
+
end
@@ -127,7 +156,7 @@ def stub_all_oauth2_requests!
before(:all) do
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activation")
sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
- sorcery_controller_property_set(:external_providers, [:facebook, :github])
+ sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid])
sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
@@ -137,6 +166,9 @@ def stub_all_oauth2_requests!
sorcery_controller_external_property_set(:google, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:google, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:google, :callback_url, "http://blabla.com")
+ sorcery_controller_external_property_set(:liveid, :key, "eYVNBjBDi33aa9GkA3w")
+ sorcery_controller_external_property_set(:liveid, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
+ sorcery_controller_external_property_set(:liveid, :callback_url, "http://blabla.com")
end
@@ -191,5 +223,20 @@ def stub_all_oauth2_requests!
@user.activate!
ActionMailer::Base.deliveries.size.should == old_size
end
+
+ # provider: liveid
+ it "should not send activation email to external users (liveid)" do
+ old_size = ActionMailer::Base.deliveries.size
+ create_new_external_user(:liveid)
+ ActionMailer::Base.deliveries.size.should == old_size
+ end
+
+ it "should not send external users an activation success email (liveid)" do
+ sorcery_model_property_set(:activation_success_email_method_name, nil)
+ create_new_external_user(:liveid)
+ old_size = ActionMailer::Base.deliveries.size
+ @user.activate!
+ ActionMailer::Base.deliveries.size.should == old_size
+ end
end
end

0 comments on commit 882411a

Please sign in to comment.
Something went wrong with that request. Please try again.