easy_auth 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/README.md +3 -1
  2. data/Rakefile +5 -21
  3. data/app/helpers/easy_auth_helper.rb +41 -0
  4. data/app/models/easy_auth/identity.rb +3 -0
  5. data/db/migrate/{20120227014023_create_identities.rb → 20120227014023_create_easy_auth_identities.rb} +3 -2
  6. data/lib/easy_auth/controllers/authenticated.rb +19 -0
  7. data/lib/easy_auth/controllers/sessions.rb +86 -0
  8. data/lib/easy_auth/controllers.rb +5 -0
  9. data/lib/easy_auth/engine.rb +0 -5
  10. data/lib/easy_auth/mailers.rb +3 -0
  11. data/lib/easy_auth/models/account.rb +38 -0
  12. data/lib/easy_auth/models/identities.rb +3 -0
  13. data/lib/easy_auth/models/identity.rb +43 -0
  14. data/lib/easy_auth/models.rb +7 -0
  15. data/lib/easy_auth/reverse_concern.rb +27 -0
  16. data/lib/easy_auth/routes.rb +5 -18
  17. data/lib/easy_auth/token_generator.rb +7 -0
  18. data/lib/easy_auth/version.rb +1 -1
  19. data/lib/easy_auth.rb +44 -2
  20. metadata +29 -34
  21. data/MIT-LICENSE +0 -20
  22. data/app/controllers/password_reset_controller.rb +0 -3
  23. data/app/mailers/password_reset_mailer.rb +0 -4
  24. data/app/mixins/easy_auth/controllers/authenticated.rb +0 -14
  25. data/app/mixins/easy_auth/controllers/password_reset.rb +0 -56
  26. data/app/mixins/easy_auth/controllers/sessions.rb +0 -43
  27. data/app/mixins/easy_auth/helpers.rb +0 -44
  28. data/app/mixins/easy_auth/mailers/password_reset.rb +0 -11
  29. data/app/mixins/easy_auth/models/account.rb +0 -56
  30. data/app/mixins/easy_auth/models/identity.rb +0 -57
  31. data/app/models/identity.rb +0 -3
  32. data/app/views/layouts/easy_auth/application.html.erb +0 -14
  33. data/app/views/password_reset/edit.html.erb +0 -13
  34. data/app/views/password_reset/new.html.erb +0 -9
  35. data/app/views/password_reset_mailer/reset.html.erb +0 -1
  36. data/app/views/password_reset_mailer/reset.text.erb +0 -1
  37. data/app/views/sessions/new.html.erb +0 -17
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # EasyAuth #
2
2
 
