Permalink
Browse files

user signup

  • Loading branch information...
Holden Thomas
Holden Thomas committed Dec 1, 2010
1 parent d559776 commit 8e05d9991f8409210fe75a6a145ef2560a14a7db
View
@@ -5,7 +5,8 @@ gem 'rails', '3.0.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
-gem 'sqlite3-ruby', :require => 'sqlite3'
+gem 'pg'
+#gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'devise', :git => 'git://github.com/plataformatec/devise', :branch => 'omniauth'
#gem 'devise'
gem 'omniauth'
View
@@ -94,6 +94,7 @@ GEM
oa-oauth (= 0.1.6)
oa-openid (= 0.1.6)
orm_adapter (0.0.3)
+ pg (0.9.0)
polyglot (0.3.1)
pyu-ruby-sasl (0.0.3.2)
rack (1.2.1)
@@ -124,7 +125,6 @@ GEM
ruby-openid-apps-discovery (1.2.0)
ruby-openid (>= 2.1.7)
rubyntlm (0.1.1)
- sqlite3-ruby (1.3.2)
thor (0.14.4)
treetop (1.4.8)
polyglot (>= 0.3.1)
@@ -138,5 +138,5 @@ PLATFORMS
DEPENDENCIES
devise!
omniauth
+ pg
rails (= 3.0.0)
- sqlite3-ruby
@@ -3,24 +3,39 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def method_missing(provider)
if !User.omniauth_providers.index(provider).nil?
#omniauth = request.env["omniauth.auth"]
- auth = env["omniauth.auth"]
+ omniauth = env["omniauth.auth"]
- if current_user
- current_user.user_tokens.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
+ if current_user #or User.find_by_email(auth.recursive_find_by_key("email"))
+ current_user.user_tokens.find_or_create_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
flash[:notice] = "Authentication successful"
redirect_to edit_user_registration_path
else
- authentication = UserToken.find_by_provider_and_uid(auth['provider'], auth['uid'])
+ authentication = UserToken.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
- flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => auth['provider']
+ flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => omniauth['provider']
sign_in_and_redirect(:user, authentication.user)
- #sign_in_and_redirect @user, :event => :authentication
- #render :text => env["omniauth.auth"].to_yaml
+ #sign_in_and_redirect(authentication.user, :event => :authentication)
else
- #session["devise.#{provider}_data"] = env["omniauth.auth"]
- redirect_to new_user_registration_url
+
+ #create a new user
+ unless omniauth.recursive_find_by_key("email").blank?
+ user = User.find_or_initialize_by_email(:email => omniauth.recursive_find_by_key("email"))
+ else
+ user = User.new
+ end
+
+ user.apply_omniauth(omniauth)
+ #user.confirm! #unless user.email.blank?
+
+ if user.save
+ flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => omniauth['provider']
+ sign_in_and_redirect(:user, user)
+ else
+ session[:omniauth] = omniauth.except('extra')
+ redirect_to new_user_registration_url
+ end
end
end
end
View
@@ -7,4 +7,33 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
+
+ def self.new_with_session(params, session)
+ super.tap do |user|
+ if data = session[:omniauth]
+ user.user_tokens.build(:provider => data['provider'], :uid => data['uid'])
+ end
+ end
+ end
+
+ def apply_omniauth(omniauth)
+ #add some info about the user
+ #self.name = omniauth['user_info']['name'] if name.blank?
+ #self.nickname = omniauth['user_info']['nickname'] if nickname.blank?
+
+ unless omniauth['credentials'].blank?
+ user_tokens.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
+ #user_tokens.build(:provider => omniauth['provider'],
+ # :uid => omniauth['uid'],
+ # :token => omniauth['credentials']['token'],
+ # :secret => omniauth['credentials']['secret'])
+ else
+ user_tokens.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
+ end
+ #self.confirm!# unless user.email.blank?
+ end
+
+ def password_required?
+ (user_tokens.empty? || !password.blank?) && super
+ end
end
View
@@ -1,10 +1,12 @@
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
- adapter: sqlite3
- database: db/development.sqlite3
+ adapter: postgresql
+ encoding: unicode
+ database: omniauth
pool: 5
- timeout: 5000
+ username: holden
+ password: postgres
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
@@ -147,4 +147,47 @@
# end
# manager.default_strategies(:scope => :user).unshift :twitter_oauth
# end
+
+ #monkey patch
+
+ require 'openid/store/nonce'
+ require 'openid/store/interface'
+ module OpenID
+ module Store
+ class Memcache < Interface
+ def use_nonce(server_url, timestamp, salt)
+ return false if (timestamp - Time.now.to_i).abs > Nonce.skew
+ ts = timestamp.to_s # base 10 seconds since epoch
+ nonce_key = key_prefix + 'N' + server_url + '|' + ts + '|' + salt
+ result = @cache_client.add(nonce_key, '', expiry(Nonce.skew + 5))
+
+ return result #== true (edited 10/25/10)
+ # return !!(result =~ /^STORED/)
+ end
+ end
+ end
+ end
+
+ class Hash
+ def recursive_find_by_key(key)
+ # Create a stack of hashes to search through for the needle which
+ # is initially this hash
+ stack = [ self ]
+
+ # So long as there are more haystacks to search...
+ while (to_search = stack.pop)
+ # ...keep searching for this particular key...
+ to_search.each do |k, v|
+ # ...and return the corresponding value if it is found.
+ return v if (k == key)
+
+ # If this value can be recursively searched...
+ if (v.respond_to?(:recursive_find_by_key))
+ # ...push that on to the list of places to search.
+ stack << v
+ end
+ end
+ end
+ end
+ end
end
@@ -6,7 +6,7 @@ def self.up
t.rememberable
t.trackable
- # t.confirmable
+ t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
View
@@ -30,7 +30,6 @@
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
- t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.string "remember_token"
t.datetime "remember_created_at"
@@ -39,6 +38,9 @@
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
+ t.string "confirmation_token"
+ t.datetime "confirmed_at"
+ t.datetime "confirmation_sent_at"
t.datetime "created_at"
t.datetime "updated_at"
end

0 comments on commit 8e05d99

Please sign in to comment.