Skip to content

Commit

Permalink
quick fixes for mongoid to work
Browse files Browse the repository at this point in the history
Please note that
1) generators are not changed, need to edit models manually
2) tests are not updated
  • Loading branch information
ybian committed Nov 6, 2014
1 parent 2d3595d commit b2c0229
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source "https://rubygems.org"
source "http://ruby.taobao.org"

# Declare your gem's dependencies in devise_token_auth.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
Expand Down
26 changes: 20 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ PATH
remote: .
specs:
devise_token_auth (0.1.30.beta3)
devise (~> 3.3)
devise (~> 3.4.1)
mongoid-locker (~> 0.3.0)
rails (~> 4.1)

GEM
remote: https://rubygems.org/
remote: http://ruby.taobao.org/
specs:
actionmailer (4.1.6)
actionpack (= 4.1.6)
Expand Down Expand Up @@ -69,12 +70,14 @@ GEM
attr_encrypted (1.3.2)
encryptor (>= 1.3.0)
bcrypt (3.1.9)
bson (2.3.0)
builder (3.2.2)
celluloid (0.15.2)
timers (~> 1.1.0)
codeclimate-test-reporter (0.4.0)
simplecov (>= 0.7.1, < 1.0.0)
coderay (1.1.0)
connection_pool (2.0.0)
devise (3.4.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand Down Expand Up @@ -126,6 +129,17 @@ GEM
builder
minitest (>= 5.0)
ruby-progressbar
mongoid (4.0.0)
activemodel (~> 4.0)
moped (~> 2.0.0)
origin (~> 2.1)
tzinfo (>= 0.3.37)
mongoid-locker (0.3.0)
mongoid (~> 4.0)
moped (2.0.1)
bson (~> 2.2)
connection_pool (~> 2.0)
optionable (~> 0.2.0)
multi_json (1.10.1)
multi_xml (0.5.5)
multipart-post (2.0.0)
Expand All @@ -144,8 +158,9 @@ GEM
multi_json (~> 1.3)
oauth2 (~> 1.0)
omniauth (~> 1.2)
optionable (0.2.0)
origin (2.1.1)
orm_adapter (0.5.0)
pg (0.17.1)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand Down Expand Up @@ -190,10 +205,10 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.1.4)
sprockets-rails (2.2.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (~> 2.8)
sprockets (>= 2.8, < 4.0)
sqlite3 (1.3.9)
thor (0.19.1)
thread_safe (0.3.4)
Expand Down Expand Up @@ -224,7 +239,6 @@ DEPENDENCIES
omniauth-facebook!
omniauth-github!
omniauth-google-oauth2!
pg
pry
pry-remote
rack-cors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def set_user_by_token(mapping=nil)
@client_id ||= 'default'

# mitigate timing attacks by finding by uid instead of auth token
user = uid && rc.find_by_uid(uid)
user = uid && rc.where(uid: uid).first

if user && user.valid_token?(@token, @client_id)
sign_in(:user, user, store: false, bypass: true)
Expand Down Expand Up @@ -102,6 +102,6 @@ def resource_class(m=nil)
def is_batch_request?(user, client_id)
user.tokens[client_id] and
user.tokens[client_id]['updated_at'] and
Time.parse(user.tokens[client_id]['updated_at']) > @request_started_at - DeviseTokenAuth.batch_request_buffer_throttle
user.tokens[client_id]['updated_at'] > @request_started_at - DeviseTokenAuth.batch_request_buffer_throttle
end
end
8 changes: 1 addition & 7 deletions app/controllers/devise_token_auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ def create
email = resource_params[:email]
end

q = "uid='#{email}' AND provider='email'"

if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
q = "BINARY uid='#{email}' AND provider='email'"
end

@resource = resource_class.where(q).first
@resource = resource_class.where(uid: email, provider: 'email').first

errors = nil

Expand Down
8 changes: 1 addition & 7 deletions app/controllers/devise_token_auth/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ def create
email = resource_params[:email]
end

q = "uid='#{email}' AND provider='email'"

if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
q = "BINARY uid='#{email}' AND provider='email'"
end

@resource = resource_class.where(q).first
@resource = resource_class.where(uid: email, provider: 'email').first

if @resource and valid_params? and @resource.valid_password?(resource_params[:password]) and @resource.confirmed?
# create client id
Expand Down
19 changes: 6 additions & 13 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
require 'mongoid-locker'

module DeviseTokenAuth::Concerns::User
extend ActiveSupport::Concern

included do
include Mongoid::Locker

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable

serialize :tokens, JSON
#serialize :tokens, JSON

validates_presence_of :email, if: Proc.new { |u| u.provider == 'email' }

# only validate unique emails among email registration users
validate :unique_email_user, on: :create

# can't set default on text fields in mysql, simulate here instead.
after_save :set_empty_token_hash
after_initialize :set_empty_token_hash

# get rid of dead tokens
before_save :destroy_expired_tokens


# don't use default devise email validation
def email_required?
false
Expand Down Expand Up @@ -181,10 +180,8 @@ def extend_batch_buffer(token, client_id)
return build_auth_header(token, client_id)
end


protected


# NOTE: ensure that fragment comes AFTER querystring for proper $location
# parsing using AngularJS.
def generate_url(url, params = {})
Expand All @@ -208,12 +205,8 @@ def unique_email_user
end
end

def set_empty_token_hash
self.tokens ||= {} if has_attribute?(:tokens)
end

def destroy_expired_tokens
self.tokens.delete_if{|cid,v|
self.tokens.delete_if {|cid,v|
expiry = v[:expiry] || v["expiry"]
DateTime.strptime(expiry.to_s, '%s') < Time.now
}
Expand Down
5 changes: 3 additions & 2 deletions devise_token_auth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Gem::Specification.new do |s|
s.test_files = Dir["test/**/*"]

s.add_dependency "rails", "~> 4.1"
s.add_dependency "devise", "~> 3.3"
s.add_dependency "devise", "~> 3.4.1"
s.add_dependency 'mongoid-locker', '~> 0.3.0'

s.add_development_dependency "sqlite3", "~> 1.3"
s.add_development_dependency 'pg'
# s.add_development_dependency 'pg'
s.add_development_dependency 'mysql2'
end

0 comments on commit b2c0229

Please sign in to comment.