3
- [![Build Status](http://travis-ci.org/dockyard/easy_auth.png)](http://travis-ci.org/dockyard/easy_auth)
3
+ [![Build Status](https://secure.travis-ci.org/dockyard/easy_auth.png?branch=master)](http://travis-ci.org/dockyard/easy_auth)
4
+ [![Dependency Status](https://gemnasium.com/dockyard/easy_auth.png?travis)](https://gemnasium.com/dockyard/easy_auth)
5
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/dockyard/easy_auth)
4
6
 
5
7
  Dead simple drop in authentication for Rails
6
8
 
data/Rakefile CHANGED
@@ -1,26 +1,10 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
- begin
8
- require 'rdoc/task'
9
- rescue LoadError
10
- require 'rdoc/rdoc'
11
- require 'rake/rdoctask'
12
- RDoc::Task = Rake::RDocTask
13
- end
1
+ require 'bundler/setup'
2
+ require 'rspec/core/rake_task'
3
+ Bundler::GemHelper.install_tasks
14
4
 
15
- RDoc::Task.new(:rdoc) do |rdoc|
16
- rdoc.rdoc_dir = 'rdoc'
17
- rdoc.title = 'EasyAuth'
18
- rdoc.options << '--line-numbers'
19
- rdoc.rdoc_files.include('README.rdoc')
20
- rdoc.rdoc_files.include('lib/**/*.rb')
5
+ RSpec::Core::RakeTask.new('default') do |t|
6
+ t.pattern = FileList['spec/**/*_spec.rb']
21
7
  end
22
8
 
23
9
  APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
10
  load 'rails/tasks/engine.rake'
25
-
26
- Bundler::GemHelper.install_tasks
@@ -0,0 +1,41 @@
1
+ module EasyAuthHelper
2
+ def self.included(base)
3
+ base.class_eval do
4
+ helper_method :current_account, :current_user, :account_signed_in?, :user_signed_in?, :account_not_signed_in?, :user_not_signed_in?
5
+ end
6
+ end
7
+
8
+ def current_account
9
+ if session[:session_token] && session[:account_class]
10
+ begin
11
+ @current_account ||= session[:account_class].constantize.find_by_session_token(session[:session_token])
12
+ rescue
13
+ @current_account = nil
14
+ session.delete(:session_token)
15
+ end
16
+ elsif cookies[:remember_token]
17
+ begin
18
+ @current_account ||= EasyAuth.identity_model.find_by_remember_token(cookies[:remember_token]).account
19
+ rescue
20
+ @current_acount = nil
21
+ cookies.delete(:remember_token)
22
+ end
23
+ else
24
+ session.delete(:session_token)
25
+ cookies.delete(:remember_token)
26
+ end
27
+
28
+ @current_account
29
+ end
30
+ alias :current_user :current_account
31
+
32
+ def account_signed_in?
33
+ current_account
34
+ end
35
+ alias :user_signed_in? :account_signed_in?
36
+
37
+ def account_not_signed_in?
38
+ !account_signed_in?
39
+ end
40
+ alias :user_not_signed_in? :account_not_signed_in?
41
+ end
@@ -0,0 +1,3 @@
1
+ class EasyAuth::Identity < ActiveRecord::Base
2
+ include EasyAuth::Models::Identity
3
+ end
@@ -1,12 +1,13 @@
1
- class CreateIdentities < ActiveRecord::Migration
1
+ class CreateEasyAuthIdentities < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :identities do |t|
4
4
  t.string :username
5
- t.string :password_digest
5
+ t.string :token
6
6
  t.string :account_type
7
7
  t.integer :account_id
8
8
  t.string :reset_token
9
9
  t.string :remember_token
10
+ t.string :type
10
11
  t.timestamps
11
12
  end
12
13
 
@@ -0,0 +1,19 @@
1
+ module EasyAuth::Controllers::Authenticated
2
+ extend EasyAuth::ReverseConcern
3
+
4
+ reverse_included do
5
+ before_filter :attempt_to_authenticate
6
+ end
7
+
8
+ private
9
+
10
+ def attempt_to_authenticate
11
+ if account_not_signed_in?
12
+ session[:requested_path] = request.path
13
+ respond_to do |format|
14
+ format.html { redirect_to main_app.sign_in_url }
15
+ format.json { render :json => {}, :status => 401 }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,86 @@
1
+ module EasyAuth::Controllers::Sessions
2
+ extend EasyAuth::ReverseConcern
3
+
4
+ def new
5
+ EasyAuth.new_session(self)
6
+ end
7
+
8
+ def create
9
+ if identity = EasyAuth.authenticate(self)
10
+ identity.set_account_session(session)
11
+ set_remember(identity)
12
+ if identity.remember
13
+ cookies[:remember_token] = { :value => identity.generate_remember_token!, :expires => identity.remember_time.from_now }
14
+ end
15
+ after_successful_sign_in(identity)
16
+ else
17
+ @identity = EasyAuth.find_identity_model(self).new(params[params[:identity]])
18
+ after_failed_sign_in(@identity)
19
+ end
20
+ end
21
+
22
+ def destroy
23
+ session.delete(:session_token)
24
+ session.delete(:account_class)
25
+ cookies.delete(:remember_token)
26
+ after_sign_out
27
+ end
28
+
29
+ private
30
+
31
+ def after_with_or_default(method_name, identity)
32
+ send("#{method_name}_with_#{params[:identity]}", identity) || send("#{method_name}_default", identity)
33
+ end
34
+
35
+ def after_successful_sign_in(identity)
36
+ after_with_or_default(__method__, identity)
37
+ end
38
+
39
+ def after_successful_sign_in_url(identity)
40
+ after_with_or_default(__method__, identity)
41
+ end
42
+
43
+ def after_failed_sign_in(identity)
44
+ after_with_or_default(__method__, identity)
45
+ end
46
+
47
+ def after_successful_sign_in_default(identity)
48
+ redirect_to(session.delete(:requested_path) || after_successful_sign_in_url(identity), :notice => I18n.t('easy_auth.sessions.create.notice'))
49
+ end
50
+
51
+ def after_successful_sign_in_url_default(identity)
52
+ identity.account
53
+ end
54
+
55
+ def after_failed_sign_in_default(identity)
56
+ flash.now[:error] = I18n.t('easy_auth.sessions.create.error')
57
+ render :new
58
+ end
59
+
60
+ def after_sign_out
61
+ redirect_to main_app.root_url, :notice => I18n.t('easy_auth.sessions.delete.notice')
62
+ end
63
+
64
+ def no_authentication
65
+ if account_signed_in?
66
+ redirect_to no_authentication_url
67
+ end
68
+ end
69
+
70
+ def no_authentication_url
71
+ main_app.root_url
72
+ end
73
+
74
+ def method_missing(method_name, *args)
75
+ # Swallow exceptions for identity callbacks
76
+ unless method_name =~ /after_\w+_with_\w+/
77
+ super
78
+ end
79
+ end
80
+
81
+ def set_remember(identity)
82
+ if identity_attributes = params[ActiveModel::Naming.param_key(EasyAuth.find_identity_model(self).new)]
83
+ identity.remember = identity_attributes[:remember]
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ module EasyAuth::Controllers
2
+ extend ActiveSupport::Autoload
3
+ autoload :Authenticated
4
+ autoload :Sessions
5
+ end
@@ -2,11 +2,6 @@ module EasyAuth
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace EasyAuth
4
4
 
5
- initializer 'filter_parameters' do |app|
6
- app.config.filter_parameters += [:password]
7
- app.config.filter_parameters.uniq!
8
- end
9
-
10
5
  config.generators do |g|
11
6
  g.test_framework :rspec, :view_specs => false
12
7
  end
@@ -0,0 +1,3 @@
1
+ module EasyAuth::Mailers
2
+ extend ActiveSupport::Autoload
3
+ end
@@ -0,0 +1,38 @@
1
+ module EasyAuth::Models::Account
2
+ include EasyAuth::TokenGenerator
3
+ extend EasyAuth::ReverseConcern
4
+ class NoIdentityUsernameError < StandardError; end
5
+
6
+ reverse_included do
7
+ # Relationships
8
+ has_many :identities, :class_name => 'EasyAuth::Identity', :as => :account, :dependent => :destroy
9
+
10
+ def identity_username_attribute
11
+ self.send(self.class.identity_username_attribute)
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ def identity_username_attribute
17
+ if respond_to?(:super)
18
+ super
19
+ elsif column_names.include?('username')
20
+ :username
21
+ elsif column_names.include?('email')
22
+ :email
23
+ else
24
+ raise EasyAuth::Models::Account::NoIdentityUsernameError, 'your model must have either a #username or #email attribute. Or you must override the .identity_username_attribute class method'
25
+ end
26
+ end
27
+ end
28
+
29
+ def generate_session_token!
30
+ self.update_column(:session_token, _generate_token(:session))
31
+ self.session_token
32
+ end
33
+
34
+ def set_session(session)
35
+ session[:session_token] = generate_session_token!
36
+ session[:account_class] = self.class.to_s
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module EasyAuth::Models::Identities
2
+ extend ActiveSupport::Autoload
3
+ end
@@ -0,0 +1,43 @@
1
+ module EasyAuth::Models::Identity
2
+ include EasyAuth::TokenGenerator
3
+ include EasyAuth::ReverseConcern
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ self.table_name = :identities
8
+ belongs_to :account, :polymorphic => true
9
+ extend ClassMethods
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def authenticate(controller = nil)
15
+ raise NotImplementedError
16
+ end
17
+
18
+ def new_session(controller)
19
+ controller.instance_variable_set(:@identity, self.new)
20
+ end
21
+ end
22
+
23
+ def set_account_session(session)
24
+ account.set_session(session)
25
+ end
26
+
27
+ def remember
28
+ @remember
29
+ end
30
+
31
+ def remember=(value)
32
+ @remember = ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
33
+ end
34
+
35
+ def generate_remember_token!
36
+ update_column(:remember_token, _generate_token(:remember))
37
+ remember_token
38
+ end
39
+
40
+ def remember_time
41
+ 1.year
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ module EasyAuth::Models
2
+ extend ActiveSupport::Autoload
3
+
4
+ autoload :Account
5
+ autoload :Identities
6
+ autoload :Identity
7
+ end
@@ -0,0 +1,27 @@
1
+ module EasyAuth::ReverseConcern
2
+ def self.extended(base)
3
+ base.instance_variable_set("@_dependencies", [])
4
+ end
5
+
6
+ def append_features(base)
7
+ if base.instance_variable_defined?("@_dependencies")
8
+ base.instance_variable_get("@_dependencies") << self
9
+ return false
10
+ else
11
+ return false if base < self
12
+ base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
13
+ base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
14
+ @_dependencies.each { |dep| base.send(:include, dep) }
15
+ super
16
+ end
17
+ end
18
+
19
+ def reverse_included(base = nil, &block)
20
+ if base.nil?
21
+ @_included_block = block
22
+ else
23
+ super
24
+ end
25
+ end
26
+ end
27
+
@@ -1,21 +1,8 @@
1
- module ActionDispatch::Routing
2
- class Mapper
3
- def easy_auth_routes
4
- easy_auth_session_routes
5
- easy_auth_password_reset_routes
6
- end
7
-
8
- def easy_auth_session_routes
9
- get '/sign_out' => 'sessions#destroy', :as => :sign_out
10
- get '/sign_in' => 'sessions#new', :as => :sign_in
11
- post '/sign_in' => 'sessions#create'
12
- end
13
-
14
- def easy_auth_password_reset_routes
15
- get '/password_reset' => 'password_reset#new', :as => :password_reset
16
- post '/password_reset' => 'password_reset#create'
17
- get '/password_reset/:reset_token' => 'password_reset#edit', :as => :edit_password
18
- put '/password_reset/:reset_token' => 'password_reset#update'
1
+ module EasyAuth::Routes
2
+ def easy_auth_routes
3
+ get '/sign_out' => 'sessions#destroy', :as => :sign_out
4
+ methods.grep(/easy_auth_\w+_routes/).each do |routes|
5
+ send(routes)
19
6
  end
20
7
  end
21
8
  end
@@ -0,0 +1,7 @@
1
+ module EasyAuth::TokenGenerator
2
+ private
3
+
4
+ def _generate_token(type)
5
+ token = BCrypt::Password.create("#{id}-#{type}_token-#{DateTime.current}")
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyAuth
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/easy_auth.rb CHANGED
@@ -1,9 +1,51 @@
1
+ require 'active_support'
1
2
  require 'bcrypt'
2
3
  require 'easy_auth/engine'
3
- require 'easy_auth/routes'
4
+ require 'easy_auth/version'
4
5
 
5
6
  module EasyAuth
7
+ extend ActiveSupport::Autoload
8
+
9
+ autoload :Controllers
10
+ autoload :Mailers
11
+ autoload :Models
12
+ autoload :ReverseConcern
13
+ autoload :Routes
14
+ autoload :TokenGenerator
15
+
6
16
  def self.identity_model
7
- Identity
17
+ EasyAuth::Identity
18
+ end
19
+
20
+ def self.account_model
21
+ User
22
+ end
23
+
24
+ def self.authenticate(controller)
25
+ if identity_model = find_identity_model(controller)
26
+ identity_model.authenticate(controller)
27
+ end
28
+ end
29
+
30
+ def self.new_session(controller)
31
+ identity_model = find_identity_model(controller)
32
+ identity_model.new_session(controller)
33
+ end
34
+
35
+ def self.config(&block)
36
+ yield self
37
+ end
38
+
39
+ private
40
+
41
+ def self.find_identity_model(controller)
42
+ method_name = "#{controller.params[:identity]}_identity_model"
43
+ if respond_to?(method_name)
44
+ send(method_name, controller)
45
+ else
46
+ controller.params[:identity].to_s.camelcase.constantize
47
+ end
8
48
  end
9
49
  end
50
+
51
+ ActionDispatch::Routing::Mapper.send(:include, EasyAuth::Routes)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-28 00:00:00.000000000 Z
13
+ date: 2012-11-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -93,7 +93,7 @@ dependencies:
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  - !ruby/object:Gem::Dependency
96
- name: capybara-email
96
+ name: database_cleaner
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  none: false
99
99
  requirements:
@@ -129,35 +129,35 @@ dependencies:
129
129
  requirement: !ruby/object:Gem::Requirement
130
130
  none: false
131
131
  requirements:
132
- - - ! '>='
132
+ - - ~>
133
133
  - !ruby/object:Gem::Version
134
- version: '0'
134
+ version: 1.7.0
135
135
  type: :development
136
136
  prerelease: false
137
137
  version_requirements: !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
- - - ! '>='
140
+ - - ~>
141
141
  - !ruby/object:Gem::Version
142
- version: '0'
142
+ version: 1.7.0
143
143
  - !ruby/object:Gem::Dependency
144
- name: bourne
144
+ name: factory_girl
145
145
  requirement: !ruby/object:Gem::Requirement
146
146
  none: false
147
147
  requirements:
148
- - - ! '>='
148
+ - - ~>
149
149
  - !ruby/object:Gem::Version
150
- version: '0'
150
+ version: 2.6.0
151
151
  type: :development
152
152
  prerelease: false
153
153
  version_requirements: !ruby/object:Gem::Requirement
154
154
  none: false
155
155
  requirements:
156
- - - ! '>='
156
+ - - ~>
157
157
  - !ruby/object:Gem::Version
158
- version: '0'
158
+ version: 2.6.0
159
159
  - !ruby/object:Gem::Dependency
160
- name: launchy
160
+ name: bourne
161
161
  requirement: !ruby/object:Gem::Requirement
162
162
  none: false
163
163
  requirements:
@@ -173,7 +173,7 @@ dependencies:
173
173
  - !ruby/object:Gem::Version
174
174
  version: '0'
175
175
  - !ruby/object:Gem::Dependency
176
- name: debugger
176
+ name: launchy
177
177
  requirement: !ruby/object:Gem::Requirement
178
178
  none: false
179
179
  requirements:
@@ -198,32 +198,27 @@ extensions: []
198
198
  extra_rdoc_files: []
199
199
  files:
200
200
  - app/controllers/authenticated_controller.rb
201
- - app/controllers/password_reset_controller.rb
202
201
  - app/controllers/sessions_controller.rb
203
- - app/mailers/password_reset_mailer.rb
204
- - app/mixins/easy_auth/controllers/authenticated.rb
205
- - app/mixins/easy_auth/controllers/password_reset.rb
206
- - app/mixins/easy_auth/controllers/sessions.rb
207
- - app/mixins/easy_auth/helpers.rb
208
- - app/mixins/easy_auth/mailers/password_reset.rb
209
- - app/mixins/easy_auth/models/account.rb
210
- - app/mixins/easy_auth/models/identity.rb
211
- - app/models/identity.rb
212
- - app/views/layouts/easy_auth/application.html.erb
213
- - app/views/password_reset/edit.html.erb
214
- - app/views/password_reset/new.html.erb
215
- - app/views/password_reset_mailer/reset.html.erb
216
- - app/views/password_reset_mailer/reset.text.erb
217
- - app/views/sessions/new.html.erb
202
+ - app/helpers/easy_auth_helper.rb
203
+ - app/models/easy_auth/identity.rb
218
204
  - config/locales/en.yml
219
205
  - config/routes.rb
220
- - db/migrate/20120227014023_create_identities.rb
206
+ - db/migrate/20120227014023_create_easy_auth_identities.rb
207
+ - lib/easy_auth/controllers/authenticated.rb
208
+ - lib/easy_auth/controllers/sessions.rb
209
+ - lib/easy_auth/controllers.rb
221
210
  - lib/easy_auth/engine.rb
211
+ - lib/easy_auth/mailers.rb
212
+ - lib/easy_auth/models/account.rb
213
+ - lib/easy_auth/models/identities.rb
214
+ - lib/easy_auth/models/identity.rb
215
+ - lib/easy_auth/models.rb
216
+ - lib/easy_auth/reverse_concern.rb
222
217
  - lib/easy_auth/routes.rb
218
+ - lib/easy_auth/token_generator.rb
223
219
  - lib/easy_auth/version.rb
224
220
  - lib/easy_auth.rb
225
221
  - lib/tasks/easy_auth_tasks.rake
226
- - MIT-LICENSE
227
222
  - Rakefile
228
223
  - README.md
229
224
  homepage: https://github.com/dockyard/easy_auth
@@ -240,7 +235,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
240
235
  version: '0'
241
236
  segments:
242
237
  - 0
243
- hash: 409473676038941757
238
+ hash: -3015113123042150927
244
239
  required_rubygems_version: !ruby/object:Gem::Requirement
245
240
  none: false
246
241
  requirements:
@@ -249,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
244
  version: '0'
250
245
  segments:
251
246
  - 0
252
- hash: 409473676038941757
247
+ hash: -3015113123042150927
253
248
  requirements: []
254
249
  rubyforge_project:
255
250
  rubygems_version: 1.8.23
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright 2012 YOURNAME
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,3 +0,0 @@
1
- class PasswordResetController < ApplicationController
2
- include EasyAuth::Controllers::PasswordReset
3
- end
@@ -1,4 +0,0 @@
1
- class PasswordResetMailer < ActionMailer::Base
2
- include EasyAuth::Mailers::PasswordReset
3
- default :from => 'from@example.com'
4
- end
@@ -1,14 +0,0 @@
1
- module EasyAuth::Controllers::Authenticated
2
- def self.included(base)
3
- base.before_filter :attempt_to_authenticate
4
- end
5
-
6
- private
7
-
8
- def attempt_to_authenticate
9
- if user_not_signed_in?
10
- session[:requested_path] = request.path
11
- redirect_to main_app.sign_in_url
12
- end
13
- end
14
- end
@@ -1,56 +0,0 @@
1
- module EasyAuth::Controllers::PasswordReset
2
- def self.included(base)
3
- base.instance_eval do
4
- before_filter :find_identity_from_reset_token, :only => [:edit, :update]
5
- end
6
- end
7
-
8
- def new
9
- @identity = EasyAuth.identity_model.new
10
- end
11
-
12
- def create
13
- if @identity = EasyAuth.identity_model.where(:username => params[:identity][:username]).first
14
- @identity.generate_reset_token!
15
- PasswordResetMailer.reset(@identity.id).deliver
16
- else
17
- @identity = EasyAuth.identity_model.new(params[:identity])
18
- end
19
-
20
- flash.now[:notice] = I18n.t('easy_auth.password_reset.create.notice')
21
- render :new
22
- end
23
-
24
- def update
25
- if @identity.update_attributes(scope_to_password_params(:identity))
26
- after_successful_password_reset(@identity)
27
- else
28
- after_failed_sign_in(@identity)
29
- end
30
- end
31
-
32
- private
33
-
34
- def scope_to_password_params(key)
35
- params[key].select { |k, v| ['password', 'password_confirmation'].include?(k) }
36
- end
37
-
38
- def find_identity_from_reset_token
39
- @identity = EasyAuth.identity_model.where(:reset_token => params[:reset_token].to_s).first
40
- end
41
-
42
- def after_successful_password_reset(identity)
43
- identity.set_account_session(session)
44
- identity.update_attribute(:reset_token, nil)
45
- redirect_to after_successful_password_reset_url(identity), :notice => I18n.t('easy_auth.password_reset.update.notice')
46
- end
47
-
48
- def after_successful_password_reset_url(identity)
49
- identity.account
50
- end
51
-
52
- def after_failed_password_reset(identity)
53
- flash.now[:error] = I18n.t('easy_auth.password_reset.update.error')
54
- render :new
55
- end
56
- end
@@ -1,43 +0,0 @@
1
- module EasyAuth::Controllers::Sessions
2
- def new
3
- @identity = EasyAuth.identity_model.new
4
- end
5
-
6
- def create
7
- if identity = EasyAuth.identity_model.authenticate(params[:identity])
8
- identity.set_account_session(session)
9
- if identity.remember
10
- cookies[:remember_token] = { :value => identity.generate_remember_token!, :expires => identity.remember_time.from_now }
11
- end
12
- after_successful_sign_in(identity)
13
- else
14
- @identity = EasyAuth.identity_model.new(params[:identity])
15
- after_failed_sign_in(@identity)
16
- end
17
- end
18
-
19
- def destroy
20
- session.delete(:session_token)
21
- cookies.delete(:remember_token)
22
- after_sign_out
23
- end
24
-
25
- private
26
-
27
- def after_successful_sign_in(identity)
28
- redirect_to session.delete(:requested_path) || after_successful_sign_in_url(identity), :notice => I18n.t('easy_auth.sessions.create.notice')
29
- end
30
-
31
- def after_successful_sign_in_url(identity)
32
- identity.account
33
- end
34
-
35
- def after_failed_sign_in(identity)
36
- flash.now[:error] = I18n.t('easy_auth.sessions.create.error')
37
- render :new
38
- end
39
-
40
- def after_sign_out
41
- redirect_to main_app.root_url, :notice => I18n.t('easy_auth.sessions.delete.notice')
42
- end
43
- end
@@ -1,44 +0,0 @@
1
- module EasyAuth
2
- module Helpers
3
- def self.included(base)
4
- base.class_eval do
5
- helper_method :current_account, :current_user, :account_signed_in?, :user_signed_in?, :account_not_signed_in?, :user_not_signed_in?
6
- end
7
- end
8
-
9
- def current_account
10
- if session[:session_token] && session[:account_class]
11
- begin
12
- @current_account ||= session[:account_class].constantize.find_by_session_token(session[:session_token])
13
- rescue
14
- @current_account = nil
15
- session.delete(:session_token)
16
- end
17
- elsif cookies[:remember_token]
18
- begin
19
- @current_account ||= EasyAuth.identity_model.find_by_remember_token(cookies[:remember_token]).account
20
- rescue
21
- @current_acount = nil
22
- cookies.delete(:remember_token)
23
- end
24
- else
25
- session.delete(:session_token)
26
- cookies.delete(:remember_token)
27
- end
28
-
29
- @current_account
30
- end
31
- alias :current_user :current_account
32
-
33
- def account_signed_in?
34
- current_account
35
- end
36
- alias :user_signed_in? :account_signed_in?
37
-
38
- def account_not_signed_in?
39
- !account_signed_in?
40
- end
41
- alias :user_not_signed_in? :account_not_signed_in?
42
-
43
- end
44
- end
@@ -1,11 +0,0 @@
1
- module EasyAuth::Mailers::PasswordReset
2
- def self.included(base)
3
- base.clear_action_methods!
4
- end
5
-
6
- def reset(id)
7
- @identity = EasyAuth.identity_model.find(id)
8
- @url = edit_password_url(@identity.reset_token)
9
- mail :to => @identity.account.email, :subject => 'Password reset'
10
- end
11
- end
@@ -1,56 +0,0 @@
1
- module EasyAuth::Models::Account
2
- class NoIdentityUsernameError < StandardError; end
3
- def self.included(base)
4
- base.class_eval do
5
- unless respond_to?(:identity_username_attribute)
6
- def self.identity_username_attribute
7
- if column_names.include?('username')
8
- :username
9
- elsif column_names.include?('email')
10
- :email
11
- else
12
- raise EasyAuth::Models::Account::NoIdentityUsernameError, 'your model must have either a #username or #email attribute. Or you must override the .identity_username_attribute class method'
13
- end
14
- end
15
- end
16
-
17
- def identity_username_attribute
18
- self.send(self.class.identity_username_attribute)
19
- end
20
-
21
- has_one :identity, :as => :account
22
- before_create :setup_identity, :unless => :skip_identity_validations
23
- before_update :update_identity, :unless => :skip_identity_validations
24
-
25
- attr_accessor :password, :skip_identity_validations
26
- validates :password, :presence => { :on => :create, :unless => :skip_identity_validations }, :confirmation => true
27
- attr_accessible :password, :password_confirmation, :skip_identity_validations
28
- validates identity_username_attribute, :presence => true, :unless => :skip_identity_validations
29
- end
30
- end
31
-
32
- def generate_session_token!
33
- token = BCrypt::Password.create("#{id}-session_token-#{DateTime.current}")
34
- self.update_attribute(:session_token, token)
35
- self.session_token
36
- end
37
-
38
- def set_session(session)
39
- session[:session_token] = generate_session_token!
40
- session[:account_class] = self.class.to_s
41
- end
42
-
43
- private
44
-
45
- def setup_identity
46
- build_identity(identity_attributes)
47
- end
48
-
49
- def update_identity
50
- identity.update_attributes(identity_attributes)
51
- end
52
-
53
- def identity_attributes
54
- { :username => self.identity_username_attribute, :password => self.password, :password_confirmation => self.password_confirmation }
55
- end
56
- end
@@ -1,57 +0,0 @@
1
- module EasyAuth::Models::Identity
2
- def self.included(base)
3
- base.class_eval do
4
- belongs_to :account, :polymorphic => true
5
- has_secure_password
6
- attr_accessible :username, :password, :password_confirmation, :remember
7
- validates :username, :uniqueness => true, :presence => true
8
- validates :password, :presence => { :on => :create }
9
- extend ClassMethods
10
- end
11
- end
12
-
13
- module ClassMethods
14
- def authenticate(attributes = nil)
15
- return nil if attributes.nil?
16
-
17
- if identity = where(arel_table[:username].matches(attributes[:username].try(&:strip))).first.try(:authenticate, attributes[:password])
18
- identity.remember = attributes[:remember]
19
- identity
20
- else
21
- nil
22
- end
23
- end
24
- end
25
-
26
- def set_account_session(session)
27
- account.set_session(session)
28
- end
29
-
30
- def remember
31
- @remember
32
- end
33
-
34
- def remember=(value)
35
- @remember = ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
36
- end
37
-
38
- def generate_reset_token!
39
- update_attribute(:reset_token, URI.escape(_generate_token(:reset).gsub(/[\.|\\\/]/,'')))
40
- self.reset_token
41
- end
42
-
43
- def generate_remember_token!
44
- self.update_attribute(:remember_token, _generate_token(:remember))
45
- self.remember_token
46
- end
47
-
48
- def remember_time
49
- 1.year
50
- end
51
-
52
- private
53
-
54
- def _generate_token(type)
55
- token = BCrypt::Password.create("#{id}-#{type}_token-#{DateTime.current}")
56
- end
57
- end
@@ -1,3 +0,0 @@
1
- class Identity < ActiveRecord::Base
2
- include EasyAuth::Models::Identity
3
- end
@@ -1,14 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>EasyAuth</title>
5
- <%= stylesheet_link_tag "easy_auth/application", :media => "all" %>
6
- <%= javascript_include_tag "easy_auth/application" %>
7
- <%= csrf_meta_tags %>
8
- </head>
9
- <body>
10
-
11
- <%= yield %>
12
-
13
- </body>
14
- </html>
@@ -1,13 +0,0 @@
1
- <%= form_for @identity, :url => main_app.edit_password_path(params[:reset_token]) do |f| %>
2
- <p>
3
- <%= f.label :password %>
4
- <%= f.text_field :password %>
5
- </p>
6
- <p>
7
- <%= f.label :password_confirmation %>
8
- <%= f.text_field :password_confirmation %>
9
- </p>
10
- <p>
11
- <%= f.submit 'Submit' %>
12
- </p>
13
- <% end %>
@@ -1,9 +0,0 @@
1
- <%= form_for @identity, :url => main_app.password_reset_path do |f| %>
2
- <p>
3
- <%= f.label :username %>
4
- <%= f.text_field :username %>
5
- </p>
6
- <p>
7
- <%= f.submit 'Submit' %>
8
- </p>
9
- <% end %>
@@ -1 +0,0 @@
1
- <%= link_to 'Reset password', @url %>
@@ -1 +0,0 @@
1
- Reset password: <%= @url %>
@@ -1,17 +0,0 @@
1
- <%= form_for @identity, :url => main_app.sign_in_path do |f| %>
2
- <p>
3
- <%= f.label :username %>
4
- <%= f.text_field :username %>
5
- </p>
6
- <p>
7
- <%= f.label :password %>
8
- <%= f.password_field :password %>
9
- </p>
10
- <p>
11
- <%= f.label :remember, "Remember for #{@identity.remember_time.inspect}" %>
12
- <%= f.check_box :remember %>
13
- </p>
14
- <p>
15
- <%= f.submit 'Submit' %>
16
- </p>
17
- <% end %>