Skip to content
This repository was archived by the owner on Jan 30, 2018. It is now read-only.

Commit 04826a4

Browse files
committed
Merge branch 'master' of github.com:teambox/teambox
2 parents 8cc45b6 + c754e8d commit 04826a4

File tree

17 files changed

+220
-344
lines changed

17 files changed

+220
-344
lines changed

Gemfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ gem 'pg', '~> 0.9.0', :require => nil, :group => 'pg'
2121
gem 'aws-s3', '~> 0.6.2', :require => 'aws/s3'
2222
gem 'hpricot', '~> 0.8.2'
2323
gem 'json'
24-
gem 'oauth2'
25-
gem 'oauth'
24+
gem 'oa-oauth', :require => 'omniauth/oauth'
2625

2726
group :plugins do
2827
gem 'sprockets-rails', '~> 0.0.1'

Gemfile.lock

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ GEM
1313
activesupport (= 2.3.10)
1414
activesupport (2.3.10)
1515
acts-as-list (0.1.2)
16-
addressable (2.2.0)
16+
addressable (2.2.2)
1717
after_commit (1.0.8)
1818
activerecord (< 3.0.0)
1919
aws-s3 (0.6.2)
@@ -80,7 +80,16 @@ GEM
8080
multi_json (0.0.4)
8181
mysql (2.8.1)
8282
nokogiri (1.4.3.1)
83-
oauth (0.4.3)
83+
oa-core (0.0.5)
84+
rack (~> 1.1)
85+
oa-oauth (0.0.1)
86+
json
87+
nokogiri
88+
oa-core (~> 0.0.0)
89+
oauth
90+
oauth2
91+
oauth (0.3.6)
92+
ruby-hmac (>= 0.3.1)
8493
oauth2 (0.0.13)
8594
faraday (~> 0.4.1)
8695
multi_json (>= 0.0.4)
@@ -109,6 +118,7 @@ GEM
109118
ruby-debug-base (~> 0.10.3.0)
110119
ruby-debug-base (0.10.3)
111120
linecache (>= 0.3)
121+
ruby-hmac (0.4.0)
112122
rubyforge (2.0.4)
113123
json_pure (>= 1.1.7)
114124
rubyzip (0.9.4)
@@ -161,8 +171,7 @@ DEPENDENCIES
161171
memcache-client (>= 1.7.4)
162172
mongrel (~> 1.1.5)
163173
mysql (~> 2.8.1)
164-
oauth
165-
oauth2
174+
oa-oauth
166175
paperclip (~> 2.3.1.1)
167176
pg (~> 0.9.0)
168177
pickle (~> 0.2.1)

app/controllers/auth_controller.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# TODO: Make it run on Heroku
2+
3+
class AuthController < ApplicationController
4+
skip_before_filter :login_required
5+
6+
def callback
7+
provider = params[:provider]
8+
begin
9+
auth_hash = params[:auth]
10+
AppLink.find_by_provider_and_app_user_id_and_user_id(provider, auth_hash[:uid], nil).try(:destroy)
11+
load_profile(auth_hash, provider)
12+
13+
if logged_in?
14+
if current_user.app_links.find_by_provider(@provider)
15+
flash[:notice] = t(:'oauth.already_linked_to_your_account')
16+
elsif AppLink.find_by_provider_and_app_user_id(provider, auth_hash[:uid])
17+
flash[:error] = t(:'oauth.already_taken_by_other_account')
18+
else
19+
current_user.link_to_app(provider, auth_hash[:uid])
20+
flash[:success] = t(:'oauth.account_linked')
21+
end
22+
return redirect_to(account_linked_accounts_path)
23+
else
24+
if oauth_login(provider, auth_hash[:uid])
25+
flash[:success] = t(:'oauth.logged_in')
26+
return redirect_to projects_path
27+
elsif User.find_by_email(auth_hash[:email])
28+
# TODO: locate existing user by email and ask to log in to link him
29+
flash[:notice] = t(:'oauth.user_already_exists_by_email', :email => auth_hash[:email])
30+
return redirect_to login_path
31+
elsif User.find_by_login(auth_hash[:login])
32+
flash[:notice] = t(:'oauth.user_already_exists_by_login', :login => auth_hash[:login])
33+
return redirect_to login_path
34+
else
35+
if signups_enabled?
36+
session[:profile] = @profile
37+
app_link = AppLink.create!(:provider => provider,
38+
:app_user_id => auth_hash[:uid],
39+
:custom_attributes => auth_hash)
40+
session[:app_link] = app_link.id
41+
return redirect_to signup_path
42+
else
43+
flash[:error] = t(:'users.new.no_public_signup')
44+
return redirect_to login_path
45+
end
46+
end
47+
end
48+
rescue
49+
render :text => %(<p>Authentification Error: #{params[:error]}:</p><p><a href="/auth/#{@provider}">Retry</a></p>)
50+
end
51+
end
52+
53+
def failure
54+
flash[:error] = "Authentification Error: #{params[:message]}"
55+
redirect_to :back rescue redirect_to login_path
56+
end
57+
58+
private
59+
# Authentificate with login
60+
def oauth_login(provider, auth_hash_uid)
61+
if app_link = AppLink.find(:first, :conditions => {:provider => provider, :app_user_id => auth_hash_uid})
62+
!!self.current_user = app_link.user if app_link.user
63+
end
64+
end
65+
66+
# Loads user's OAuth profile in @profile
67+
def load_profile(user, provider)
68+
@profile = {}
69+
70+
@profile[:login] = user[:user_info][:nickname] if user[:user_info][:nickname]
71+
@profile[:phone] = user[:user_info][:phone] if user[:user_info][:phone]
72+
73+
if user[:user_info][:first_name] and user[:user_info][:last_name]
74+
@profile[:first_name] = user[:user_info][:first_name]
75+
@profile[:last_name] = user[:user_info][:last_name]
76+
else
77+
@profile[:first_name] = user[:user_info][:name].try(:split).try(:first)
78+
@profile[:last_name] = user[:user_info][:name].try(:split).try(:second)
79+
end
80+
81+
# Extra
82+
@profile[:email] = user[:extra][:user_hash][:email] if user[:extra][:user_hash][:email]
83+
84+
if @profile[:login]
85+
@profile[:login] = User.find_available_login(@profile[:login])
86+
end
87+
end
88+
end

app/controllers/oauth_controller.rb

Lines changed: 0 additions & 219 deletions
This file was deleted.

0 commit comments

Comments
 (0)