mirror of
https://github.com/heartcombo/devise.git
synced 2026-01-11 08:37:56 -05:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dea0f68291 | ||
|
|
5fc2c2e6f5 | ||
|
|
3bba7edc4f | ||
|
|
36906a03f3 | ||
|
|
d6af3d7dc6 | ||
|
|
767331657b | ||
|
|
55860675c2 | ||
|
|
da3af3f341 | ||
|
|
0f46ed3a33 | ||
|
|
8d33e8f313 | ||
|
|
fb1e9bc8a7 | ||
|
|
4310ad798c | ||
|
|
8def06e1a6 | ||
|
|
3b0aaaaae8 | ||
|
|
cb83c66139 | ||
|
|
2104397bee | ||
|
|
374948cf4b | ||
|
|
7c51ec0742 | ||
|
|
ed05225dd5 | ||
|
|
c32cb3da6c | ||
|
|
70c32e48fc | ||
|
|
522219e5db | ||
|
|
a843b74c86 | ||
|
|
56834284bd | ||
|
|
5b762ff85a | ||
|
|
73822fe109 | ||
|
|
85a4aa2afa | ||
|
|
c49fe8c6d7 | ||
|
|
a59e20e3bb | ||
|
|
5ef88a8fe6 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,4 +7,3 @@ coverage/*
|
||||
rdoc/*
|
||||
pkg
|
||||
log
|
||||
test/tmp/*
|
||||
@@ -1,40 +1,10 @@
|
||||
== 1.2.rc
|
||||
|
||||
* deprecations
|
||||
* cookie_domain is deprecated in favor of cookie_options
|
||||
* after_update_path_for can no longer be defined in ApplicationController
|
||||
|
||||
* enhancements
|
||||
* Added OmniAuth support
|
||||
* Added ORM adapter to abstract ORM iteraction
|
||||
* sign_out_via is available in the router to configure the method used for sign out (by github.com/martinrehfeld)
|
||||
* Improved Ajax requests handling in failure app (by github.com/spastorino)
|
||||
* Added request_keys to easily use request specific values (like subdomain) in authentication
|
||||
* Increased the size of friendly_token to 60 characters (reduces the chances of a successful brute attack)
|
||||
* Ensure the friendly token does not include "_" or "-" since some e-mails may not autolink it properly (by github.com/rymai)
|
||||
* Extracted encryptors into :encryptable for better bcrypt support
|
||||
* :rememberable is now able to use salt as token if no remember_token is provided
|
||||
* Store the salt in session and expire the session if the user changes his password
|
||||
* Allow :stateless_token to be set to true avoiding users to be stored in session through token authentication
|
||||
* cookie_options uses session_options values by default
|
||||
* Sign up now check if the user is active or not and redirect him accordingly setting the inactive_signed_up message
|
||||
* Use ActiveModel#to_key instead of #id
|
||||
* sign_out_all_scopes now destroys the whole session
|
||||
* Added case_insensitive_keys that automatically downcases the given keys, by default downcases only e-mail (by github.com/adahl)
|
||||
|
||||
* default behavior changes
|
||||
* sign_out_all_scopes defaults to true as security measure
|
||||
* http authenticatable is disabled by default
|
||||
* Devise does not intercept 401 returned from applications
|
||||
== 1.1.5
|
||||
|
||||
* bugfix
|
||||
* after_sign_in_path_for always receives a resource
|
||||
* Do not execute Warden::Callbacks on Devise::TestHelpers (by github.com/sgronblo)
|
||||
* Password recovery and account unlocking takes into account authentication keys (by github.com/RStankov)
|
||||
* FailureApp now properly handles nil request.format
|
||||
* Fix a bug causing FailureApp to return with HTTP Auth Headers for IE7
|
||||
* Ensure namespaces has proper scoped views
|
||||
* Ensure Devise does not set empty flash messages (by github.com/sxross)
|
||||
* Ensure to convert keys on indifferent hash
|
||||
|
||||
* defaults
|
||||
* Set config.http_authenticatable to false to avoid confusion
|
||||
|
||||
== 1.1.4
|
||||
|
||||
|
||||
2
Gemfile
2
Gemfile
@@ -3,8 +3,6 @@ source "http://rubygems.org"
|
||||
gemspec
|
||||
|
||||
gem "rails", "~> 3.0.0"
|
||||
gem "oa-oauth", :require => "omniauth/oauth"
|
||||
gem "oa-openid", :require => "omniauth/openid"
|
||||
|
||||
group :test do
|
||||
gem "webrat", "0.7.2", :require => false
|
||||
|
||||
47
Gemfile.lock
47
Gemfile.lock
@@ -1,9 +1,8 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
devise (1.2.rc)
|
||||
devise (1.1.4)
|
||||
bcrypt-ruby (~> 2.1.2)
|
||||
orm_adapter (~> 0.0.2)
|
||||
warden (~> 1.0.2)
|
||||
|
||||
GEM
|
||||
@@ -32,15 +31,10 @@ GEM
|
||||
activesupport (= 3.0.3)
|
||||
arel (~> 2.0.2)
|
||||
tzinfo (~> 0.3.23)
|
||||
activerecord-jdbc-adapter (1.0.2-java)
|
||||
activerecord-jdbcsqlite3-adapter (1.0.2-java)
|
||||
activerecord-jdbc-adapter (= 1.0.2)
|
||||
jdbc-sqlite3 (~> 3.6.0)
|
||||
activeresource (3.0.3)
|
||||
activemodel (= 3.0.3)
|
||||
activesupport (= 3.0.3)
|
||||
activesupport (3.0.3)
|
||||
addressable (2.2.2)
|
||||
arel (2.0.4)
|
||||
bcrypt-ruby (2.1.2)
|
||||
bson (1.1.2)
|
||||
@@ -49,12 +43,7 @@ GEM
|
||||
columnize (0.3.2)
|
||||
erubis (2.6.6)
|
||||
abstract (>= 1.0.0)
|
||||
faraday (0.5.2)
|
||||
addressable (~> 2.2.2)
|
||||
multipart-post (~> 1.0.1)
|
||||
rack (>= 1.1.0, < 2)
|
||||
i18n (0.4.2)
|
||||
jdbc-sqlite3 (3.6.14.2.056-java)
|
||||
linecache (0.43)
|
||||
mail (2.2.10)
|
||||
activesupport (>= 2.3.6)
|
||||
@@ -71,35 +60,11 @@ GEM
|
||||
mongo (~> 1.1)
|
||||
tzinfo (~> 0.3.22)
|
||||
will_paginate (~> 3.0.pre)
|
||||
multi_json (0.0.5)
|
||||
multipart-post (1.0.1)
|
||||
nokogiri (1.4.3.1)
|
||||
nokogiri (1.4.3.1-java)
|
||||
weakling (>= 0.0.3)
|
||||
oa-core (0.1.6)
|
||||
rack (~> 1.1)
|
||||
oa-oauth (0.1.6)
|
||||
multi_json (~> 0.0.2)
|
||||
nokogiri (~> 1.4.2)
|
||||
oa-core (= 0.1.6)
|
||||
oauth (~> 0.4.0)
|
||||
oauth2 (~> 0.1.0)
|
||||
oa-openid (0.1.6)
|
||||
oa-core (= 0.1.6)
|
||||
rack-openid (~> 1.2.0)
|
||||
ruby-openid-apps-discovery
|
||||
oauth (0.4.4)
|
||||
oauth2 (0.1.0)
|
||||
faraday (~> 0.5.0)
|
||||
multi_json (~> 0.0.4)
|
||||
orm_adapter (0.0.3)
|
||||
nokogiri (1.4.4)
|
||||
polyglot (0.3.1)
|
||||
rack (1.2.1)
|
||||
rack-mount (0.6.13)
|
||||
rack (>= 1.0.0)
|
||||
rack-openid (1.2.0)
|
||||
rack (>= 1.1.0)
|
||||
ruby-openid (>= 2.1.8)
|
||||
rack-test (0.5.6)
|
||||
rack (>= 1.0)
|
||||
rails (3.0.3)
|
||||
@@ -121,9 +86,6 @@ GEM
|
||||
ruby-debug-base (~> 0.10.4.0)
|
||||
ruby-debug-base (0.10.4)
|
||||
linecache (>= 0.3)
|
||||
ruby-openid (2.1.8)
|
||||
ruby-openid-apps-discovery (1.2.0)
|
||||
ruby-openid (>= 2.1.7)
|
||||
sqlite3-ruby (1.3.2)
|
||||
thor (0.14.6)
|
||||
treetop (1.4.9)
|
||||
@@ -131,7 +93,6 @@ GEM
|
||||
tzinfo (0.3.23)
|
||||
warden (1.0.2)
|
||||
rack (>= 1.0.0)
|
||||
weakling (0.0.4-java)
|
||||
webrat (0.7.2)
|
||||
nokogiri (>= 1.2.0)
|
||||
rack (>= 1.0)
|
||||
@@ -139,7 +100,6 @@ GEM
|
||||
will_paginate (3.0.pre2)
|
||||
|
||||
PLATFORMS
|
||||
java
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
@@ -150,9 +110,6 @@ DEPENDENCIES
|
||||
mocha
|
||||
mongo (= 1.1.2)
|
||||
mongoid (= 2.0.0.beta.20)
|
||||
oa-oauth
|
||||
oa-openid
|
||||
orm_adapter (~> 0.0.2)
|
||||
rails (~> 3.0.0)
|
||||
ruby-debug (>= 0.10.3)
|
||||
sqlite3-ruby
|
||||
|
||||
252
README.rdoc
252
README.rdoc
@@ -7,11 +7,10 @@ Devise is a flexible authentication solution for Rails based on Warden. It:
|
||||
* Allows you to have multiple roles (or models/scopes) signed in at the same time;
|
||||
* Is based on a modularity concept: use just what you really need.
|
||||
|
||||
It's composed of 12 modules:
|
||||
Right now it's composed of 11 modules:
|
||||
|
||||
* Database Authenticatable: encrypts and stores a password in the database to validate the authenticity of an user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.
|
||||
* Token Authenticatable: signs in a user based on an authentication token (also known as "single access token"). The token can be given both through query string or HTTP Basic Authentication.
|
||||
* Omniauthable: adds Omniauth (github.com/intridea/omniauth) support;
|
||||
* Token Authenticatable: signs in an user based on an authentication token (also known as "single access token"). The token can be given both through query string or HTTP Basic Authentication.
|
||||
* Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
|
||||
* Recoverable: resets the user password and sends reset instructions.
|
||||
* Registerable: handles signing up users through a registration process, also allowing them to edit and destroy their account.
|
||||
@@ -20,61 +19,14 @@ It's composed of 12 modules:
|
||||
* Timeoutable: expires sessions that have no activity in a specified period of time.
|
||||
* Validatable: provides validations of email and password. It's optional and can be customized, so you're able to define your own validations.
|
||||
* Lockable: locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period.
|
||||
* Encryptable: allows support of other authentication mechanisms besides Bcrypt (the default).
|
||||
|
||||
== Information
|
||||
|
||||
=== The Devise Wiki
|
||||
|
||||
The Devise Wiki has lots of additional information about Devise including many "how-to" articles and answers to the most frequently asked questions. Please browse the Wiki after finishing this README:
|
||||
|
||||
http://wiki.github.com/plataformatec/devise
|
||||
|
||||
=== Bug reports
|
||||
|
||||
If you discover a problem with Devise, we would like to know about it. However, we ask that you please review these guidelines before submitting a bug report:
|
||||
|
||||
http://github.com/plataformatec/devise/wiki/Bug-reports
|
||||
|
||||
If you found a security bug, do *NOT* use the GitHub Issue tracker. Send private GitHub message or email to the maintainers listed in the bottom of the README.
|
||||
|
||||
=== Google Group
|
||||
|
||||
If you have any questions, comments, or concerns please use the Google Group instead of the GitHub Issues tracker:
|
||||
|
||||
http://groups.google.com/group/plataformatec-devise
|
||||
|
||||
=== RDocs
|
||||
|
||||
You can view the Devise documentation in RDoc format here:
|
||||
|
||||
http://rubydoc.info/github/plataformatec/devise/master/frames
|
||||
|
||||
If you need to use Devise with Rails 2.3, you can always run `gem server` from the command line after you install the gem to access the old documentation.
|
||||
|
||||
=== Example Applications
|
||||
|
||||
There are a few example applications available on GitHub that demonstrate various features of Devise with different versions of Rails. You can view them here:
|
||||
|
||||
http://github.com/plataformatec/devise/wiki/Example-Applications
|
||||
|
||||
=== Extensions
|
||||
|
||||
Our community has created a number of extensions that add functionality above and beyond what is included with Devise. You can view a list of available extensions and add your own here:
|
||||
|
||||
http://github.com/plataformatec/devise/wiki/Extensions
|
||||
|
||||
=== Contributing
|
||||
|
||||
We hope that you will consider contributing to Devise. Please read this short overview for some information about how to get started:
|
||||
|
||||
http://github.com/plataformatec/devise/wiki/Contributing
|
||||
|
||||
== Installation
|
||||
|
||||
You can use the latest Rails 3 gem with the latest Devise gem:
|
||||
Devise 1.1 supports Rails 3 and is NOT backward compatible. You can use the latest Rails 3 beta gem with Devise latest gem:
|
||||
|
||||
gem install devise
|
||||
gem install devise --version=1.1.3
|
||||
|
||||
If you want to use Rails master (from git repository) you need to use Devise from git repository and vice-versa.
|
||||
|
||||
After you install Devise and add it to your Gemfile, you need to run the generator:
|
||||
|
||||
@@ -86,9 +38,39 @@ The generator will install an initializer which describes ALL Devise's configura
|
||||
|
||||
Replace MODEL by the class name you want to add devise, like User, Admin, etc. This will create a model (if one does not exist) and configure it with default Devise modules. The generator will also create a migration file (if your ORM support them) and configure your routes. Continue reading this file to understand exactly what the generator produces and how to use it.
|
||||
|
||||
Support for Rails 2.3.x can be found by installing Devise 1.0.x from the v1.0 branch.
|
||||
== Rails 2.3
|
||||
|
||||
== Getting started
|
||||
If you want to use the Rails 2.3.x version, you should do:
|
||||
|
||||
gem install devise --version=1.0.8
|
||||
|
||||
And please check the README at the v1.0 branch since this one is based on Rails 3:
|
||||
|
||||
http://github.com/plataformatec/devise/tree/v1.0
|
||||
|
||||
== Ecosystem
|
||||
|
||||
Devise ecosystem is growing solid day after day. If you just need a walkthrough about setting up Devise, this README will work great. But if you need more documentation and resources, please check both the wiki and rdoc:
|
||||
|
||||
* http://rdoc.info/projects/plataformatec/devise
|
||||
* http://wiki.github.com/plataformatec/devise
|
||||
|
||||
Both links above are for Devise with Rails 3. If you need to use Devise with Rails 2.3, you can always run `gem server` from the command line after you install the gem to access the old documentation.
|
||||
|
||||
Another great way to learn Devise are Ryan Bates' screencasts:
|
||||
|
||||
* http://railscasts.com/episodes/209-introducing-devise
|
||||
* http://railscasts.com/episodes/210-customizing-devise
|
||||
|
||||
And a few example applications:
|
||||
|
||||
* Rails 2.3 app using Devise at http://github.com/plataformatec/devise_example
|
||||
* Rails 2.3 app using Devise with subdomains at http://github.com/fortuity/subdomain-authentication
|
||||
* Rails 3.0 app with Mongoid at http://github.com/fortuity/rails3-mongoid-devise
|
||||
|
||||
Finally, Devise also has several extensions built by the community. Don't forget to check them at the end of this README. If you want to write an extension on your own, you should also check Warden (http://github.com/hassox/warden), a Rack Authentication Framework which Devise depends on.
|
||||
|
||||
== Basic Usage
|
||||
|
||||
This is a walkthrough with all steps you need to setup a devise resource, including model, migration, route files, and optional configuration.
|
||||
|
||||
@@ -117,11 +99,17 @@ Configure your routes after setting up your model. Open your config/routes.rb fi
|
||||
|
||||
devise_for :users
|
||||
|
||||
This will use your User model to create a set of needed routes (you can see them by running `rake routes`). If you invoked the devise generator, you noticed that this is exactly what the generator produces for us: model, routes and migrations.
|
||||
This will use your User model to create a set of needed routes (you can see them by running `rake routes`).
|
||||
|
||||
Don't forget to run rake db:migrate and you are ready to go! But don't stop reading here, we still have a lot to tell you.
|
||||
Options for configuring your routes include :class_name (to set the class for that route), :path_prefix, :path and :path_names, where the last two have the same meaning as in common routes. The available :path_names are:
|
||||
|
||||
=== Controller filters and helpers
|
||||
devise_for :users, :path => "usuarios", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
|
||||
|
||||
Be sure to check devise_for documentation for details.
|
||||
|
||||
This exactly what the devise generator produces for you: model, routes and migrations. Don't forget to run rake db:migrate and you are ready to go! But don't stop reading here, we still have a lot to tell you.
|
||||
|
||||
== Controller filters and helpers
|
||||
|
||||
Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this before_filter:
|
||||
|
||||
@@ -149,25 +137,7 @@ Finally, you need to set up default url options for the mailer in each environme
|
||||
|
||||
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
|
||||
|
||||
Notice that if your devise model is not called "user" but "member", then the helpers you should use are:
|
||||
|
||||
before_filter :authenticate_member!
|
||||
|
||||
member_signed_in?
|
||||
|
||||
current_member
|
||||
|
||||
member_session
|
||||
|
||||
=== Configuring Models
|
||||
|
||||
The devise method in your models also accepts some options to configure its modules. For example, you can choose which encryptor to use in database_authenticatable:
|
||||
|
||||
devise :database_authenticatable, :confirmable, :recoverable, :stretches => 20
|
||||
|
||||
Besides :stretches, you can define :pepper, :encryptor, :confirm_within, :remember_for, :timeout_in, :unlock_in and other values. For details, see the initializer file that was created when you invoked the "devise:install" generator described above.
|
||||
|
||||
=== Configuring multiple models
|
||||
== Tidying up
|
||||
|
||||
Devise allows you to set up as many roles as you want. For example, you may have a User model and also want an Admin model with just authentication, trackable, lockable and timeoutable features and no confirmation or password-recovery features. Just follow these steps:
|
||||
|
||||
@@ -193,7 +163,15 @@ Devise allows you to set up as many roles as you want. For example, you may have
|
||||
current_admin
|
||||
admin_session
|
||||
|
||||
=== Configuring views
|
||||
== Model configuration
|
||||
|
||||
The devise method in your models also accepts some options to configure its modules. For example, you can choose which encryptor to use in database_authenticatable:
|
||||
|
||||
devise :database_authenticatable, :confirmable, :recoverable, :encryptor => :bcrypt
|
||||
|
||||
Besides :encryptor, you can define :pepper, :stretches, :confirm_within, :remember_for, :timeout_in, :unlock_in and other values. For details, see the initializer file that was created when you invoked the "devise:install" generator described above.
|
||||
|
||||
== Configuring views
|
||||
|
||||
We built Devise to help you quickly develop an application that uses authentication. However, we don't want to be in your way when you need to customize it.
|
||||
|
||||
@@ -201,15 +179,11 @@ Since Devise is an engine, all its views are packaged inside the gem. These view
|
||||
|
||||
rails generate devise:views
|
||||
|
||||
If you are using HAML, you will need hpricot installed to convert Devise views to HAML.
|
||||
However, if you have more than one role in your application (such as "User" and "Admin"), you will notice that Devise uses the same views for all roles. Fortunately, Devise offers an easy way to customize views. All you need to do is set "config.scoped_views = true" inside "config/initializers/devise.rb".
|
||||
|
||||
If you have more than one role in your application (such as "User" and "Admin"), you will notice that Devise uses the same views for all roles. Fortunately, Devise offers an easy way to customize views. All you need to do is set "config.scoped_views = true" inside "config/initializers/devise.rb".
|
||||
After doing so, you will be able to have views based on the role like "users/sessions/new" and "admins/sessions/new". If no view is found within the scope, Devise will use the default view at "devise/sessions/new".
|
||||
|
||||
After doing so, you will be able to have views based on the role like "users/sessions/new" and "admins/sessions/new". If no view is found within the scope, Devise will use the default view at "devise/sessions/new". You can also use the generator to generate scoped views:
|
||||
|
||||
rails generate devise:views users
|
||||
|
||||
=== Configuring controllers
|
||||
== Configuring controllers
|
||||
|
||||
If the customization at the views level is not enough, you can customize each controller by following these steps:
|
||||
|
||||
@@ -220,35 +194,13 @@ If the customization at the views level is not enough, you can customize each co
|
||||
|
||||
2) Tell the router to use this controller:
|
||||
|
||||
devise_for :admins, :controllers => { :sessions => "admins/sessions" }
|
||||
devise_for :admins, :controllers => { :sessions => "admin/sessions" }
|
||||
|
||||
3) And since we changed the controller, it won't use the "devise/sessions" views, so remember to copy "devise/sessions" to "admin/sessions".
|
||||
|
||||
Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call "flash[:notice]" and "flash[:alert]" as appropriate.
|
||||
|
||||
=== Configuring routes
|
||||
|
||||
Devise also ships with default routes. If you need to customize them, you should probably be able to do it through the devise_for method. It accepts several options like :class_name, :path_prefix and so on, including the possibility to change path names for I18n:
|
||||
|
||||
devise_for :users, :path => "usuarios", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
|
||||
|
||||
Be sure to check devise_for documentation for details.
|
||||
|
||||
If you have the need for more deep customization, for instance to also allow "/sign_in" besides "/users/sign_in", all you need to do is to create your routes normally and wrap them in a +devise_scope+ block in the router:
|
||||
|
||||
devise_scope :user do
|
||||
get "sign_in", :to => "devise/sessions#new"
|
||||
end
|
||||
|
||||
This way you tell devise to use the scope :user when "/sign_in" is accessed. Notice +devise_scope+ is also aliased as +as+ and you can also give a block to +devise_for+, resulting in the same behavior:
|
||||
|
||||
devise_for :users do
|
||||
get "sign_in", :to => "devise/sessions#new"
|
||||
end
|
||||
|
||||
Feel free to choose the one you prefer!
|
||||
|
||||
=== I18n
|
||||
== I18n
|
||||
|
||||
Devise uses flash messages with I18n with the flash keys :success and :failure. To customize your app, you can set up your locale file:
|
||||
|
||||
@@ -267,22 +219,19 @@ You can also create distinct messages based on the resource you've configured us
|
||||
admin:
|
||||
signed_in: 'Hello admin!'
|
||||
|
||||
The Devise mailer uses a similar pattern to create subject messages:
|
||||
The Devise mailer uses the same pattern to create subject messages:
|
||||
|
||||
en:
|
||||
devise:
|
||||
mailer:
|
||||
confirmation_instructions:
|
||||
subject: 'Hello everybody!'
|
||||
user_subject: 'Hello User! Please confirm your email'
|
||||
reset_password_instructions:
|
||||
subject: 'Reset instructions'
|
||||
confirmation_instructions: 'Hello everybody!'
|
||||
user:
|
||||
confirmation_instructions: 'Hello User! Please confirm your email'
|
||||
reset_password_instructions: 'Reset instructions'
|
||||
|
||||
Take a look at our locale file to check all available messages. You may also be interested in one of the many translations that are available on our wiki:
|
||||
Take a look at our locale file to check all available messages.
|
||||
|
||||
http://github.com/plataformatec/devise/wiki/I18n
|
||||
|
||||
=== Test helpers
|
||||
== Test helpers
|
||||
|
||||
Devise includes some tests helpers for functional specs. To use them, you just need to include Devise::TestHelpers in your test class and use the sign_in and sign_out method. Such methods have the same signature as in controllers:
|
||||
|
||||
@@ -292,53 +241,70 @@ Devise includes some tests helpers for functional specs. To use them, you just n
|
||||
sign_out :user # sign_out(scope)
|
||||
sign_out @user # sign_out(resource)
|
||||
|
||||
You can include the Devise Test Helpers in all of your tests by adding the following to the bottom of your test/test_helper.rb file:
|
||||
You can include the Devise Test Helpers in all of your tests by adding the following to the bottom of your test/test_helper.rb or spec/spec_helper.rb file:
|
||||
|
||||
class ActionController::TestCase
|
||||
include Devise::TestHelpers
|
||||
end
|
||||
|
||||
If you're using RSpec and want the helpers automatically included within all +describe+ blocks, add a file called spec/support/devise.rb with the following contents:
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.include Devise::TestHelpers, :type => :controller
|
||||
end
|
||||
|
||||
Do not use such helpers for integration tests such as Cucumber or Webrat. Instead, fill in the form or explicitly set the user in session. For more tips, check the wiki (http://wiki.github.com/plataformatec/devise).
|
||||
|
||||
=== OAuth2
|
||||
== Migrating from other solutions
|
||||
|
||||
Devise comes with OAuth support out of the box if you're using Devise from the git repository (for now). You can read more about OAuth2 support in the wiki:
|
||||
Devise implements encryption strategies for Clearance, Authlogic and Restful-Authentication. To make use of these strategies, set the desired encryptor in the encryptor initializer config option. You might also need to rename your encrypted password and salt columns to match Devise's fields (encrypted_password and password_salt).
|
||||
|
||||
* http://github.com/plataformatec/devise/wiki/OAuth2:-Overview
|
||||
* http://github.com/plataformatec/devise/wiki/OAuth2:-Testing
|
||||
== Other ORMs
|
||||
|
||||
=== Other ORMs
|
||||
Devise supports ActiveRecord (by default) and Mongoid. We offer experimental Datamapper support (with the limitation that the Devise test suite does not run completely with Datamapper). To choose other ORM, you just need to configure it in the initializer file.
|
||||
|
||||
Devise supports ActiveRecord (default) and Mongoid. To choose other ORM, you just need to require it in the initializer file.
|
||||
== Extensions
|
||||
|
||||
=== Migrating from other solutions
|
||||
Devise also has extensions created by the community:
|
||||
|
||||
Devise implements encryption strategies for Clearance, Authlogic and Restful-Authentication. To make use of these strategies, you need set the desired encryptor in the encryptor initializer config option and add :encryptable to your model. You might also need to rename your encrypted password and salt columns to match Devise's fields (encrypted_password and password_salt).
|
||||
* http://github.com/scambra/devise_invitable adds support to Devise for sending invitations by email.
|
||||
|
||||
== Additional information
|
||||
* http://github.com/grimen/devise_facebook_connectable adds support for Facebook Connect authentication, and optionally fetching user info from Facebook in the same step.
|
||||
|
||||
=== Warden
|
||||
* http://github.com/joshk/devise_imapable adds support for imap based authentication, excellent for internal apps when an LDAP server isn't available.
|
||||
|
||||
Devise is based on Warden, which is a general Rack authentication framework created by Daniel Neighman. We encourage you to read more about Warden here:
|
||||
* http://github.com/cschiewek/devise_ldap_authenticatable adds support for LDAP authentication via simple bind.
|
||||
|
||||
http://github.com/hassox/warden
|
||||
Please consult their respective documentation for more information and requirements.
|
||||
|
||||
=== Contributors
|
||||
== TODO
|
||||
|
||||
Please refer to TODO file.
|
||||
|
||||
== Security
|
||||
|
||||
Needless to say, security is extremely important to Devise. If you find yourself in a possible security issue with Devise, please go through the following steps, trying to reproduce the bug:
|
||||
|
||||
1) Look at the source code a bit to find out whether your assumptions are correct;
|
||||
2) If possible, provide a way to reproduce the bug: a small app on Github or a step-by-step to reproduce;
|
||||
3) E-mail us or send a Github private message instead of using the normal issues;
|
||||
|
||||
Being able to reproduce the bug is the first step to fix it. Thanks for your understanding.
|
||||
|
||||
== Maintainers
|
||||
|
||||
* José Valim (http://github.com/josevalim)
|
||||
* Carlos Antônio da Silva (http://github.com/carlosantoniodasilva)
|
||||
|
||||
== Contributors
|
||||
|
||||
We have a long list of valued contributors. Check them all at:
|
||||
|
||||
http://github.com/plataformatec/devise/contributors
|
||||
|
||||
=== Maintainers
|
||||
== Bugs and Feedback
|
||||
|
||||
* José Valim (http://github.com/josevalim)
|
||||
* Carlos Antônio da Silva (http://github.com/carlosantoniodasilva)
|
||||
If you discover any bugs, please create an issue on GitHub.
|
||||
|
||||
http://github.com/plataformatec/devise/issues
|
||||
|
||||
For support, send an e-mail to the mailing list.
|
||||
|
||||
http://groups.google.com/group/plataformatec-devise
|
||||
|
||||
== License
|
||||
|
||||
|
||||
6
Rakefile
6
Rakefile
@@ -36,7 +36,6 @@ end
|
||||
begin
|
||||
require 'jeweler'
|
||||
Jeweler::Tasks.new do |s|
|
||||
root_files = FileList["README.rdoc", "MIT-LICENSE", "CHANGELOG.rdoc"]
|
||||
s.name = "devise"
|
||||
s.version = Devise::VERSION.dup
|
||||
s.summary = "Flexible authentication solution for Rails with Warden"
|
||||
@@ -44,10 +43,9 @@ begin
|
||||
s.homepage = "http://github.com/plataformatec/devise"
|
||||
s.description = "Flexible authentication solution for Rails with Warden"
|
||||
s.authors = ['José Valim', 'Carlos Antônio']
|
||||
s.files = root_files + FileList["{app,config,lib}/**/*"]
|
||||
s.extra_rdoc_files = root_files
|
||||
s.files = FileList["[A-Z]*", "{app,config,lib}/**/*"]
|
||||
s.extra_rdoc_files = FileList["[A-Z]*"] - %w(Gemfile Rakefile)
|
||||
s.add_dependency("warden", "~> 1.0.2")
|
||||
s.add_dependency("orm_adapter", "~> 0.0.2")
|
||||
s.add_dependency("bcrypt-ruby", "~> 2.1.2")
|
||||
end
|
||||
|
||||
|
||||
3
TODO
3
TODO
@@ -1,4 +1,3 @@
|
||||
* Move integration tests to Capybara
|
||||
* Better ORM integration
|
||||
* Add support to automatically refresh the access token for OAuth
|
||||
* Add test to generators using the new Rails::Generators::TestCase
|
||||
* Extract activatable models tests from confirmable
|
||||
|
||||
@@ -3,7 +3,7 @@ class Devise::ConfirmationsController < ApplicationController
|
||||
|
||||
# GET /resource/confirmation/new
|
||||
def new
|
||||
build_resource({})
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
class Devise::OmniauthCallbacksController < ApplicationController
|
||||
include Devise::Controllers::InternalHelpers
|
||||
|
||||
def failure
|
||||
set_flash_message :alert, :failure, :kind => failed_strategy.name.to_s.humanize, :reason => failure_message
|
||||
redirect_to after_omniauth_failure_path_for(resource_name)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def failed_strategy
|
||||
env["omniauth.failed_strategy"]
|
||||
end
|
||||
|
||||
def failure_message
|
||||
exception = env["omniauth.error"]
|
||||
error = exception.error_reason if exception.respond_to?(:error_reason)
|
||||
error ||= exception.error if exception.respond_to?(:error)
|
||||
error ||= env["omniauth.failure_key"]
|
||||
error.to_s.humanize if error
|
||||
end
|
||||
|
||||
def after_omniauth_failure_path_for(scope)
|
||||
new_session_path(scope)
|
||||
end
|
||||
end
|
||||
@@ -4,7 +4,7 @@ class Devise::PasswordsController < ApplicationController
|
||||
|
||||
# GET /resource/password/new
|
||||
def new
|
||||
build_resource({})
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Devise::RegistrationsController < ApplicationController
|
||||
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
|
||||
prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
|
||||
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
|
||||
include Devise::Controllers::InternalHelpers
|
||||
|
||||
@@ -14,14 +14,8 @@ class Devise::RegistrationsController < ApplicationController
|
||||
build_resource
|
||||
|
||||
if resource.save
|
||||
if resource.active?
|
||||
set_flash_message :notice, :signed_up
|
||||
sign_in_and_redirect(resource_name, resource)
|
||||
else
|
||||
set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s
|
||||
expire_session_data_after_sign_in!
|
||||
redirect_to after_inactive_sign_up_path_for(resource)
|
||||
end
|
||||
set_flash_message :notice, :signed_up
|
||||
sign_in_and_redirect(resource_name, resource)
|
||||
else
|
||||
clean_up_passwords(resource)
|
||||
render_with_scope :new
|
||||
@@ -37,7 +31,6 @@ class Devise::RegistrationsController < ApplicationController
|
||||
def update
|
||||
if resource.update_with_password(params[resource_name])
|
||||
set_flash_message :notice, :updated
|
||||
sign_in resource_name, resource, :bypass => true
|
||||
redirect_to after_update_path_for(resource)
|
||||
else
|
||||
clean_up_passwords(resource)
|
||||
@@ -48,63 +41,17 @@ class Devise::RegistrationsController < ApplicationController
|
||||
# DELETE /resource
|
||||
def destroy
|
||||
resource.destroy
|
||||
sign_out_and_redirect(self.resource)
|
||||
set_flash_message :notice, :destroyed
|
||||
end
|
||||
|
||||
# GET /resource/cancel
|
||||
# Forces the session data which is usually expired after sign
|
||||
# in to be expired now. This is useful if the user wants to
|
||||
# cancel oauth signing in/up in the middle of the process,
|
||||
# removing all OAuth session data.
|
||||
def cancel
|
||||
expire_session_data_after_sign_in!
|
||||
redirect_to new_registration_path(resource_name)
|
||||
sign_out_and_redirect(self.resource)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Build a devise resource passing in the session. Useful to move
|
||||
# temporary session data to the newly created user.
|
||||
def build_resource(hash=nil)
|
||||
hash ||= params[resource_name] || {}
|
||||
self.resource = resource_class.new_with_session(hash, session)
|
||||
end
|
||||
|
||||
# The path used after sign up. You need to overwrite this method
|
||||
# in your own RegistrationsController.
|
||||
def after_sign_up_path_for(resource)
|
||||
after_sign_in_path_for(resource)
|
||||
end
|
||||
|
||||
# Overwrite redirect_for_sign_in so it takes uses after_sign_up_path_for.
|
||||
def redirect_for_sign_in(scope, resource) #:nodoc:
|
||||
redirect_to stored_location_for(scope) || after_sign_up_path_for(resource)
|
||||
end
|
||||
|
||||
# The path used after sign up for inactive accounts. You need to overwrite
|
||||
# this method in your own RegistrationsController.
|
||||
def after_inactive_sign_up_path_for(resource)
|
||||
root_path
|
||||
end
|
||||
|
||||
# The default url to be used after updating a resource. You need to overwrite
|
||||
# this method in your own RegistrationsController.
|
||||
def after_update_path_for(resource)
|
||||
if defined?(super)
|
||||
ActiveSupport::Deprecation.warn "Defining after_update_path_for in ApplicationController " <<
|
||||
"is deprecated. Please add a RegistrationsController to your application and define it there."
|
||||
super
|
||||
else
|
||||
after_sign_in_path_for(resource)
|
||||
end
|
||||
end
|
||||
|
||||
# Authenticates the current scope and gets a copy of the current resource.
|
||||
# We need to use a copy because we don't want actions like update changing
|
||||
# the current user in place.
|
||||
def authenticate_scope!
|
||||
send(:"authenticate_#{resource_name}!")
|
||||
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
|
||||
self.resource = resource_class.find(send(:"current_#{resource_name}").id)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,15 +10,14 @@ class Devise::SessionsController < ApplicationController
|
||||
|
||||
# POST /resource/sign_in
|
||||
def create
|
||||
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
|
||||
resource = warden.authenticate!(:scope => resource_name, :recall => "new")
|
||||
set_flash_message :notice, :signed_in
|
||||
sign_in_and_redirect(resource_name, resource)
|
||||
end
|
||||
|
||||
# GET /resource/sign_out
|
||||
def destroy
|
||||
signed_in = signed_in?(resource_name)
|
||||
set_flash_message :notice, :signed_out if signed_in?(resource_name)
|
||||
sign_out_and_redirect(resource_name)
|
||||
set_flash_message :notice, :signed_out if signed_in
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ class Devise::UnlocksController < ApplicationController
|
||||
|
||||
# GET /resource/unlock/new
|
||||
def new
|
||||
build_resource({})
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
|
||||
@@ -3,9 +3,7 @@ module DeviseHelper
|
||||
return "" if resource.errors.empty?
|
||||
|
||||
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
|
||||
sentence = I18n.t("errors.messages.not_saved",
|
||||
:count => resource.errors.count,
|
||||
:resource => resource_name)
|
||||
sentence = "#{pluralize(resource.errors.count, "error")} prohibited this #{resource_name} from being saved:"
|
||||
|
||||
html = <<-HTML
|
||||
<div id="error_explanation">
|
||||
@@ -16,4 +14,4 @@ module DeviseHelper
|
||||
|
||||
html.html_safe
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
class Devise::Mailer < ::ActionMailer::Base
|
||||
include Devise::Controllers::ScopedViews
|
||||
attr_reader :scope_name, :resource
|
||||
attr_reader :devise_mapping, :resource
|
||||
|
||||
def confirmation_instructions(record)
|
||||
setup_mail(record, :confirmation_instructions)
|
||||
@@ -18,36 +18,19 @@ class Devise::Mailer < ::ActionMailer::Base
|
||||
|
||||
# Configure default email options
|
||||
def setup_mail(record, action)
|
||||
initialize_from_record(record)
|
||||
mail headers_for(action)
|
||||
end
|
||||
@scope_name = Devise::Mapping.find_scope!(record)
|
||||
@devise_mapping = Devise.mappings[@scope_name]
|
||||
@resource = instance_variable_set("@#{@devise_mapping.name}", record)
|
||||
|
||||
def initialize_from_record(record)
|
||||
@scope_name = Devise::Mapping.find_scope!(record)
|
||||
@resource = instance_variable_set("@#{devise_mapping.name}", record)
|
||||
end
|
||||
|
||||
def devise_mapping
|
||||
@devise_mapping ||= Devise.mappings[scope_name]
|
||||
end
|
||||
|
||||
def headers_for(action)
|
||||
headers = {
|
||||
:subject => translate(devise_mapping, action),
|
||||
:from => mailer_sender(devise_mapping),
|
||||
:to => resource.email,
|
||||
:subject => translate(@devise_mapping, action),
|
||||
:from => mailer_sender(@devise_mapping),
|
||||
:to => record.email,
|
||||
:template_path => template_paths
|
||||
}
|
||||
|
||||
if resource.respond_to?(:headers_for)
|
||||
headers.merge!(resource.headers_for(action))
|
||||
end
|
||||
|
||||
unless headers.key?(:reply_to)
|
||||
headers[:reply_to] = headers[:from]
|
||||
end
|
||||
|
||||
headers
|
||||
headers.merge!(record.headers_for(action)) if record.respond_to?(:headers_for)
|
||||
mail(headers)
|
||||
end
|
||||
|
||||
def mailer_sender(mapping)
|
||||
@@ -60,7 +43,7 @@ class Devise::Mailer < ::ActionMailer::Base
|
||||
|
||||
def template_paths
|
||||
template_path = [self.class.mailer_name]
|
||||
template_path.unshift "#{@devise_mapping.scoped_path}/mailer" if self.class.scoped_views?
|
||||
template_path.unshift "#{@devise_mapping.plural}/mailer" if self.class.scoped_views?
|
||||
template_path
|
||||
end
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<%= devise_error_messages! %>
|
||||
|
||||
<p><%= f.label :email %><br />
|
||||
<%= f.email_field :email %></p>
|
||||
<%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Resend confirmation instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
<%= devise_error_messages! %>
|
||||
<%= f.hidden_field :reset_password_token %>
|
||||
|
||||
<p><%= f.label :password, "New password" %><br />
|
||||
<p><%= f.label :password %><br />
|
||||
<%= f.password_field :password %></p>
|
||||
|
||||
<p><%= f.label :password_confirmation, "Confirm new password" %><br />
|
||||
<p><%= f.label :password_confirmation %><br />
|
||||
<%= f.password_field :password_confirmation %></p>
|
||||
|
||||
<p><%= f.submit "Change my password" %></p>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<%= devise_error_messages! %>
|
||||
|
||||
<p><%= f.label :email %><br />
|
||||
<%= f.email_field :email %></p>
|
||||
<%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Send me reset password instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<%= devise_error_messages! %>
|
||||
|
||||
<p><%= f.label :email %><br />
|
||||
<%= f.email_field :email %></p>
|
||||
<%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
|
||||
<%= f.password_field :password %></p>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<%= devise_error_messages! %>
|
||||
|
||||
<p><%= f.label :email %><br />
|
||||
<%= f.email_field :email %></p>
|
||||
<%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %><br />
|
||||
<%= f.password_field :password %></p>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
|
||||
<p><%= f.label :email %><br />
|
||||
<%= f.email_field :email %></p>
|
||||
<%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %><br />
|
||||
<%= f.password_field :password %></p>
|
||||
|
||||
@@ -17,9 +17,3 @@
|
||||
<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
|
||||
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.omniauthable? %>
|
||||
<%- resource_class.omniauth_providers.each do |provider| %>
|
||||
<%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
@@ -4,7 +4,7 @@
|
||||
<%= devise_error_messages! %>
|
||||
|
||||
<p><%= f.label :email %><br />
|
||||
<%= f.email_field :email %></p>
|
||||
<%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Resend unlock instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
@@ -2,11 +2,8 @@ en:
|
||||
errors:
|
||||
messages:
|
||||
not_found: "not found"
|
||||
already_confirmed: "was already confirmed, please try signing in"
|
||||
already_confirmed: "was already confirmed"
|
||||
not_locked: "was not locked"
|
||||
not_saved:
|
||||
one: "1 error prohibited this %{resource} from being saved:"
|
||||
other: "%{count} errors prohibited this %{resource} from being saved:"
|
||||
|
||||
devise:
|
||||
failure:
|
||||
@@ -27,16 +24,12 @@ en:
|
||||
send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.'
|
||||
confirmed: 'Your account was successfully confirmed. You are now signed in.'
|
||||
registrations:
|
||||
signed_up: 'Welcome! You have signed up successfully.'
|
||||
inactive_signed_up: 'You have signed up successfully. However, we could not sign you in because your account is %{reason}.'
|
||||
signed_up: 'You have signed up successfully. If enabled, a confirmation was sent to your e-mail.'
|
||||
updated: 'You updated your account successfully.'
|
||||
destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
|
||||
unlocks:
|
||||
send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
|
||||
unlocked: 'Your account was successfully unlocked. You are now signed in.'
|
||||
omniauth_callbacks:
|
||||
success: 'Successfully authorized from %{kind} account.'
|
||||
failure: 'Could not authorize you from %{kind} because "%{reason}".'
|
||||
mailer:
|
||||
confirmation_instructions:
|
||||
subject: 'Confirmation instructions'
|
||||
|
||||
@@ -5,24 +5,29 @@
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = %q{devise}
|
||||
s.version = "1.2.rc"
|
||||
s.version = "1.1.5"
|
||||
|
||||
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["Jos\303\251 Valim", "Carlos Ant\303\264nio"]
|
||||
s.date = %q{2010-11-26}
|
||||
s.date = %q{2010-11-27}
|
||||
s.description = %q{Flexible authentication solution for Rails with Warden}
|
||||
s.email = %q{contact@plataformatec.com.br}
|
||||
s.extra_rdoc_files = [
|
||||
"CHANGELOG.rdoc",
|
||||
"Gemfile.lock",
|
||||
"MIT-LICENSE",
|
||||
"README.rdoc"
|
||||
"README.rdoc",
|
||||
"TODO"
|
||||
]
|
||||
s.files = [
|
||||
"CHANGELOG.rdoc",
|
||||
"Gemfile",
|
||||
"Gemfile.lock",
|
||||
"MIT-LICENSE",
|
||||
"README.rdoc",
|
||||
"Rakefile",
|
||||
"TODO",
|
||||
"app/controllers/devise/confirmations_controller.rb",
|
||||
"app/controllers/devise/omniauth_callbacks_controller.rb",
|
||||
"app/controllers/devise/passwords_controller.rb",
|
||||
"app/controllers/devise/registrations_controller.rb",
|
||||
"app/controllers/devise/sessions_controller.rb",
|
||||
@@ -48,6 +53,7 @@ Gem::Specification.new do |s|
|
||||
"lib/devise/controllers/url_helpers.rb",
|
||||
"lib/devise/encryptors/authlogic_sha512.rb",
|
||||
"lib/devise/encryptors/base.rb",
|
||||
"lib/devise/encryptors/bcrypt.rb",
|
||||
"lib/devise/encryptors/clearance_sha1.rb",
|
||||
"lib/devise/encryptors/restful_authentication_sha1.rb",
|
||||
"lib/devise/encryptors/sha1.rb",
|
||||
@@ -63,9 +69,7 @@ Gem::Specification.new do |s|
|
||||
"lib/devise/models/authenticatable.rb",
|
||||
"lib/devise/models/confirmable.rb",
|
||||
"lib/devise/models/database_authenticatable.rb",
|
||||
"lib/devise/models/encryptable.rb",
|
||||
"lib/devise/models/lockable.rb",
|
||||
"lib/devise/models/omniauthable.rb",
|
||||
"lib/devise/models/recoverable.rb",
|
||||
"lib/devise/models/registerable.rb",
|
||||
"lib/devise/models/rememberable.rb",
|
||||
@@ -74,10 +78,6 @@ Gem::Specification.new do |s|
|
||||
"lib/devise/models/trackable.rb",
|
||||
"lib/devise/models/validatable.rb",
|
||||
"lib/devise/modules.rb",
|
||||
"lib/devise/omniauth.rb",
|
||||
"lib/devise/omniauth/config.rb",
|
||||
"lib/devise/omniauth/test_helpers.rb",
|
||||
"lib/devise/omniauth/url_helpers.rb",
|
||||
"lib/devise/orm/active_record.rb",
|
||||
"lib/devise/orm/mongoid.rb",
|
||||
"lib/devise/path_checker.rb",
|
||||
@@ -98,6 +98,8 @@ Gem::Specification.new do |s|
|
||||
"lib/generators/devise/install_generator.rb",
|
||||
"lib/generators/devise/orm_helpers.rb",
|
||||
"lib/generators/devise/views_generator.rb",
|
||||
"lib/generators/devise_install_generator.rb",
|
||||
"lib/generators/devise_views_generator.rb",
|
||||
"lib/generators/mongoid/devise_generator.rb",
|
||||
"lib/generators/templates/README",
|
||||
"lib/generators/templates/devise.rb"
|
||||
@@ -114,15 +116,12 @@ Gem::Specification.new do |s|
|
||||
"test/devise_test.rb",
|
||||
"test/encryptors_test.rb",
|
||||
"test/failure_app_test.rb",
|
||||
"test/generators/generators_test_helper.rb",
|
||||
"test/generators/install_generator_test.rb",
|
||||
"test/generators/views_generator_test.rb",
|
||||
"test/indifferent_hash.rb",
|
||||
"test/integration/authenticatable_test.rb",
|
||||
"test/integration/confirmable_test.rb",
|
||||
"test/integration/database_authenticatable_test.rb",
|
||||
"test/integration/http_authenticatable_test.rb",
|
||||
"test/integration/lockable_test.rb",
|
||||
"test/integration/omniauthable_test.rb",
|
||||
"test/integration/recoverable_test.rb",
|
||||
"test/integration/registerable_test.rb",
|
||||
"test/integration/rememberable_test.rb",
|
||||
@@ -135,7 +134,6 @@ Gem::Specification.new do |s|
|
||||
"test/mapping_test.rb",
|
||||
"test/models/confirmable_test.rb",
|
||||
"test/models/database_authenticatable_test.rb",
|
||||
"test/models/encryptable_test.rb",
|
||||
"test/models/lockable_test.rb",
|
||||
"test/models/recoverable_test.rb",
|
||||
"test/models/rememberable_test.rb",
|
||||
@@ -144,19 +142,17 @@ Gem::Specification.new do |s|
|
||||
"test/models/trackable_test.rb",
|
||||
"test/models/validatable_test.rb",
|
||||
"test/models_test.rb",
|
||||
"test/omniauth/url_helpers_test.rb",
|
||||
"test/orm/active_record.rb",
|
||||
"test/orm/mongoid.rb",
|
||||
"test/rails_app/app/active_record/admin.rb",
|
||||
"test/rails_app/app/active_record/shim.rb",
|
||||
"test/rails_app/app/active_record/user.rb",
|
||||
"test/rails_app/app/controllers/admins/sessions_controller.rb",
|
||||
"test/rails_app/app/controllers/admins_controller.rb",
|
||||
"test/rails_app/app/controllers/application_controller.rb",
|
||||
"test/rails_app/app/controllers/home_controller.rb",
|
||||
"test/rails_app/app/controllers/publisher/registrations_controller.rb",
|
||||
"test/rails_app/app/controllers/publisher/sessions_controller.rb",
|
||||
"test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb",
|
||||
"test/rails_app/app/controllers/sessions_controller.rb",
|
||||
"test/rails_app/app/controllers/users_controller.rb",
|
||||
"test/rails_app/app/helpers/application_helper.rb",
|
||||
"test/rails_app/app/mongoid/admin.rb",
|
||||
@@ -175,12 +171,11 @@ Gem::Specification.new do |s|
|
||||
"test/rails_app/config/routes.rb",
|
||||
"test/rails_app/db/migrate/20100401102949_create_tables.rb",
|
||||
"test/rails_app/db/schema.rb",
|
||||
"test/rails_app/lib/shared_admin.rb",
|
||||
"test/rails_app/lib/shared_user.rb",
|
||||
"test/routes_test.rb",
|
||||
"test/support/assertions.rb",
|
||||
"test/support/helpers.rb",
|
||||
"test/support/integration.rb",
|
||||
"test/support/test_silencer.rb",
|
||||
"test/support/webrat/integrations/rails.rb",
|
||||
"test/test_helper.rb",
|
||||
"test/test_helpers_test.rb"
|
||||
@@ -192,16 +187,13 @@ Gem::Specification.new do |s|
|
||||
|
||||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
||||
s.add_runtime_dependency(%q<warden>, ["~> 1.0.2"])
|
||||
s.add_runtime_dependency(%q<orm_adapter>, ["~> 0.0.2"])
|
||||
s.add_runtime_dependency(%q<bcrypt-ruby>, ["~> 2.1.2"])
|
||||
else
|
||||
s.add_dependency(%q<warden>, ["~> 1.0.2"])
|
||||
s.add_dependency(%q<orm_adapter>, ["~> 0.0.2"])
|
||||
s.add_dependency(%q<bcrypt-ruby>, ["~> 2.1.2"])
|
||||
end
|
||||
else
|
||||
s.add_dependency(%q<warden>, ["~> 1.0.2"])
|
||||
s.add_dependency(%q<orm_adapter>, ["~> 0.0.2"])
|
||||
s.add_dependency(%q<bcrypt-ruby>, ["~> 2.1.2"])
|
||||
end
|
||||
end
|
||||
|
||||
161
lib/devise.rb
161
lib/devise.rb
@@ -1,11 +1,8 @@
|
||||
require 'active_support/core_ext/numeric/time'
|
||||
require 'active_support/dependencies'
|
||||
require 'orm_adapter'
|
||||
require 'set'
|
||||
|
||||
module Devise
|
||||
autoload :FailureApp, 'devise/failure_app'
|
||||
autoload :OmniAuth, 'devise/omniauth'
|
||||
autoload :PathChecker, 'devise/path_checker'
|
||||
autoload :Schema, 'devise/schema'
|
||||
autoload :TestHelpers, 'devise/test_helpers'
|
||||
@@ -19,6 +16,7 @@ module Devise
|
||||
|
||||
module Encryptors
|
||||
autoload :Base, 'devise/encryptors/base'
|
||||
autoload :Bcrypt, 'devise/encryptors/bcrypt'
|
||||
autoload :AuthlogicSha512, 'devise/encryptors/authlogic_sha512'
|
||||
autoload :ClearanceSha1, 'devise/encryptors/clearance_sha1'
|
||||
autoload :RestfulAuthenticationSha1, 'devise/encryptors/restful_authentication_sha1'
|
||||
@@ -32,12 +30,11 @@ module Devise
|
||||
end
|
||||
|
||||
# Constants which holds devise configuration for extensions. Those should
|
||||
# not be modified by the "end user" (this is why they are constants).
|
||||
# not be modified by the "end user".
|
||||
ALL = []
|
||||
CONTROLLERS = ActiveSupport::OrderedHash.new
|
||||
ROUTES = ActiveSupport::OrderedHash.new
|
||||
STRATEGIES = ActiveSupport::OrderedHash.new
|
||||
URL_HELPERS = ActiveSupport::OrderedHash.new
|
||||
|
||||
# True values used to check params
|
||||
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
|
||||
@@ -48,34 +45,31 @@ module Devise
|
||||
:sha512 => 128,
|
||||
:clearance_sha1 => 40,
|
||||
:restful_authentication_sha1 => 40,
|
||||
:authlogic_sha512 => 128
|
||||
:authlogic_sha512 => 128,
|
||||
:bcrypt => 60
|
||||
}
|
||||
|
||||
# Custom domain for cookies. Not set by default
|
||||
mattr_accessor :cookie_options
|
||||
@@cookie_options = {}
|
||||
mattr_accessor :cookie_domain
|
||||
@@cookie_domain = false
|
||||
|
||||
# Used to encrypt password. Please generate one with rake secret.
|
||||
mattr_accessor :pepper
|
||||
@@pepper = nil
|
||||
|
||||
# The number of times to encrypt password.
|
||||
mattr_accessor :stretches
|
||||
@@stretches = 10
|
||||
|
||||
# Keys used when authenticating a user.
|
||||
# Keys used when authenticating an user.
|
||||
mattr_accessor :authentication_keys
|
||||
@@authentication_keys = [ :email ]
|
||||
|
||||
# Request keys used when authenticating a user.
|
||||
mattr_accessor :request_keys
|
||||
@@request_keys = []
|
||||
|
||||
# Keys that should be case-insensitive.
|
||||
mattr_accessor :case_insensitive_keys
|
||||
@@case_insensitive_keys = [ :email ]
|
||||
|
||||
# If http authentication is enabled by default.
|
||||
mattr_accessor :http_authenticatable
|
||||
@@http_authenticatable = false
|
||||
|
||||
# If http headers should be returned for ajax requests. True by default.
|
||||
# If http authentication is used for ajax requests. True by default.
|
||||
mattr_accessor :http_authenticatable_on_xhr
|
||||
@@http_authenticatable_on_xhr = true
|
||||
|
||||
@@ -107,11 +101,6 @@ module Devise
|
||||
mattr_accessor :extend_remember_period
|
||||
@@extend_remember_period = false
|
||||
|
||||
# If true, uses salt as remember token and does not create it in the database.
|
||||
# By default is false for backwards compatibility.
|
||||
mattr_accessor :use_salt_as_remember_token
|
||||
@@use_salt_as_remember_token = false
|
||||
|
||||
# Time interval you can access your account before confirming your account.
|
||||
mattr_accessor :confirm_within
|
||||
@@confirm_within = 0.days
|
||||
@@ -120,14 +109,14 @@ module Devise
|
||||
mattr_accessor :timeout_in
|
||||
@@timeout_in = 30.minutes
|
||||
|
||||
# Used to encrypt password. Please generate one with rake secret.
|
||||
mattr_accessor :pepper
|
||||
@@pepper = nil
|
||||
|
||||
# Used to define the password encryption algorithm.
|
||||
mattr_accessor :encryptor
|
||||
@@encryptor = nil
|
||||
|
||||
# Store scopes mappings.
|
||||
mattr_accessor :mappings
|
||||
@@mappings = ActiveSupport::OrderedHash.new
|
||||
|
||||
# Tells if devise should apply the schema in ORMs where devise declaration
|
||||
# and schema belongs to the same class (as Datamapper and Mongoid).
|
||||
mattr_accessor :apply_schema
|
||||
@@ -168,58 +157,32 @@ module Devise
|
||||
mattr_accessor :token_authentication_key
|
||||
@@token_authentication_key = :auth_token
|
||||
|
||||
# If true, authentication through token does not store user in session
|
||||
mattr_accessor :stateless_token
|
||||
@@stateless_token = false
|
||||
|
||||
# Which formats should be treated as navigational.
|
||||
mattr_accessor :navigational_formats
|
||||
@@navigational_formats = [:"*/*", :html]
|
||||
|
||||
# When set to true, signing out an user signs out all other scopes.
|
||||
mattr_accessor :sign_out_all_scopes
|
||||
@@sign_out_all_scopes = true
|
||||
|
||||
# The default method used while signing out
|
||||
mattr_accessor :sign_out_via
|
||||
@@sign_out_via = :get
|
||||
|
||||
# PRIVATE CONFIGURATION
|
||||
|
||||
# Store scopes mappings.
|
||||
mattr_reader :mappings
|
||||
@@mappings = ActiveSupport::OrderedHash.new
|
||||
|
||||
# Omniauth configurations.
|
||||
mattr_reader :omniauth_configs
|
||||
@@omniauth_configs = ActiveSupport::OrderedHash.new
|
||||
|
||||
# Define a set of modules that are called when a mapping is added.
|
||||
mattr_reader :helpers
|
||||
@@helpers = Set.new
|
||||
@@helpers << Devise::Controllers::Helpers
|
||||
@@navigational_formats = [:html]
|
||||
|
||||
# Private methods to interface with Warden.
|
||||
mattr_accessor :warden_config
|
||||
@@warden_config = nil
|
||||
@@warden_config_block = nil
|
||||
|
||||
# When set to true, signing out an user signs out all other scopes.
|
||||
mattr_accessor :sign_out_all_scopes
|
||||
@@sign_out_all_scopes = false
|
||||
|
||||
def self.use_default_scope=(*)
|
||||
ActiveSupport::Deprecation.warn "config.use_default_scope is deprecated and removed from Devise. " <<
|
||||
"If you are using non conventional routes in Devise, all you need to do is to pass the devise " <<
|
||||
"scope in the router DSL:\n\n as :user do\n get \"sign_in\", :to => \"devise/sessions\"\n end\n\n" <<
|
||||
"The method :as is also aliased to :devise_scope. Choose the one you prefer.", caller
|
||||
end
|
||||
|
||||
# Default way to setup Devise. Run rails generate devise_install to create
|
||||
# a fresh initializer with all configuration values.
|
||||
def self.setup
|
||||
yield self
|
||||
end
|
||||
|
||||
def self.omniauth_providers
|
||||
omniauth_configs.keys
|
||||
end
|
||||
|
||||
def self.cookie_domain=(value)
|
||||
ActiveSupport::Deprecation.warn "Devise.cookie_domain=(value) is deprecated. "
|
||||
"Please use Devise.cookie_options = { :domain => value } instead."
|
||||
self.cookie_options[:domain] = value
|
||||
end
|
||||
|
||||
# Get the mailer class from the mailer reference object.
|
||||
def self.mailer
|
||||
@@mailer_ref.get
|
||||
@@ -234,19 +197,19 @@ module Devise
|
||||
# Small method that adds a mapping to Devise.
|
||||
def self.add_mapping(resource, options)
|
||||
mapping = Devise::Mapping.new(resource, options)
|
||||
@@mappings[mapping.name] = mapping
|
||||
@@default_scope ||= mapping.name
|
||||
@@helpers.each { |h| h.define_helpers(mapping) }
|
||||
self.mappings[mapping.name] = mapping
|
||||
self.default_scope ||= mapping.name
|
||||
mapping
|
||||
end
|
||||
|
||||
# Make Devise aware of an 3rd party Devise-module (like invitable). For convenience.
|
||||
# Make Devise aware of an 3rd party Devise-module. For convenience.
|
||||
#
|
||||
# == Options:
|
||||
#
|
||||
# +model+ - String representing the load path to a custom *model* for this module (to autoload.)
|
||||
# +controller+ - Symbol representing the name of an exisiting or custom *controller* for this module.
|
||||
# +route+ - Symbol representing the named *route* helper for this module.
|
||||
# +flash+ - Symbol representing the *flash messages* used by this helper.
|
||||
# +strategy+ - Symbol representing if this module got a custom *strategy*.
|
||||
#
|
||||
# All values, except :model, accept also a boolean and will have the same name as the given module
|
||||
@@ -262,36 +225,26 @@ module Devise
|
||||
ALL << module_name
|
||||
options.assert_valid_keys(:strategy, :model, :controller, :route)
|
||||
|
||||
if strategy = options[:strategy]
|
||||
STRATEGIES[module_name] = (strategy == true ? module_name : strategy)
|
||||
end
|
||||
config = {
|
||||
:strategy => STRATEGIES,
|
||||
:route => ROUTES,
|
||||
:controller => CONTROLLERS
|
||||
}
|
||||
|
||||
if controller = options[:controller]
|
||||
CONTROLLERS[module_name] = (controller == true ? module_name : controller)
|
||||
end
|
||||
config.each do |key, value|
|
||||
next unless options[key]
|
||||
name = (options[key] == true ? module_name : options[key])
|
||||
|
||||
if route = options[:route]
|
||||
case route
|
||||
when TrueClass
|
||||
key, value = module_name, []
|
||||
when Symbol
|
||||
key, value = route, []
|
||||
when Hash
|
||||
key, value = route.keys.first, route.values.flatten
|
||||
if value.is_a?(Hash)
|
||||
value[module_name] = name
|
||||
else
|
||||
raise ArgumentError, ":route should be true, a Symbol or a Hash"
|
||||
value << name unless value.include?(name)
|
||||
end
|
||||
|
||||
URL_HELPERS[key] ||= []
|
||||
URL_HELPERS[key].concat(value)
|
||||
URL_HELPERS[key].uniq!
|
||||
|
||||
ROUTES[module_name] = key
|
||||
end
|
||||
|
||||
if options[:model]
|
||||
path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
|
||||
Devise::Models.send(:autoload, module_name.to_s.camelize.to_sym, path)
|
||||
model_path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
|
||||
Devise::Models.send(:autoload, module_name.to_s.camelize.to_sym, model_path)
|
||||
end
|
||||
|
||||
Devise::Mapping.add_module module_name
|
||||
@@ -312,27 +265,6 @@ module Devise
|
||||
@@warden_config_block = block
|
||||
end
|
||||
|
||||
# Specify an omniauth provider.
|
||||
#
|
||||
# config.omniauth :github, APP_ID, APP_SECRET
|
||||
#
|
||||
def self.omniauth(provider, *args)
|
||||
@@helpers << Devise::OmniAuth::UrlHelpers
|
||||
@@omniauth_configs[provider] = Devise::OmniAuth::Config.new(provider, args)
|
||||
end
|
||||
|
||||
# Include helpers in the given scope to AC and AV.
|
||||
def self.include_helpers(scope)
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
include scope::Helpers if defined?(scope::Helpers)
|
||||
include scope::UrlHelpers
|
||||
end
|
||||
|
||||
ActiveSupport.on_load(:action_view) do
|
||||
include scope::UrlHelpers
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if Rails version is bigger than 3.0.x
|
||||
def self.rack_session?
|
||||
Rails::VERSION::STRING[0,3] != "3.0"
|
||||
@@ -344,7 +276,6 @@ module Devise
|
||||
@@warden_configured ||= begin
|
||||
warden_config.failure_app = Devise::FailureApp
|
||||
warden_config.default_scope = Devise.default_scope
|
||||
warden_config.intercept_401 = false
|
||||
|
||||
Devise.mappings.each_value do |mapping|
|
||||
warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
|
||||
@@ -357,7 +288,7 @@ module Devise
|
||||
|
||||
# Generate a friendly string randomically to be used as token.
|
||||
def self.friendly_token
|
||||
ActiveSupport::SecureRandom.base64(44).tr('+/=', 'xyz')
|
||||
ActiveSupport::SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -5,57 +5,8 @@ module Devise
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
helper_method :warden, :signed_in?, :devise_controller?, :anybody_signed_in?
|
||||
end
|
||||
|
||||
# Define authentication filters and accessor helpers based on mappings.
|
||||
# These filters should be used inside the controllers as before_filters,
|
||||
# so you can control the scope of the user who should be signed in to
|
||||
# access that specific controller/action.
|
||||
# Example:
|
||||
#
|
||||
# Roles:
|
||||
# User
|
||||
# Admin
|
||||
#
|
||||
# Generated methods:
|
||||
# authenticate_user! # Signs user in or redirect
|
||||
# authenticate_admin! # Signs admin in or redirect
|
||||
# user_signed_in? # Checks whether there is an user signed in or not
|
||||
# admin_signed_in? # Checks whether there is an admin signed in or not
|
||||
# current_user # Current signed in user
|
||||
# current_admin # Current signed in admin
|
||||
# user_session # Session data available only to the user scope
|
||||
# admin_session # Session data available only to the admin scope
|
||||
#
|
||||
# Use:
|
||||
# before_filter :authenticate_user! # Tell devise to use :user map
|
||||
# before_filter :authenticate_admin! # Tell devise to use :admin map
|
||||
#
|
||||
def self.define_helpers(mapping) #:nodoc:
|
||||
mapping = mapping.name
|
||||
|
||||
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
||||
def authenticate_#{mapping}!
|
||||
warden.authenticate!(:scope => :#{mapping})
|
||||
end
|
||||
|
||||
def #{mapping}_signed_in?
|
||||
!!current_#{mapping}
|
||||
end
|
||||
|
||||
def current_#{mapping}
|
||||
@current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
|
||||
end
|
||||
|
||||
def #{mapping}_session
|
||||
current_#{mapping} && warden.session(:#{mapping})
|
||||
end
|
||||
METHODS
|
||||
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
|
||||
end
|
||||
helper_method :warden, :signed_in?, :devise_controller?, :anybody_signed_in?,
|
||||
*Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?", :"#{m}_session"] }.flatten
|
||||
end
|
||||
|
||||
# The main accessor for the warden proxy instance
|
||||
@@ -65,7 +16,7 @@ module Devise
|
||||
|
||||
# Return true if it's a devise_controller. false to all controllers unless
|
||||
# the controllers defined inside devise. Useful if you want to apply a before
|
||||
# filter to all controllers, except the ones in devise:
|
||||
# filter to all controller, except the ones in devise:
|
||||
#
|
||||
# before_filter :my_filter, :unless => { |c| c.devise_controller? }
|
||||
def devise_controller?
|
||||
@@ -87,30 +38,15 @@ module Devise
|
||||
# Sign in an user that already was authenticated. This helper is useful for logging
|
||||
# users in after sign up.
|
||||
#
|
||||
# All options given to sign_in is passed forward to the set_user method in warden.
|
||||
# The only exception is the :bypass option, which bypass warden callbacks and stores
|
||||
# the user straight in session. This option is useful in cases the user is already
|
||||
# signed in, but we want to refresh the credentials in session.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# sign_in :user, @user # sign_in(scope, resource)
|
||||
# sign_in @user # sign_in(resource)
|
||||
# sign_in @user, :event => :authentication # sign_in(resource, options)
|
||||
# sign_in @user, :bypass => true # sign_in(resource, options)
|
||||
#
|
||||
def sign_in(resource_or_scope, *args)
|
||||
options = args.extract_options!
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
resource = args.last || resource_or_scope
|
||||
|
||||
expire_session_data_after_sign_in!
|
||||
|
||||
if options[:bypass]
|
||||
warden.session_serializer.store(resource, scope)
|
||||
else
|
||||
warden.set_user(resource, options.merge!(:scope => scope))
|
||||
end
|
||||
# sign_in :user, @user # sign_in(scope, resource)
|
||||
# sign_in @user # sign_in(resource)
|
||||
#
|
||||
def sign_in(resource_or_scope, resource=nil)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
resource ||= resource_or_scope
|
||||
warden.set_user(resource, :scope => scope)
|
||||
end
|
||||
|
||||
# Sign out a given user or scope. This helper is useful for signing out an user
|
||||
@@ -121,8 +57,7 @@ module Devise
|
||||
# sign_out :user # sign_out(scope)
|
||||
# sign_out @user # sign_out(resource)
|
||||
#
|
||||
def sign_out(resource_or_scope=nil)
|
||||
return sign_out_all_scopes unless resource_or_scope
|
||||
def sign_out(resource_or_scope)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
warden.user(scope) # Without loading user here, before_logout hook is not called
|
||||
warden.raw_session.inspect # Without this inspect here. The session does not clear.
|
||||
@@ -130,10 +65,13 @@ module Devise
|
||||
end
|
||||
|
||||
# Sign out all active users or scopes. This helper is useful for signing out all roles
|
||||
# in one click. This signs out ALL scopes in warden.
|
||||
# in one click.
|
||||
def sign_out_all_scopes
|
||||
# Not "warden.logout" since we need to sign_out only devise-defined scopes.
|
||||
scopes = Devise.mappings.keys
|
||||
scopes.each { |scope| warden.user(scope) }
|
||||
warden.raw_session.inspect
|
||||
warden.logout
|
||||
warden.logout(*scopes)
|
||||
end
|
||||
|
||||
# Returns and delete the url stored in the session for the given scope. Useful
|
||||
@@ -180,6 +118,36 @@ module Devise
|
||||
respond_to?(home_path, true) ? send(home_path) : root_path
|
||||
end
|
||||
|
||||
# The default url to be used after updating a resource. This is used by all Devise
|
||||
# controllers and you can overwrite it in your ApplicationController to
|
||||
# provide a custom hook for a custom resource.
|
||||
#
|
||||
# By default, it first tries to find a resource_root_path, otherwise it
|
||||
# uses the root path. For a user scope, you can define the default url in
|
||||
# the following way:
|
||||
#
|
||||
# map.user_root '/users', :controller => 'users' # creates user_root_path
|
||||
#
|
||||
# map.resources :users do |users|
|
||||
# users.root # creates user_root_path
|
||||
# end
|
||||
#
|
||||
#
|
||||
# If none of these are defined, root_path is used. However, if this default
|
||||
# is not enough, you can customize it, for example:
|
||||
#
|
||||
# def after_update_path_for(resource)
|
||||
# if resource.is_a?(User) && resource.can_publish?
|
||||
# publisher_url
|
||||
# else
|
||||
# super
|
||||
# end
|
||||
# end
|
||||
#
|
||||
def after_update_path_for(resource_or_scope)
|
||||
after_sign_in_path_for(resource_or_scope)
|
||||
end
|
||||
|
||||
# Method used by sessions controller to sign out an user. You can overwrite
|
||||
# it in your ApplicationController to provide a custom hook for a custom
|
||||
# scope. Notice that differently from +after_sign_in_path_for+ this method
|
||||
@@ -191,23 +159,14 @@ module Devise
|
||||
end
|
||||
|
||||
# Sign in an user and tries to redirect first to the stored location and
|
||||
# then to the url specified by after_sign_in_path_for. It accepts the same
|
||||
# parameters as the sign_in method.
|
||||
def sign_in_and_redirect(resource_or_scope, *args)
|
||||
options = args.extract_options!
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
resource = args.last || resource_or_scope
|
||||
|
||||
if warden.user(scope) == resource
|
||||
expire_session_data_after_sign_in!
|
||||
else
|
||||
sign_in(scope, resource, options)
|
||||
end
|
||||
|
||||
redirect_for_sign_in(scope, resource)
|
||||
end
|
||||
|
||||
def redirect_for_sign_in(scope, resource) #:nodoc:
|
||||
# then to the url specified by after_sign_in_path_for.
|
||||
#
|
||||
# If just a symbol is given, consider that the user was already signed in
|
||||
# through other means and just perform the redirection.
|
||||
def sign_in_and_redirect(resource_or_scope, resource=nil)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
resource ||= resource_or_scope
|
||||
sign_in(scope, resource) unless warden.user(scope) == resource
|
||||
redirect_to stored_location_for(scope) || after_sign_in_path_for(resource)
|
||||
end
|
||||
|
||||
@@ -215,19 +174,58 @@ module Devise
|
||||
# after_sign_out_path_for.
|
||||
def sign_out_and_redirect(resource_or_scope)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
|
||||
redirect_for_sign_out(scope)
|
||||
end
|
||||
|
||||
def redirect_for_sign_out(scope) #:nodoc:
|
||||
if Devise.sign_out_all_scopes
|
||||
sign_out_all_scopes
|
||||
else
|
||||
sign_out(scope)
|
||||
end
|
||||
redirect_to after_sign_out_path_for(scope)
|
||||
end
|
||||
|
||||
# A hook called to expire session data after sign up/in. All keys
|
||||
# stored under "devise." namespace are removed after sign in.
|
||||
def expire_session_data_after_sign_in!
|
||||
session.keys.grep(/^devise\./).each { |k| session.delete(k) }
|
||||
# Define authentication filters and accessor helpers based on mappings.
|
||||
# These filters should be used inside the controllers as before_filters,
|
||||
# so you can control the scope of the user who should be signed in to
|
||||
# access that specific controller/action.
|
||||
# Example:
|
||||
#
|
||||
# Roles:
|
||||
# User
|
||||
# Admin
|
||||
#
|
||||
# Generated methods:
|
||||
# authenticate_user! # Signs user in or redirect
|
||||
# authenticate_admin! # Signs admin in or redirect
|
||||
# user_signed_in? # Checks whether there is an user signed in or not
|
||||
# admin_signed_in? # Checks whether there is an admin signed in or not
|
||||
# current_user # Current signed in user
|
||||
# current_admin # Currend signed in admin
|
||||
# user_session # Session data available only to the user scope
|
||||
# admin_session # Session data available only to the admin scope
|
||||
#
|
||||
# Use:
|
||||
# before_filter :authenticate_user! # Tell devise to use :user map
|
||||
# before_filter :authenticate_admin! # Tell devise to use :admin map
|
||||
#
|
||||
Devise.mappings.each_key do |mapping|
|
||||
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
||||
def authenticate_#{mapping}!
|
||||
warden.authenticate!(:scope => :#{mapping})
|
||||
end
|
||||
|
||||
def #{mapping}_signed_in?
|
||||
warden.authenticate?(:scope => :#{mapping})
|
||||
end
|
||||
|
||||
def current_#{mapping}
|
||||
@current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
|
||||
end
|
||||
|
||||
def #{mapping}_session
|
||||
current_#{mapping} && warden.session(:#{mapping})
|
||||
end
|
||||
METHODS
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,7 +10,7 @@ module Devise
|
||||
included do
|
||||
helper DeviseHelper
|
||||
|
||||
helpers = %w(resource scope_name resource_name signed_in_resource
|
||||
helpers = %w(resource scope_name resource_name
|
||||
resource_class devise_mapping devise_controller?)
|
||||
hide_action *helpers
|
||||
helper_method *helpers
|
||||
@@ -35,11 +35,6 @@ module Devise
|
||||
devise_mapping.to
|
||||
end
|
||||
|
||||
# Returns a signed in resource from session (if one exists)
|
||||
def signed_in_resource
|
||||
warden.authenticate(:scope => resource_name)
|
||||
end
|
||||
|
||||
# Attempt to find the mapped route for devise based on request path
|
||||
def devise_mapping
|
||||
@devise_mapping ||= request.env["devise.mapping"]
|
||||
@@ -54,12 +49,7 @@ module Devise
|
||||
|
||||
# Checks whether it's a devise mapped resource or not.
|
||||
def is_devise_resource? #:nodoc:
|
||||
unknown_action!("Could not find devise mapping for path #{request.fullpath.inspect}") unless devise_mapping
|
||||
end
|
||||
|
||||
def unknown_action!(msg)
|
||||
logger.debug "[Devise] #{msg}" if logger
|
||||
raise ActionController::UnknownAction, msg
|
||||
raise ActionController::UnknownAction unless devise_mapping
|
||||
end
|
||||
|
||||
# Sets the resource creating an instance variable
|
||||
@@ -78,10 +68,7 @@ module Devise
|
||||
# Example:
|
||||
# before_filter :require_no_authentication, :only => :new
|
||||
def require_no_authentication
|
||||
if warden.authenticated?(resource_name)
|
||||
resource = warden.user(resource_name)
|
||||
redirect_to after_sign_in_path_for(resource)
|
||||
end
|
||||
redirect_to after_sign_in_path_for(resource_name) if warden.authenticated?(resource_name)
|
||||
end
|
||||
|
||||
# Sets the flash message with :key, using I18n. By default you are able
|
||||
@@ -98,15 +85,12 @@ module Devise
|
||||
#
|
||||
# Please refer to README or en.yml locale file to check what messages are
|
||||
# available.
|
||||
def set_flash_message(key, kind, options={}) #:nodoc:
|
||||
options[:scope] = "devise.#{controller_name}"
|
||||
options[:default] = Array(options[:default]).unshift(kind.to_sym)
|
||||
options[:resource_name] = resource_name
|
||||
message = I18n.t("#{resource_name}.#{kind}", options)
|
||||
flash[key] = message if message.present?
|
||||
def set_flash_message(key, kind)
|
||||
flash[key] = I18n.t(:"#{resource_name}.#{kind}", :resource_name => resource_name,
|
||||
:scope => [:devise, controller_name.to_sym], :default => kind)
|
||||
end
|
||||
|
||||
def clean_up_passwords(object) #:nodoc:
|
||||
def clean_up_passwords(object)
|
||||
object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,15 +17,17 @@ module Devise
|
||||
|
||||
# Render a view for the specified scope. Turned off by default.
|
||||
# Accepts just :controller as option.
|
||||
def render_with_scope(action, path=self.controller_path)
|
||||
def render_with_scope(action, options={})
|
||||
controller_name = options.delete(:controller) || self.controller_name
|
||||
|
||||
if self.class.scoped_views?
|
||||
begin
|
||||
render :template => "#{devise_mapping.scoped_path}/#{path.split("/").last}/#{action}"
|
||||
render :template => "#{devise_mapping.plural}/#{controller_name}/#{action}"
|
||||
rescue ActionView::MissingTemplate
|
||||
render :template => "#{path}/#{action}"
|
||||
render :template => "#{controller_path}/#{action}"
|
||||
end
|
||||
else
|
||||
render :template => "#{path}/#{action}"
|
||||
render :template => "#{controller_path}/#{action}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,11 +19,13 @@ module Devise
|
||||
# Those helpers are added to your ApplicationController.
|
||||
module UrlHelpers
|
||||
|
||||
Devise::URL_HELPERS.each do |module_name, actions|
|
||||
Devise::ROUTES.values.uniq.each do |module_name|
|
||||
[:path, :url].each do |path_or_url|
|
||||
actions.each do |action|
|
||||
action = action ? "#{action}_" : ""
|
||||
actions = [ nil, :new_ ]
|
||||
actions << :edit_ if [:password, :registration].include?(module_name)
|
||||
actions << :destroy_ if [:session].include?(module_name)
|
||||
|
||||
actions.each do |action|
|
||||
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
||||
def #{action}#{module_name}_#{path_or_url}(resource_or_scope, *args)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
|
||||
@@ -13,7 +13,7 @@ module Devise
|
||||
end
|
||||
|
||||
def self.salt(stretches)
|
||||
Devise.friendly_token[0,20]
|
||||
Devise.friendly_token
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
19
lib/devise/encryptors/bcrypt.rb
Normal file
19
lib/devise/encryptors/bcrypt.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
require "bcrypt"
|
||||
|
||||
module Devise
|
||||
module Encryptors
|
||||
# = BCrypt
|
||||
# Uses the BCrypt hash algorithm to encrypt passwords.
|
||||
class Bcrypt < Base
|
||||
# Gererates a default password digest based on stretches, salt, pepper and the
|
||||
# incoming password. We don't strech it ourselves since BCrypt does so internally.
|
||||
def self.digest(password, stretches, salt, pepper)
|
||||
::BCrypt::Engine.hash_secret([password, pepper].join, salt, stretches)
|
||||
end
|
||||
|
||||
def self.salt(stretches)
|
||||
::BCrypt::Engine.generate_salt(stretches)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,10 +5,10 @@ module Devise
|
||||
# = RestfulAuthenticationSha1
|
||||
# Simulates Restful Authentication's default encryption mechanism.
|
||||
# Warning: it uses Devise's pepper to port the concept of REST_AUTH_SITE_KEY
|
||||
# Warning: it uses Devise's stretches configuration to port the concept of REST_AUTH_DIGEST_STRETCHES. Should be set to 10 in
|
||||
# the initializer to simulate the default behavior.
|
||||
# Warning: it uses Devise's stretches configuration to port the concept of REST_AUTH_DIGEST_STRETCHES. Should be set to 10 in
|
||||
# the initializer to silumate the default behavior.
|
||||
class RestfulAuthenticationSha1 < Base
|
||||
|
||||
|
||||
# Gererates a default password digest based on salt, pepper and the
|
||||
# incoming password.
|
||||
def self.digest(password, stretches, salt, pepper)
|
||||
@@ -19,4 +19,4 @@ module Devise
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -33,7 +33,7 @@ module Devise
|
||||
|
||||
def http_auth
|
||||
self.status = 401
|
||||
self.headers["WWW-Authenticate"] = %(Basic realm=#{Devise.http_authentication_realm.inspect}) if http_auth_header?
|
||||
self.headers["WWW-Authenticate"] = %(Basic realm=#{Devise.http_authentication_realm.inspect})
|
||||
self.content_type = request.format.to_s
|
||||
self.response_body = http_auth_body
|
||||
end
|
||||
@@ -41,12 +41,12 @@ module Devise
|
||||
def recall
|
||||
env["PATH_INFO"] = attempted_path
|
||||
flash.now[:alert] = i18n_message(:invalid)
|
||||
self.response = recall_app(warden_options[:recall]).call(env)
|
||||
self.response = recall_controller.action(warden_options[:recall]).call(env)
|
||||
end
|
||||
|
||||
def redirect
|
||||
store_location!
|
||||
flash[:alert] = i18n_message
|
||||
flash[:alert] = i18n_message unless flash[:notice]
|
||||
redirect_to redirect_url
|
||||
end
|
||||
|
||||
@@ -67,37 +67,17 @@ module Devise
|
||||
send(:"new_#{scope}_session_path")
|
||||
end
|
||||
|
||||
# Choose whether we should respond in a http authentication fashion,
|
||||
# including 401 and optional headers.
|
||||
#
|
||||
# This method allows the user to explicitly disable http authentication
|
||||
# on ajax requests in case they want to redirect on failures instead of
|
||||
# handling the errors on their own. This is useful in case your ajax API
|
||||
# is the same as your public API and uses a format like JSON (so you
|
||||
# cannot mark JSON as a navigational format).
|
||||
def http_auth?
|
||||
if request.xhr?
|
||||
Devise.http_authenticatable_on_xhr
|
||||
else
|
||||
!(request.format && Devise.navigational_formats.include?(request.format.to_sym))
|
||||
end
|
||||
end
|
||||
|
||||
# It does not make sense to send authenticate headers in ajax requests
|
||||
# or if the user disabled them.
|
||||
def http_auth_header?
|
||||
Devise.mappings[scope].to.http_authenticatable && !request.xhr?
|
||||
!Devise.navigational_formats.include?(request.format.to_sym) || (request.xhr? && Devise.http_authenticatable_on_xhr)
|
||||
end
|
||||
|
||||
def http_auth_body
|
||||
return i18n_message unless request.format
|
||||
method = "to_#{request.format.to_sym}"
|
||||
method = :"to_#{request.format.to_sym}"
|
||||
{}.respond_to?(method) ? { :error => i18n_message }.send(method) : i18n_message
|
||||
end
|
||||
|
||||
def recall_app(app)
|
||||
controller, action = app.split("#")
|
||||
"#{controller.camelize}Controller".constantize.action(action)
|
||||
def recall_controller
|
||||
"#{params[:controller].camelize}Controller".constantize
|
||||
end
|
||||
|
||||
def warden
|
||||
@@ -109,7 +89,7 @@ module Devise
|
||||
end
|
||||
|
||||
def scope
|
||||
@scope ||= warden_options[:scope] || Devise.default_scope
|
||||
@scope ||= warden_options[:scope]
|
||||
end
|
||||
|
||||
def attempted_path
|
||||
@@ -121,7 +101,7 @@ module Devise
|
||||
# yet, but we still need to store the uri based on scope, so different scopes
|
||||
# would never use the same uri to redirect.
|
||||
def store_location!
|
||||
session["#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
|
||||
session[:"#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
Warden::Manager.before_logout do |record, warden, options|
|
||||
if record.respond_to?(:forget_me!)
|
||||
record.forget_me! unless record.frozen?
|
||||
cookie_options = Rails.configuration.session_options.slice(:path, :domain, :secure)
|
||||
cookie_options.merge!(record.cookie_options)
|
||||
cookie_options = record.cookie_domain? ? { :domain => record.cookie_domain } : {}
|
||||
warden.cookies.delete("remember_#{options[:scope]}_token", cookie_options)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,22 +10,20 @@ module Devise
|
||||
|
||||
if succeeded? && resource.respond_to?(:remember_me!) && remember_me?
|
||||
resource.remember_me!(extend_remember_period?)
|
||||
cookies.signed["remember_#{scope}_token"] = cookie_values(resource)
|
||||
|
||||
configuration = {
|
||||
:value => resource.class.serialize_into_cookie(resource),
|
||||
:expires => resource.remember_expires_at,
|
||||
:path => "/"
|
||||
}
|
||||
|
||||
configuration[:domain] = resource.cookie_domain if resource.cookie_domain?
|
||||
cookies.signed["remember_#{scope}_token"] = configuration
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def cookie_values(resource)
|
||||
options = Rails.configuration.session_options.slice(:path, :domain, :secure)
|
||||
options.merge!(resource.cookie_options)
|
||||
options.merge!(
|
||||
:value => resource.class.serialize_into_cookie(resource),
|
||||
:expires => resource.remember_expires_at
|
||||
)
|
||||
options
|
||||
end
|
||||
|
||||
def succeeded?
|
||||
@result == :success
|
||||
end
|
||||
|
||||
@@ -22,7 +22,7 @@ module Devise
|
||||
# # is the modules included in the class
|
||||
#
|
||||
class Mapping #:nodoc:
|
||||
attr_reader :singular, :scoped_path, :path, :controllers, :path_names, :class_name, :sign_out_via
|
||||
attr_reader :singular, :plural, :path, :controllers, :path_names, :class_name
|
||||
alias :name :singular
|
||||
|
||||
# Receives an object and find a scope for it. If a scope cannot be found,
|
||||
@@ -37,17 +37,12 @@ module Devise
|
||||
Devise.mappings.each_value { |m| return m.name if duck.is_a?(m.to) }
|
||||
end
|
||||
|
||||
raise "Could not find a valid mapping for #{duck.inspect}"
|
||||
end
|
||||
|
||||
def self.find_by_path!(path, path_type=:fullpath)
|
||||
Devise.mappings.each_value { |m| return m if path.include?(m.send(path_type)) }
|
||||
raise "Could not find a valid mapping for path #{path.inspect}"
|
||||
raise "Could not find a valid mapping for #{duck}"
|
||||
end
|
||||
|
||||
def initialize(name, options) #:nodoc:
|
||||
@scoped_path = options[:as] ? "#{options[:as]}/#{name}" : name.to_s
|
||||
@singular = (options[:singular] || @scoped_path.tr('/', '_').singularize).to_sym
|
||||
@plural = (options[:as] ? "#{options[:as]}_#{name}" : name).to_sym
|
||||
@singular = (options[:singular] || @plural.to_s.singularize).to_sym
|
||||
|
||||
@class_name = (options[:class_name] || name.to_s.classify).to_s
|
||||
@ref = ActiveSupport::Dependencies.ref(@class_name)
|
||||
@@ -62,8 +57,6 @@ module Devise
|
||||
@path_names = Hash.new { |h,k| h[k] = k.to_s }
|
||||
@path_names.merge!(:registration => "")
|
||||
@path_names.merge!(options[:path_names] || {})
|
||||
|
||||
@sign_out_via = options[:sign_out_via] || Devise.sign_out_via
|
||||
end
|
||||
|
||||
# Return modules for the mapping.
|
||||
@@ -89,7 +82,7 @@ module Devise
|
||||
end
|
||||
|
||||
def fullpath
|
||||
"/#{@path_prefix}/#{@path}".squeeze("/")
|
||||
"#{@path_prefix}/#{@path}".squeeze("/")
|
||||
end
|
||||
|
||||
# Create magic predicates for verifying what module is activated by this map.
|
||||
|
||||
@@ -47,6 +47,20 @@ module Devise
|
||||
def devise(*modules)
|
||||
include Devise::Models::Authenticatable
|
||||
options = modules.extract_options!
|
||||
|
||||
if modules.delete(:authenticatable)
|
||||
ActiveSupport::Deprecation.warn ":authenticatable as module is deprecated. Please give :database_authenticatable instead.", caller
|
||||
modules << :database_authenticatable
|
||||
end
|
||||
|
||||
if modules.delete(:activatable)
|
||||
ActiveSupport::Deprecation.warn ":activatable as module is deprecated. It's included in your model by default.", caller
|
||||
end
|
||||
|
||||
if modules.delete(:http_authenticatable)
|
||||
ActiveSupport::Deprecation.warn ":http_authenticatable as module is deprecated and is on by default. Revert by setting :http_authenticatable => false.", caller
|
||||
end
|
||||
|
||||
self.devise_modules += Devise::ALL & modules.map(&:to_sym).uniq
|
||||
|
||||
devise_modules_hook! do
|
||||
|
||||
@@ -4,25 +4,18 @@ module Devise
|
||||
module Models
|
||||
# Authenticable module. Holds common settings for authentication.
|
||||
#
|
||||
# == Options
|
||||
# == Configuration:
|
||||
#
|
||||
# Authenticatable adds the following options to devise_for:
|
||||
# You can overwrite configuration values by setting in globally in Devise,
|
||||
# using devise method or overwriting the respective instance method.
|
||||
#
|
||||
# * +authentication_keys+: parameters used for authentication. By default [:email].
|
||||
# authentication_keys: parameters used for authentication. By default [:email].
|
||||
#
|
||||
# * +request_keys+: parameters from the request object used for authentication.
|
||||
# By specifying a symbol (which should be a request method), it will automatically be
|
||||
# passed to find_for_authentication method and considered in your model lookup.
|
||||
# http_authenticatable: if this model allows http authentication. By default true.
|
||||
# It also accepts an array specifying the strategies that should allow http.
|
||||
#
|
||||
# For instance, if you set :request_keys to [:subdomain], :subdomain will be considered
|
||||
# as key on authentication. This can also be a hash where the value is a boolean expliciting
|
||||
# if the value is required or not.
|
||||
#
|
||||
# * +http_authenticatable+: if this model allows http authentication. By default true.
|
||||
# It also accepts an array specifying the strategies that should allow http.
|
||||
#
|
||||
# * +params_authenticatable+: if this model allows authentication through request params. By default true.
|
||||
# It also accepts an array specifying the strategies that should allow params authentication.
|
||||
# params_authenticatable: if this model allows authentication through request params. By default true.
|
||||
# It also accepts an array specifying the strategies that should allow params authentication.
|
||||
#
|
||||
# == Active?
|
||||
#
|
||||
@@ -73,11 +66,8 @@ module Devise
|
||||
:inactive
|
||||
end
|
||||
|
||||
def authenticatable_salt
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
Devise::Models.config(self, :authentication_keys, :request_keys, :case_insensitive_keys, :http_authenticatable, :params_authenticatable)
|
||||
Devise::Models.config(self, :authentication_keys, :http_authenticatable, :params_authenticatable)
|
||||
|
||||
def params_authenticatable?(strategy)
|
||||
params_authenticatable.is_a?(Array) ?
|
||||
@@ -100,34 +90,24 @@ module Devise
|
||||
# end
|
||||
#
|
||||
def find_for_authentication(conditions)
|
||||
case_insensitive_keys.each { |k| conditions[k].try(:downcase!) }
|
||||
to_adapter.find_first(conditions)
|
||||
find(:first, :conditions => conditions)
|
||||
end
|
||||
|
||||
# Find an initialize a record setting an error if it can't be found.
|
||||
def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
|
||||
find_or_initialize_with_errors([attribute], { attribute => value }, error)
|
||||
end
|
||||
|
||||
# Find an initialize a group of attributes based on a list of required attributes.
|
||||
def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
|
||||
case_insensitive_keys.each { |k| attributes[k].try(:downcase!) }
|
||||
|
||||
attributes = attributes.slice(*required_attributes)
|
||||
attributes.delete_if { |key, value| value.blank? }
|
||||
|
||||
if attributes.size == required_attributes.size
|
||||
record = to_adapter.find_first(attributes)
|
||||
if value.present?
|
||||
conditions = { attribute => value }
|
||||
record = find(:first, :conditions => conditions)
|
||||
end
|
||||
|
||||
|
||||
unless record
|
||||
record = new
|
||||
|
||||
required_attributes.each do |key|
|
||||
value = attributes[key]
|
||||
record.send("#{key}=", value)
|
||||
record.errors.add(key, value.present? ? error : :blank)
|
||||
if value.present?
|
||||
record.send(:"#{attribute}=", value)
|
||||
else
|
||||
error = :blank
|
||||
end
|
||||
record.errors.add(attribute, error)
|
||||
end
|
||||
|
||||
record
|
||||
@@ -137,7 +117,7 @@ module Devise
|
||||
def generate_token(column)
|
||||
loop do
|
||||
token = Devise.friendly_token
|
||||
break token unless to_adapter.find_first({ column => token })
|
||||
break token unless find(:first, :conditions => { column => token })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,24 +3,28 @@ module Devise
|
||||
# Confirmable is responsible to verify if an account is already confirmed to
|
||||
# sign in, and to send emails with confirmation instructions.
|
||||
# Confirmation instructions are sent to the user email after creating a
|
||||
# record and when manually requested by a new confirmation instruction request.
|
||||
# record, after updating it's email and also when manually requested by
|
||||
# a new confirmation instruction request.
|
||||
# Whenever the user update it's email, his account is automatically unconfirmed,
|
||||
# it means it won't be able to sign in again without confirming the account
|
||||
# again through the email that was sent.
|
||||
#
|
||||
# == Options
|
||||
# Configuration:
|
||||
#
|
||||
# Confirmable adds the following options to devise_for:
|
||||
# confirm_within: the time you want the user will have to confirm it's account
|
||||
# without blocking his access. When confirm_within is zero, the
|
||||
# user won't be able to sign in without confirming. You can
|
||||
# use this to let your user access some features of your
|
||||
# application without confirming the account, but blocking it
|
||||
# after a certain period (ie 7 days). By default confirm_within is
|
||||
# zero, it means users always have to confirm to sign in.
|
||||
#
|
||||
# * +confirm_within+: the time you want to allow the user to access his account
|
||||
# before confirming it. After this period, the user access is denied. You can
|
||||
# use this to let your user access some features of your application without
|
||||
# confirming the account, but blocking it after a certain period (ie 7 days).
|
||||
# By default confirm_within is zero, it means users always have to confirm to sign in.
|
||||
#
|
||||
# == Examples
|
||||
# Examples:
|
||||
#
|
||||
# User.find(1).confirm! # returns true unless it's already confirmed
|
||||
# User.find(1).confirmed? # true/false
|
||||
# User.find(1).send_confirmation_instructions # manually send instructions
|
||||
#
|
||||
# User.find(1).resend_confirmation! # generates a new token and resent it
|
||||
module Confirmable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
require 'devise/strategies/database_authenticatable'
|
||||
require 'bcrypt'
|
||||
|
||||
module Devise
|
||||
module Models
|
||||
# Authenticable Module, responsible for encrypting password and validating
|
||||
# authenticity of a user while signing in.
|
||||
#
|
||||
# == Options
|
||||
# Configuration:
|
||||
#
|
||||
# DatabaseAuthenticable adds the following options to devise_for:
|
||||
# You can overwrite configuration values by setting in globally in Devise,
|
||||
# using devise method or overwriting the respective instance method.
|
||||
#
|
||||
# * +stretches+: the cost given to bcrypt.
|
||||
# pepper: encryption key used for creating encrypted password. Each time
|
||||
# password changes, it's gonna be encrypted again, and this key
|
||||
# is added to the password and salt to create a secure hash.
|
||||
# Always use `rake secret' to generate a new key.
|
||||
#
|
||||
# == Examples
|
||||
# stretches: defines how many times the password will be encrypted.
|
||||
#
|
||||
# encryptor: the encryptor going to be used. By default :sha1.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# User.find(1).valid_password?('password123') # returns true/false
|
||||
#
|
||||
@@ -22,18 +29,22 @@ module Devise
|
||||
included do
|
||||
attr_reader :password, :current_password
|
||||
attr_accessor :password_confirmation
|
||||
before_save :downcase_keys
|
||||
end
|
||||
|
||||
# Generates password encryption based on the given value.
|
||||
# Regenerates password salt and encrypted password each time password is set,
|
||||
# and then trigger any "after_changed_password"-callbacks.
|
||||
def password=(new_password)
|
||||
@password = new_password
|
||||
self.encrypted_password = password_digest(@password) if @password.present?
|
||||
|
||||
if @password.present?
|
||||
self.password_salt = self.class.password_salt
|
||||
self.encrypted_password = password_digest(@password)
|
||||
end
|
||||
end
|
||||
|
||||
# Verifies whether an incoming_password (ie from sign in) is the user password.
|
||||
def valid_password?(password)
|
||||
::BCrypt::Password.new(self.encrypted_password) == "#{password}#{self.class.pepper}"
|
||||
def valid_password?(incoming_password)
|
||||
password_digest(incoming_password) == self.encrypted_password
|
||||
end
|
||||
|
||||
# Set password and password confirmation to nil
|
||||
@@ -67,25 +78,26 @@ module Devise
|
||||
def after_database_authentication
|
||||
end
|
||||
|
||||
# A reliable way to expose the salt regardless of the implementation.
|
||||
def authenticatable_salt
|
||||
self.encrypted_password[0,29] if self.encrypted_password
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Downcase case-insensitive keys
|
||||
def downcase_keys
|
||||
self.class.case_insensitive_keys.each { |k| self[k].try(:downcase!) }
|
||||
end
|
||||
|
||||
# Digests the password using bcrypt.
|
||||
# Digests the password using the configured encryptor.
|
||||
def password_digest(password)
|
||||
::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
|
||||
if self.password_salt.present?
|
||||
self.class.encryptor_class.digest(password, self.class.stretches, self.password_salt, self.class.pepper)
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
Devise::Models.config(self, :pepper, :stretches)
|
||||
Devise::Models.config(self, :pepper, :stretches, :encryptor)
|
||||
|
||||
# Returns the class for the configured encryptor.
|
||||
def encryptor_class
|
||||
@encryptor_class ||= ::Devise::Encryptors.const_get(encryptor.to_s.classify)
|
||||
end
|
||||
|
||||
def password_salt
|
||||
self.encryptor_class.salt(self.stretches)
|
||||
end
|
||||
|
||||
# We assume this method already gets the sanitized values from the
|
||||
# DatabaseAuthenticatable strategy. If you are using this method on
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
require 'devise/strategies/database_authenticatable'
|
||||
|
||||
module Devise
|
||||
module Models
|
||||
# Encryptable Module adds support to several encryptors.
|
||||
#
|
||||
# == Options
|
||||
#
|
||||
# Encryptable adds the following options to devise_for:
|
||||
#
|
||||
# * +pepper+: a random string used to provide a more secure hash.
|
||||
#
|
||||
# * +encryptor+: the encryptor going to be used. By default is nil.
|
||||
#
|
||||
# == Examples
|
||||
#
|
||||
# User.find(1).valid_password?('password123') # returns true/false
|
||||
#
|
||||
module Encryptable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
attr_reader :password, :current_password
|
||||
attr_accessor :password_confirmation
|
||||
end
|
||||
|
||||
# Generates password salt.
|
||||
def password=(new_password)
|
||||
self.password_salt = self.class.password_salt if new_password.present?
|
||||
super
|
||||
end
|
||||
|
||||
def authenticatable_salt
|
||||
self.password_salt
|
||||
end
|
||||
|
||||
# Verifies whether an incoming_password (ie from sign in) is the user password.
|
||||
def valid_password?(incoming_password)
|
||||
password_digest(incoming_password) == self.encrypted_password
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Digests the password using the configured encryptor.
|
||||
def password_digest(password)
|
||||
if self.password_salt.present?
|
||||
self.class.encryptor_class.digest(password, self.class.stretches, self.password_salt, self.class.pepper)
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
Devise::Models.config(self, :encryptor)
|
||||
|
||||
# Returns the class for the configured encryptor.
|
||||
def encryptor_class
|
||||
@encryptor_class ||= ::Devise::Encryptors.const_get(encryptor.to_s.classify)
|
||||
end
|
||||
|
||||
def password_salt
|
||||
self.encryptor_class.salt(self.stretches)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,14 +7,13 @@ module Devise
|
||||
# will unlock the user automatically after some configured time (ie 2.hours).
|
||||
# It's also possible to setup lockable to use both email and time strategies.
|
||||
#
|
||||
# == Options
|
||||
# Configuration:
|
||||
#
|
||||
# Lockable adds the following options to devise_for:
|
||||
#
|
||||
# * +maximum_attempts+: how many attempts should be accepted before blocking the user.
|
||||
# * +lock_strategy+: lock the user account by :failed_attempts or :none.
|
||||
# * +unlock_strategy+: unlock the user account by :time, :email, :both or :none.
|
||||
# * +unlock_in+: the time you want to lock the user after to lock happens. Only available when unlock_strategy is :time or :both.
|
||||
# maximum_attempts: how many attempts should be accepted before blocking the user.
|
||||
# lock_strategy: lock the user account by :failed_attempts or :none.
|
||||
# unlock_strategy: unlock the user account by :time, :email, :both or :none.
|
||||
# unlock_in: the time you want to lock the user after to lock happens. Only
|
||||
# available when unlock_strategy is :time or :both.
|
||||
#
|
||||
module Lockable
|
||||
extend ActiveSupport::Concern
|
||||
@@ -132,7 +131,7 @@ module Devise
|
||||
# with an email not found error.
|
||||
# Options must contain the user email
|
||||
def send_unlock_instructions(attributes={})
|
||||
lockable = find_or_initialize_with_errors(authentication_keys, attributes, :not_found)
|
||||
lockable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
|
||||
lockable.resend_unlock_token if lockable.persisted?
|
||||
lockable
|
||||
end
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
require 'devise/omniauth'
|
||||
|
||||
module Devise
|
||||
module Models
|
||||
# Adds OmniAuth support to your model.
|
||||
#
|
||||
# == Options
|
||||
#
|
||||
# Oauthable adds the following options to devise_for:
|
||||
#
|
||||
# * +omniauth_providers+: Which providers are avaialble to this model. It expects an array:
|
||||
#
|
||||
# devise_for :database_authenticatable, :omniauthable, :omniauth_providers => [:twitter]
|
||||
#
|
||||
module Omniauthable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
Devise::Models.config(self, :omniauth_providers)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,9 +1,8 @@
|
||||
module Devise
|
||||
module Models
|
||||
|
||||
# Recoverable takes care of reseting the user password and send reset instructions.
|
||||
#
|
||||
# == Examples
|
||||
# Recoverable takes care of reseting the user password and send reset instructions
|
||||
# Examples:
|
||||
#
|
||||
# # resets the user password and save the record, true if valid passwords are given, otherwise false
|
||||
# User.find(1).reset_password!('password123', 'password123')
|
||||
@@ -14,7 +13,6 @@ module Devise
|
||||
#
|
||||
# # creates a new token and send it with instructions about how to reset the password
|
||||
# User.find(1).send_reset_password_instructions
|
||||
#
|
||||
module Recoverable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
@@ -57,7 +55,7 @@ module Devise
|
||||
# with an email not found error.
|
||||
# Attributes must contain the user email
|
||||
def send_reset_password_instructions(attributes={})
|
||||
recoverable = find_or_initialize_with_errors(authentication_keys, attributes, :not_found)
|
||||
recoverable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
|
||||
recoverable.send_reset_password_instructions if recoverable.persisted?
|
||||
recoverable
|
||||
end
|
||||
|
||||
@@ -3,19 +3,6 @@ module Devise
|
||||
# Registerable is responsible for everything related to registering a new
|
||||
# resource (ie user sign up).
|
||||
module Registerable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
# A convenience method that receives both parameters and session to
|
||||
# initialize an user. This can be used by OAuth, for example, to send
|
||||
# in the user token and be stored on initialization.
|
||||
#
|
||||
# By default discards all information sent by the session by calling
|
||||
# new with params.
|
||||
def new_with_session(params, session)
|
||||
new(params)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,27 +11,24 @@ module Devise
|
||||
# You probably wouldn't use rememberable methods directly, they are used
|
||||
# mostly internally for handling the remember token.
|
||||
#
|
||||
# == Options
|
||||
# Configuration:
|
||||
#
|
||||
# Rememberable adds the following options in devise_for:
|
||||
# remember_for: the time you want the user will be remembered without
|
||||
# asking for credentials. After this time the user will be
|
||||
# blocked and will have to enter his credentials again.
|
||||
# This configuration is also used to calculate the expires
|
||||
# time for the cookie created to remember the user.
|
||||
# 2.weeks by default.
|
||||
#
|
||||
# * +remember_for+: the time you want the user will be remembered without
|
||||
# asking for credentials. After this time the user will be blocked and
|
||||
# will have to enter his credentials again. This configuration is also
|
||||
# used to calculate the expires time for the cookie created to remember
|
||||
# the user. By default remember_for is 2.weeks.
|
||||
# remember_across_browsers: if true, a valid remember token can be
|
||||
# re-used between multiple browsers.
|
||||
# True by default.
|
||||
#
|
||||
# * +remember_across_browsers+: if a valid remember token can be re-used
|
||||
# between multiple browsers. By default remember_across_browsers is true
|
||||
# and cannot be turned off if you are using password salt instead of remember
|
||||
# token.
|
||||
# extend_remember_period: if true, extends the user's remember period
|
||||
# when remembered via cookie.
|
||||
# False by default.
|
||||
#
|
||||
# * +extend_remember_period+: if true, extends the user's remember period
|
||||
# when remembered via cookie. False by default.
|
||||
#
|
||||
# * +cookie_options+: configuration options passed to the created cookie.
|
||||
#
|
||||
# == Examples
|
||||
# Examples:
|
||||
#
|
||||
# User.find(1).remember_me! # regenerating the token
|
||||
# User.find(1).forget_me! # clearing the token
|
||||
@@ -52,7 +49,7 @@ module Devise
|
||||
# Generate a new remember token and save the record without validations
|
||||
# unless remember_across_browsers is true and the user already has a valid token.
|
||||
def remember_me!(extend_period=false)
|
||||
self.remember_token = self.class.remember_token if respond_to?(:remember_token) && generate_remember_token?
|
||||
self.remember_token = self.class.remember_token if generate_remember_token?
|
||||
self.remember_created_at = Time.now.utc if generate_remember_timestamp?(extend_period)
|
||||
save(:validate => false)
|
||||
end
|
||||
@@ -60,14 +57,16 @@ module Devise
|
||||
# Removes the remember token only if it exists, and save the record
|
||||
# without validations.
|
||||
def forget_me!
|
||||
self.remember_token = nil if respond_to?(:remember_token)
|
||||
self.remember_created_at = nil
|
||||
save(:validate => false)
|
||||
if remember_token
|
||||
self.remember_token = nil
|
||||
self.remember_created_at = nil
|
||||
save(:validate => false)
|
||||
end
|
||||
end
|
||||
|
||||
# Remember token should be expired if expiration time not overpass now.
|
||||
def remember_expired?
|
||||
remember_created_at.nil? || (remember_expires_at <= Time.now.utc)
|
||||
remember_created_at && (remember_expires_at <= Time.now.utc)
|
||||
end
|
||||
|
||||
# Remember token expires at created time + remember_for configuration
|
||||
@@ -75,20 +74,12 @@ module Devise
|
||||
remember_created_at + self.class.remember_for
|
||||
end
|
||||
|
||||
def rememberable_value
|
||||
if respond_to?(:remember_token)
|
||||
remember_token
|
||||
elsif respond_to?(:authenticatable_salt) && (salt = authenticatable_salt)
|
||||
salt
|
||||
else
|
||||
raise "The #{self.class.name} class does not respond to remember_token and " <<
|
||||
"authenticatable_salt returns nil. In order to use rememberable, you must " <<
|
||||
"add a remember_token field to your model or ensure a password is always set."
|
||||
end
|
||||
def cookie_domain
|
||||
self.class.cookie_domain
|
||||
end
|
||||
|
||||
def cookie_options
|
||||
self.class.cookie_options
|
||||
def cookie_domain?
|
||||
self.class.cookie_domain != false
|
||||
end
|
||||
|
||||
protected
|
||||
@@ -108,13 +99,14 @@ module Devise
|
||||
module ClassMethods
|
||||
# Create the cookie key using the record id and remember_token
|
||||
def serialize_into_cookie(record)
|
||||
[record.to_key, record.rememberable_value]
|
||||
[record.id, record.remember_token]
|
||||
end
|
||||
|
||||
# Recreate the user based on the stored cookie
|
||||
def serialize_from_cookie(id, remember_token)
|
||||
record = to_adapter.get(id)
|
||||
record if record && record.rememberable_value == remember_token && !record.remember_expired?
|
||||
conditions = { :id => id, :remember_token => remember_token }
|
||||
record = find(:first, :conditions => conditions)
|
||||
record if record && !record.remember_expired?
|
||||
end
|
||||
|
||||
# Generate a token checking if one does not already exist in the database.
|
||||
@@ -123,7 +115,7 @@ module Devise
|
||||
end
|
||||
|
||||
Devise::Models.config(self, :remember_for, :remember_across_browsers,
|
||||
:extend_remember_period, :cookie_options)
|
||||
:extend_remember_period, :cookie_domain)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,34 +7,17 @@ module Devise
|
||||
# will be asked for credentials again, it means, he/she will be redirected
|
||||
# to the sign in page.
|
||||
#
|
||||
# == Options
|
||||
#
|
||||
# Timeoutable adds the following options to devise_for:
|
||||
#
|
||||
# * +timeout_in+: the interval to timeout the user session without activity.
|
||||
#
|
||||
# == Examples
|
||||
#
|
||||
# user.timedout?(30.minutes.ago)
|
||||
# Configuration:
|
||||
#
|
||||
# timeout_in: the time you want to timeout the user session without activity.
|
||||
module Timeoutable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
# Checks whether the user session has expired based on configured time.
|
||||
def timedout?(last_access)
|
||||
return false if remember_exists_and_not_expired?
|
||||
|
||||
last_access && last_access <= self.class.timeout_in.ago
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def remember_exists_and_not_expired?
|
||||
return false unless respond_to?(:remember_expired?)
|
||||
|
||||
remember_created_at && !remember_expired?
|
||||
end
|
||||
|
||||
|
||||
module ClassMethods
|
||||
Devise::Models.config(self, :timeout_in)
|
||||
end
|
||||
|
||||
@@ -5,27 +5,15 @@ module Devise
|
||||
# The TokenAuthenticatable module is responsible for generating an authentication token and
|
||||
# validating the authenticity of the same while signing in.
|
||||
#
|
||||
# This module only provides a few helpers to help you manage the token, but it is up to you
|
||||
# to choose how to use it. For example, if you want to have a new token every time the user
|
||||
# saves his account, you can do the following:
|
||||
# This module only provides a few helpers to help you manage the token. Creating and resetting
|
||||
# the token is your responsibility.
|
||||
#
|
||||
# before_save :reset_authentication_token
|
||||
# == Configuration:
|
||||
#
|
||||
# On the other hand, if you want to generate token unless one exists, you should use instead:
|
||||
# You can overwrite configuration values by setting in globally in Devise (+Devise.setup+),
|
||||
# using devise method, or overwriting the respective instance method.
|
||||
#
|
||||
# before_save :ensure_authentication_token
|
||||
#
|
||||
# If you want to delete the token after it is used, you can do so in the
|
||||
# after_token_authentication callback.
|
||||
#
|
||||
# == Options
|
||||
#
|
||||
# TokenAuthenticable adds the following options to devise_for:
|
||||
#
|
||||
# * +token_authentication_key+: Defines name of the authentication token params key. E.g. /users/sign_in?some_key=...
|
||||
#
|
||||
# * +stateless_token+: By default, when you sign up with a token, Devise will store the user in session
|
||||
# as any other authentication strategy. You can set stateless_token to true to avoid this.
|
||||
# +token_authentication_key+ - Defines name of the authentication token params key. E.g. /users/sign_in?some_key=...
|
||||
#
|
||||
module TokenAuthenticatable
|
||||
extend ActiveSupport::Concern
|
||||
@@ -65,7 +53,7 @@ module Devise
|
||||
generate_token(:authentication_token)
|
||||
end
|
||||
|
||||
::Devise::Models.config(self, :token_authentication_key, :stateless_token)
|
||||
::Devise::Models.config(self, :token_authentication_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,17 +1,10 @@
|
||||
module Devise
|
||||
module Models
|
||||
|
||||
# Validatable creates all needed validations for a user email and password.
|
||||
# It's optional, given you may want to create the validations by yourself.
|
||||
# Automatically validate if the email is present, unique and it's format is
|
||||
# valid. Also tests presence of password, confirmation and length.
|
||||
#
|
||||
# == Options
|
||||
#
|
||||
# Validatable adds the following options to devise_for:
|
||||
#
|
||||
# * +email_regexp+: the regular expression used to validate e-mails;
|
||||
# * +password_length+: a range expressing password length. Defaults to 6..20.
|
||||
#
|
||||
# valid. Also tests presence of password, confirmation and length
|
||||
module Validatable
|
||||
# All validations used by this module.
|
||||
VALIDATIONS = [ :validates_presence_of, :validates_uniqueness_of, :validates_format_of,
|
||||
@@ -22,9 +15,8 @@ module Devise
|
||||
assert_validations_api!(base)
|
||||
|
||||
base.class_eval do
|
||||
validates_presence_of :email, :if => :email_required?
|
||||
validates_uniqueness_of :email, :scope => authentication_keys[1..-1],
|
||||
:case_sensitive => case_insensitive_keys.exclude?(:email), :allow_blank => true
|
||||
validates_presence_of :email
|
||||
validates_uniqueness_of :email, :scope => authentication_keys[1..-1], :case_sensitive => false, :allow_blank => true
|
||||
validates_format_of :email, :with => email_regexp, :allow_blank => true
|
||||
|
||||
with_options :if => :password_required? do |v|
|
||||
@@ -53,10 +45,6 @@ module Devise
|
||||
!persisted? || !password.nil? || !password_confirmation.nil?
|
||||
end
|
||||
|
||||
def email_required?
|
||||
true
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
Devise::Models.config(self, :email_regexp, :password_length)
|
||||
end
|
||||
|
||||
@@ -2,27 +2,20 @@ require 'active_support/core_ext/object/with_options'
|
||||
|
||||
Devise.with_options :model => true do |d|
|
||||
# Strategies first
|
||||
d.with_options :strategy => true do |s|
|
||||
routes = [nil, :new, :destroy]
|
||||
s.add_module :database_authenticatable, :controller => :sessions, :route => { :session => routes }
|
||||
s.add_module :token_authenticatable, :controller => :sessions, :route => { :session => routes }
|
||||
d.with_options :strategy => true do |s|
|
||||
s.add_module :database_authenticatable, :controller => :sessions, :route => :session
|
||||
s.add_module :token_authenticatable, :controller => :sessions, :route => :session
|
||||
s.add_module :rememberable
|
||||
end
|
||||
|
||||
# Other authentications
|
||||
d.add_module :encryptable
|
||||
d.add_module :omniauthable, :controller => :omniauth_callbacks, :route => :omniauth_callback
|
||||
|
||||
# Misc after
|
||||
routes = [nil, :new, :edit]
|
||||
d.add_module :recoverable, :controller => :passwords, :route => { :password => routes }
|
||||
d.add_module :registerable, :controller => :registrations, :route => { :registration => (routes << :cancel) }
|
||||
# Misc after
|
||||
d.add_module :recoverable, :controller => :passwords, :route => :password
|
||||
d.add_module :registerable, :controller => :registrations, :route => :registration
|
||||
d.add_module :validatable
|
||||
|
||||
# The ones which can sign out after
|
||||
routes = [nil, :new]
|
||||
d.add_module :confirmable, :controller => :confirmations, :route => { :confirmation => routes }
|
||||
d.add_module :lockable, :controller => :unlocks, :route => { :unlock => routes }
|
||||
d.add_module :confirmable, :controller => :confirmations, :route => :confirmation
|
||||
d.add_module :lockable, :controller => :unlocks, :route => :unlock
|
||||
d.add_module :timeoutable
|
||||
|
||||
# Stats for last, so we make sure the user is really signed in
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
begin
|
||||
require "omniauth/core"
|
||||
rescue LoadError => e
|
||||
warn "Could not load 'omniauth/core'. Please ensure you have the oa-core gem installed and listed in your Gemfile."
|
||||
raise
|
||||
end
|
||||
|
||||
module OmniAuth
|
||||
# TODO HAXES Backport to OmniAuth
|
||||
module Strategy #:nodoc:
|
||||
def initialize(app, name, *args)
|
||||
@app = app
|
||||
@name = name.to_sym
|
||||
@options = args.last.is_a?(Hash) ? args.pop : {}
|
||||
yield self if block_given?
|
||||
end
|
||||
|
||||
def fail!(message_key, exception = nil)
|
||||
self.env['omniauth.error'] = exception
|
||||
self.env['omniauth.failure_key'] = message_key
|
||||
self.env['omniauth.failed_strategy'] = self
|
||||
OmniAuth.config.on_failure.call(self.env, message_key.to_sym)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Clean up the default path_prefix. It will be automatically set by Devise.
|
||||
OmniAuth.config.path_prefix = nil
|
||||
|
||||
OmniAuth.config.on_failure = Proc.new do |env, key|
|
||||
env['devise.mapping'] = Devise::Mapping.find_by_path!(env['PATH_INFO'], :path)
|
||||
controller_klass = "#{env['devise.mapping'].controllers[:omniauth_callbacks].camelize}Controller"
|
||||
controller_klass.constantize.action(:failure).call(env)
|
||||
end
|
||||
|
||||
module Devise
|
||||
module OmniAuth
|
||||
autoload :Config, "devise/omniauth/config"
|
||||
autoload :UrlHelpers, "devise/omniauth/url_helpers"
|
||||
autoload :TestHelpers, "devise/omniauth/test_helpers"
|
||||
|
||||
class << self
|
||||
delegate :short_circuit_authorizers!, :unshort_circuit_authorizers!,
|
||||
:test_mode!, :stub!, :reset_stubs!, :to => "Devise::OmniAuth::TestHelpers"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,30 +0,0 @@
|
||||
module Devise
|
||||
module OmniAuth
|
||||
class Config
|
||||
attr_accessor :strategy
|
||||
attr_reader :args
|
||||
|
||||
def initialize(provider, args)
|
||||
@provider = provider
|
||||
@args = args
|
||||
@strategy = nil
|
||||
end
|
||||
|
||||
def strategy_class
|
||||
::OmniAuth::Strategies.const_get("#{::OmniAuth::Utils.camelize(@provider.to_s)}")
|
||||
end
|
||||
|
||||
def check_if_allow_stubs!
|
||||
raise "OmniAuth strategy for #{@provider} does not allow stubs, only OAuth2 ones do." unless allow_stubs?
|
||||
end
|
||||
|
||||
def allow_stubs?
|
||||
defined?(::OmniAuth::Strategies::OAuth2) && strategy.is_a?(::OmniAuth::Strategies::OAuth2)
|
||||
end
|
||||
|
||||
def build_connection(&block)
|
||||
strategy.client.connection.build(&block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,57 +0,0 @@
|
||||
module Devise
|
||||
module OmniAuth
|
||||
module TestHelpers
|
||||
def self.test_mode!
|
||||
Faraday.default_adapter = :test if defined?(Faraday)
|
||||
ActiveSupport.on_load(:action_controller) { include Devise::OmniAuth::TestHelpers }
|
||||
ActiveSupport.on_load(:action_view) { include Devise::OmniAuth::TestHelpers }
|
||||
end
|
||||
|
||||
def self.stub!(provider, stubs=nil, &block)
|
||||
raise "You either need to pass stubs as a block or as a parameter" unless block_given? || stubs
|
||||
|
||||
config = Devise.omniauth_configs[provider]
|
||||
raise "Could not find configuration for #{provider.to_s} omniauth provider" unless config
|
||||
|
||||
config.check_if_allow_stubs!
|
||||
stubs ||= Faraday::Adapter::Test::Stubs.new(&block)
|
||||
|
||||
config.build_connection do |b|
|
||||
b.adapter :test, stubs
|
||||
end
|
||||
end
|
||||
|
||||
def self.reset_stubs!(*providers)
|
||||
target = providers.any? ? Devise.omniauth_configs.slice(*providers) : Devise.omniauth_configs
|
||||
target.each_value do |config|
|
||||
next unless config.allow_stubs?
|
||||
config.build_connection { |b| b.adapter Faraday.default_adapter }
|
||||
end
|
||||
end
|
||||
|
||||
def self.short_circuit_authorizers!
|
||||
module_eval <<-ALIASES, __FILE__, __LINE__ + 1
|
||||
def omniauth_authorize_path(*args)
|
||||
omniauth_callback_path(*args)
|
||||
end
|
||||
ALIASES
|
||||
|
||||
Devise.mappings.each_value do |m|
|
||||
next unless m.omniauthable?
|
||||
|
||||
module_eval <<-ALIASES, __FILE__, __LINE__ + 1
|
||||
def #{m.name}_omniauth_authorize_path(provider)
|
||||
#{m.name}_omniauth_callback_path(provider)
|
||||
end
|
||||
ALIASES
|
||||
end
|
||||
end
|
||||
|
||||
def self.unshort_circuit_authorizers!
|
||||
module_eval do
|
||||
instance_methods.each { |m| remove_method(m) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,29 +0,0 @@
|
||||
module Devise
|
||||
module OmniAuth
|
||||
module UrlHelpers
|
||||
def self.define_helpers(mapping)
|
||||
return unless mapping.omniauthable?
|
||||
|
||||
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
||||
def #{mapping.name}_omniauth_authorize_path(provider, params = {})
|
||||
if Devise.omniauth_configs[provider.to_sym]
|
||||
"/#{mapping.path}/auth/\#{provider}\#{'?'+params.to_param if params.present?}"
|
||||
else
|
||||
raise ArgumentError, "Could not find omniauth provider \#{provider.inspect}"
|
||||
end
|
||||
end
|
||||
URL_HELPERS
|
||||
end
|
||||
|
||||
def omniauth_authorize_path(resource_or_scope, *args)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
send("#{scope}_omniauth_authorize_path", *args)
|
||||
end
|
||||
|
||||
def omniauth_callback_path(resource_or_scope, *args)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
send("#{scope}_omniauth_callback_path", *args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,3 @@
|
||||
require 'orm_adapter/adapters/active_record'
|
||||
|
||||
module Devise
|
||||
module Orm
|
||||
# This module contains some helpers and handle schema (migrations):
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
require 'orm_adapter/adapters/mongoid'
|
||||
|
||||
module Devise
|
||||
module Orm
|
||||
module Mongoid
|
||||
@@ -18,7 +16,7 @@ module Devise
|
||||
# Tell how to apply schema methods
|
||||
def apply_devise_schema(name, type, options={})
|
||||
type = Time if type == DateTime
|
||||
field name, { :type => type }.merge!(options)
|
||||
field name, { :type => type }.merge(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -28,4 +26,4 @@ end
|
||||
Mongoid::Document::ClassMethods.class_eval do
|
||||
include Devise::Models
|
||||
include Devise::Orm::Mongoid::Hook
|
||||
end
|
||||
end
|
||||
@@ -1,11 +1,14 @@
|
||||
require 'devise/rails/routes'
|
||||
require 'devise/rails/warden_compat'
|
||||
|
||||
# Include UrlHelpers in ActionController and ActionView as soon as they are loaded.
|
||||
ActiveSupport.on_load(:action_controller) { include Devise::Controllers::UrlHelpers }
|
||||
ActiveSupport.on_load(:action_view) { include Devise::Controllers::UrlHelpers }
|
||||
|
||||
module Devise
|
||||
class Engine < ::Rails::Engine
|
||||
config.devise = Devise
|
||||
|
||||
# Initialize Warden and copy its configurations.
|
||||
config.app_middleware.use Warden::Manager do |config|
|
||||
Devise.warden_config = config
|
||||
end
|
||||
@@ -13,37 +16,54 @@ module Devise
|
||||
# Force routes to be loaded if we are doing any eager load.
|
||||
config.before_eager_load { |app| app.reload_routes! }
|
||||
|
||||
initializer "devise.url_helpers" do
|
||||
Devise.include_helpers(Devise::Controllers)
|
||||
config.after_initialize do
|
||||
Devise.encryptor ||= begin
|
||||
warn "[WARNING] config.encryptor is not set in your config/initializers/devise.rb. " \
|
||||
"Devise will then set it to :bcrypt. If you were using the previous default " \
|
||||
"encryptor, please add config.encryptor = :sha1 to your configuration file." if Devise.mailer_sender
|
||||
:bcrypt
|
||||
end
|
||||
end
|
||||
|
||||
initializer "devise.omniauth" do |app|
|
||||
Devise.omniauth_configs.each do |provider, config|
|
||||
app.middleware.use config.strategy_class, *config.args do |strategy|
|
||||
config.strategy = strategy
|
||||
initializer "devise.add_filters" do |app|
|
||||
app.config.filter_parameters += [:password, :password_confirmation]
|
||||
app.config.filter_parameters.uniq
|
||||
end
|
||||
|
||||
unless Rails.env.production?
|
||||
config.after_initialize do
|
||||
actions = [:confirmation_instructions, :reset_password_instructions, :unlock_instructions]
|
||||
|
||||
translations = begin
|
||||
I18n.t("devise.mailer", :raise => true).map { |k, v| k if v.is_a?(String) }.compact
|
||||
rescue Exception => e # Do not care if something fails
|
||||
[]
|
||||
end
|
||||
|
||||
keys = actions & translations
|
||||
|
||||
keys.each do |key|
|
||||
ActiveSupport::Deprecation.warn "The I18n message 'devise.mailer.#{key}' is deprecated. " \
|
||||
"Please use 'devise.mailer.#{key}.subject' instead."
|
||||
end
|
||||
end
|
||||
|
||||
if Devise.omniauth_configs.any?
|
||||
Devise.include_helpers(Devise::OmniAuth)
|
||||
end
|
||||
end
|
||||
config.after_initialize do
|
||||
flash = [:unauthenticated, :unconfirmed, :invalid, :invalid_token, :timeout, :inactive, :locked]
|
||||
|
||||
initializer "devise.encryptor_check" do
|
||||
case Devise.encryptor
|
||||
when :bcrypt
|
||||
puts "[DEVISE] From version 1.2, there is no need to set your encryptor to bcrypt " <<
|
||||
"since encryptors are only enabled if you include :encryptable in your models. " <<
|
||||
"With this change, we can integrate better with bcrypt and get rid of the " <<
|
||||
"password_salt column (since bcrypt stores the salt with password). " <<
|
||||
"Please comment config.encryptor in your initializer to get rid of this warning."
|
||||
when nil
|
||||
# Nothing to say
|
||||
else
|
||||
puts "[DEVISE] You are using #{Devise.encryptor} as encryptor. From version 1.2, " <<
|
||||
"you need to explicitly add `devise :encryptable, :encryptor => #{Devise.encryptor.to_sym}` " <<
|
||||
"to your models and comment the current value in the config/initializers/devise.rb"
|
||||
translations = begin
|
||||
I18n.t("devise.sessions", :raise => true).keys
|
||||
rescue Exception => e # Do not care if something fails
|
||||
[]
|
||||
end
|
||||
|
||||
keys = flash & translations
|
||||
|
||||
if keys.any?
|
||||
ActiveSupport::Deprecation.warn "The following I18n messages in 'devise.sessions' " \
|
||||
"are deprecated: #{keys.to_sentence}. Please move them to 'devise.failure' instead."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,6 +5,7 @@ module ActionDispatch::Routing
|
||||
def finalize_with_devise!
|
||||
finalize_without_devise!
|
||||
Devise.configure_warden!
|
||||
ActionController::Base.send :include, Devise::Controllers::Helpers
|
||||
end
|
||||
alias_method_chain :finalize!, :devise
|
||||
end
|
||||
@@ -69,13 +70,6 @@ module ActionDispatch::Routing
|
||||
#
|
||||
# devise_for :users, :controllers => { :sessions => "users/sessions" }
|
||||
#
|
||||
# * :sign_out_via => the HTTP method(s) accepted for the :sign_out action (default: :get),
|
||||
# if you wish to restrict this to accept only :post or :delete requests you should do:
|
||||
#
|
||||
# devise_for :users, :sign_out_via => [ :post, :delete ]
|
||||
#
|
||||
# You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
|
||||
#
|
||||
# * :module => the namespace to find controlers. By default, devise will access devise/sessions,
|
||||
# devise/registrations and so on. If you want to namespace all at once, use module:
|
||||
#
|
||||
@@ -91,10 +85,6 @@ module ActionDispatch::Routing
|
||||
# Will use publisher/sessions controller instead of devise/sessions controller. You can revert
|
||||
# this by providing the :module option to devise_for.
|
||||
#
|
||||
# Also pay attention that when you use a namespace it will affect all the helpers and methods for controllers
|
||||
# and views. For example, using the above setup you'll end with following methods:
|
||||
# current_publisher_account, authenticate_publisher_account!, pusblisher_account_signed_in, etc.
|
||||
#
|
||||
# * :skip => tell which controller you want to skip routes from being created:
|
||||
#
|
||||
# devise_for :users, :skip => :sessions
|
||||
@@ -123,33 +113,19 @@ module ActionDispatch::Routing
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# ==== Adding custom actions to override controllers
|
||||
#
|
||||
# You can pass a block to devise_for that will add any routes defined in the block to Devise's
|
||||
# list of known actions. This is important if you add a custom action to a controller that
|
||||
# overrides an out of the box Devise controller.
|
||||
# For example:
|
||||
#
|
||||
# class RegistrationsController < Devise::RegistrationsController
|
||||
# def update
|
||||
# # do something different here
|
||||
# end
|
||||
#
|
||||
# def deactivate
|
||||
# # not a standard action
|
||||
# # deactivate code here
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# In order to get Devise to recognize the deactivate action, your devise_for entry should look like this,
|
||||
#
|
||||
# devise_for :owners, :controllers => { :registrations => "registrations" } do
|
||||
# post "deactivate", :to => "registrations#deactivate", :as => "deactivate_registration"
|
||||
# end
|
||||
#
|
||||
def devise_for(*resources)
|
||||
options = resources.extract_options!
|
||||
|
||||
if as = options.delete(:as)
|
||||
ActiveSupport::Deprecation.warn ":as is deprecated, please use :path instead."
|
||||
options[:path] ||= as
|
||||
end
|
||||
|
||||
if scope = options.delete(:scope)
|
||||
ActiveSupport::Deprecation.warn ":scope is deprecated, please use :singular instead."
|
||||
options[:singular] ||= scope
|
||||
end
|
||||
|
||||
options[:as] ||= @scope[:as] if @scope[:as].present?
|
||||
options[:module] ||= @scope[:module] if @scope[:module].present?
|
||||
options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
|
||||
@@ -178,7 +154,7 @@ module ActionDispatch::Routing
|
||||
devise_scope mapping.name do
|
||||
yield if block_given?
|
||||
with_devise_exclusive_scope mapping.fullpath, mapping.name do
|
||||
routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
|
||||
routes.each { |mod| send(:"devise_#{mod}", mapping, mapping.controllers) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -227,22 +203,22 @@ module ActionDispatch::Routing
|
||||
|
||||
def devise_session(mapping, controllers) #:nodoc:
|
||||
resource :session, :only => [], :controller => controllers[:sessions], :path => "" do
|
||||
get :new, :path => mapping.path_names[:sign_in], :as => "new"
|
||||
post :create, :path => mapping.path_names[:sign_in]
|
||||
match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via
|
||||
get :new, :path => mapping.path_names[:sign_in], :as => "new"
|
||||
post :create, :path => mapping.path_names[:sign_in]
|
||||
get :destroy, :path => mapping.path_names[:sign_out], :as => "destroy"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def devise_password(mapping, controllers) #:nodoc:
|
||||
resource :password, :only => [:new, :create, :edit, :update],
|
||||
:path => mapping.path_names[:password], :controller => controllers[:passwords]
|
||||
end
|
||||
|
||||
|
||||
def devise_confirmation(mapping, controllers) #:nodoc:
|
||||
resource :confirmation, :only => [:new, :create, :show],
|
||||
:path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
|
||||
end
|
||||
|
||||
|
||||
def devise_unlock(mapping, controllers) #:nodoc:
|
||||
if mapping.to.unlock_strategy_enabled?(:email)
|
||||
resource :unlock, :only => [:new, :create, :show],
|
||||
@@ -251,28 +227,8 @@ module ActionDispatch::Routing
|
||||
end
|
||||
|
||||
def devise_registration(mapping, controllers) #:nodoc:
|
||||
path_names = {
|
||||
:new => mapping.path_names[:sign_up],
|
||||
:cancel => mapping.path_names[:cancel]
|
||||
}
|
||||
|
||||
resource :registration, :except => :show, :path => mapping.path_names[:registration],
|
||||
:path_names => path_names, :controller => controllers[:registrations] do
|
||||
get :cancel
|
||||
end
|
||||
end
|
||||
|
||||
def devise_omniauth_callback(mapping, controllers) #:nodoc:
|
||||
path_prefix = "/#{mapping.path}/auth"
|
||||
|
||||
if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix
|
||||
warn "[DEVISE] You can only add :omniauthable behavior to one model."
|
||||
else
|
||||
::OmniAuth.config.path_prefix = path_prefix
|
||||
end
|
||||
|
||||
match "/auth/:action/callback", :action => Regexp.union(mapping.to.omniauth_providers.map(&:to_s)),
|
||||
:to => controllers[:omniauth_callbacks], :as => :omniauth_callback
|
||||
resource :registration, :only => [:new, :create, :edit, :update, :destroy], :path => mapping.path_names[:registration],
|
||||
:path_names => { :new => mapping.path_names[:sign_up] }, :controller => controllers[:registrations]
|
||||
end
|
||||
|
||||
def with_devise_exclusive_scope(new_path, new_as) #:nodoc:
|
||||
|
||||
@@ -3,9 +3,9 @@ module Warden::Mixins::Common
|
||||
@request ||= ActionDispatch::Request.new(env)
|
||||
end
|
||||
|
||||
# This is called internally by Warden on logout
|
||||
def reset_session!
|
||||
request.reset_session
|
||||
raw_session.inspect # why do I have to inspect it to get it to clear?
|
||||
raw_session.clear
|
||||
end
|
||||
|
||||
def cookies
|
||||
@@ -15,28 +15,25 @@ end
|
||||
|
||||
class Warden::SessionSerializer
|
||||
def serialize(record)
|
||||
[record.class.name, record.to_key, record.authenticatable_salt]
|
||||
[record.class.name, record.id]
|
||||
end
|
||||
|
||||
def deserialize(keys)
|
||||
if keys.size == 2
|
||||
klass, id = keys
|
||||
|
||||
if klass.is_a?(Class)
|
||||
raise "Devise changed how it stores objects in session. If you are seeing this message, " <<
|
||||
"you can fix it by changing one character in your cookie secret or cleaning up your " <<
|
||||
"database sessions if you are using a db store."
|
||||
"you can fix it by changing one character in your cookie secret, forcing all previous " <<
|
||||
"cookies to expire, or cleaning up your database sessions if you are using a db store."
|
||||
end
|
||||
|
||||
klass, id, salt = keys
|
||||
|
||||
begin
|
||||
record = klass.constantize.to_adapter.get(id)
|
||||
record if record && record.authenticatable_salt == salt
|
||||
rescue NameError => e
|
||||
if e.message =~ /uninitialized constant/
|
||||
Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass}"
|
||||
nil
|
||||
else
|
||||
raise
|
||||
end
|
||||
klass.constantize.find(:first, :conditions => { :id => id })
|
||||
rescue NameError => e
|
||||
if e.message =~ /uninitialized constant/
|
||||
Rails.logger.debug "Trying to deserialize invalid class #{klass}"
|
||||
nil
|
||||
else
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -47,6 +44,10 @@ unless Devise.rack_session?
|
||||
alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
|
||||
alias_method :regular_update, :update unless method_defined?(:regular_update)
|
||||
|
||||
def [](key)
|
||||
super(convert_key(key))
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
regular_writer(convert_key(key), value)
|
||||
end
|
||||
@@ -91,6 +92,7 @@ unless Devise.rack_session?
|
||||
def symbolize_keys; to_hash.symbolize_keys end
|
||||
|
||||
def to_options!; self end
|
||||
def to_hash; Hash.new.update(self) end
|
||||
|
||||
protected
|
||||
|
||||
|
||||
@@ -3,6 +3,11 @@ module Devise
|
||||
# and overwrite the apply_schema method.
|
||||
module Schema
|
||||
|
||||
def authenticatable(*args)
|
||||
ActiveSupport::Deprecation.warn "t.authenticatable in migrations is deprecated. Please use t.database_authenticatable instead.", caller
|
||||
database_authenticatable(*args)
|
||||
end
|
||||
|
||||
# Creates email, encrypted_password and password_salt.
|
||||
#
|
||||
# == Options
|
||||
@@ -16,17 +21,17 @@ module Devise
|
||||
null = options[:null] || false
|
||||
default = options.key?(:default) ? options[:default] : ("" if null == false)
|
||||
|
||||
if options.delete(:encryptor)
|
||||
ActiveSupport::Deprecation.warn ":encryptor as option is deprecated, simply remove it."
|
||||
end
|
||||
|
||||
apply_devise_schema :email, String, :null => null, :default => default
|
||||
apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
|
||||
end
|
||||
|
||||
# Creates password salt for encryption support.
|
||||
def encryptable
|
||||
apply_devise_schema :password_salt, String
|
||||
end
|
||||
apply_devise_schema :password_salt, String, :null => null, :default => default
|
||||
end
|
||||
|
||||
# Creates authentication_token.
|
||||
def token_authenticatable
|
||||
def token_authenticatable(options={})
|
||||
apply_devise_schema :authentication_token, String
|
||||
end
|
||||
|
||||
@@ -43,12 +48,8 @@ module Devise
|
||||
end
|
||||
|
||||
# Creates remember_token and remember_created_at.
|
||||
#
|
||||
# == Options
|
||||
# * :use_salt - When true, does not create a remember_token and use password_salt instead.
|
||||
def rememberable(options={})
|
||||
use_salt = options.fetch(:use_salt, Devise.use_salt_as_remember_token)
|
||||
apply_devise_schema :remember_token, String unless use_salt
|
||||
def rememberable
|
||||
apply_devise_schema :remember_token, String
|
||||
apply_devise_schema :remember_created_at, DateTime
|
||||
end
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ module Devise
|
||||
attr_accessor :authentication_hash, :password
|
||||
|
||||
def valid?
|
||||
valid_for_params_auth? || valid_for_http_auth?
|
||||
valid_for_http_auth? || valid_for_params_auth?
|
||||
end
|
||||
|
||||
private
|
||||
@@ -96,17 +96,15 @@ module Devise
|
||||
|
||||
# Helper to decode credentials from HTTP.
|
||||
def decode_credentials
|
||||
return [] unless request.authorization && request.authorization =~ /^Basic (.*)/m
|
||||
return [] unless request.authorization && request.authorization =~ /^Basic (.*)/
|
||||
ActiveSupport::Base64.decode64($1).split(/:/, 2)
|
||||
end
|
||||
|
||||
# Sets the authentication hash and the password from params_auth_hash or http_auth_hash.
|
||||
def with_authentication_hash(auth_values)
|
||||
self.authentication_hash = {}
|
||||
self.password = auth_values[:password]
|
||||
|
||||
parse_authentication_key_values(auth_values, authentication_keys) &&
|
||||
parse_authentication_key_values(request_values, request_keys)
|
||||
def with_authentication_hash(hash)
|
||||
self.authentication_hash = hash.slice(*authentication_keys)
|
||||
self.password = hash[:password]
|
||||
authentication_keys.all?{ |k| authentication_hash[k].present? }
|
||||
end
|
||||
|
||||
# Holds the authentication keys.
|
||||
@@ -114,31 +112,6 @@ module Devise
|
||||
@authentication_keys ||= mapping.to.authentication_keys
|
||||
end
|
||||
|
||||
# Holds request keys.
|
||||
def request_keys
|
||||
@request_keys ||= mapping.to.request_keys
|
||||
end
|
||||
|
||||
# Returns values from the request object.
|
||||
def request_values
|
||||
keys = request_keys.respond_to?(:keys) ? request_keys.keys : request_keys
|
||||
values = keys.map { |k| self.request.send(k) }
|
||||
Hash[keys.zip(values)]
|
||||
end
|
||||
|
||||
# Parse authentication keys considering if they should be enforced or not.
|
||||
def parse_authentication_key_values(hash, keys)
|
||||
keys.each do |key, enforce|
|
||||
value = hash[key].presence
|
||||
if value
|
||||
self.authentication_hash[key] = value
|
||||
else
|
||||
return false unless enforce == false
|
||||
end
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# Holds the authenticatable name for this class. Devise::Strategies::DatabaseAuthenticatable
|
||||
# becomes simply :database.
|
||||
def authenticatable_name
|
||||
|
||||
@@ -10,10 +10,6 @@ module Devise
|
||||
# For HTTP, you can pass the token as username and blank password. Since some clients may require
|
||||
# a password, you can pass "X" as password and it will simply be ignored.
|
||||
class TokenAuthenticatable < Authenticatable
|
||||
def store?
|
||||
!mapping.to.stateless_token
|
||||
end
|
||||
|
||||
def authenticate!
|
||||
resource = mapping.to.find_for_token_authentication(authentication_hash)
|
||||
|
||||
@@ -32,7 +28,7 @@ module Devise
|
||||
true
|
||||
end
|
||||
|
||||
# Do not use remember_me behavior with token.
|
||||
# Do not use remember_me behavir with token.
|
||||
def remember_me?
|
||||
false
|
||||
end
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
module Devise
|
||||
# Devise::TestHelpers provides a facility to test controllers in isolation
|
||||
# when using ActionController::TestCase allowing you to quickly sign_in or
|
||||
# sign_out an user. Do not use Devise::TestHelpers in integration tests.
|
||||
#
|
||||
# Notice you should not test Warden specific behavior (like Warden callbacks)
|
||||
# using Devise::TestHelpers since it is a stub of the actual behavior. Such
|
||||
# callbacks should be tested in your integration suite instead.
|
||||
module TestHelpers
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
@@ -68,7 +61,6 @@ module Devise
|
||||
end
|
||||
|
||||
# sign_in a given resource by storing its keys in the session.
|
||||
# This method bypass any warden authentication callback.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
@@ -82,7 +74,6 @@ module Devise
|
||||
end
|
||||
|
||||
# Sign out a given resource or scope by calling logout on Warden.
|
||||
# This method bypass any warden logout callback.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
@@ -92,8 +83,7 @@ module Devise
|
||||
def sign_out(resource_or_scope)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
@controller.instance_variable_set(:"@current_#{scope}", nil)
|
||||
user = warden.instance_variable_get(:@users).delete(scope)
|
||||
warden.session_serializer.delete(scope, user)
|
||||
warden.logout(scope)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module Devise
|
||||
VERSION = "1.2.rc".freeze
|
||||
VERSION = "1.1.5".freeze
|
||||
end
|
||||
|
||||
@@ -6,7 +6,6 @@ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
|
||||
t.rememberable
|
||||
t.trackable
|
||||
|
||||
# t.encryptable
|
||||
# t.confirmable
|
||||
# t.lockable :lock_strategy => :<%= Devise.lock_strategy %>, :unlock_strategy => :<%= Devise.unlock_strategy %>
|
||||
# t.token_authenticatable
|
||||
|
||||
@@ -4,7 +4,7 @@ module Devise
|
||||
def model_contents
|
||||
<<-CONTENT
|
||||
# Include default devise modules. Others available are:
|
||||
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
|
||||
# :token_authenticatable, :confirmable, :lockable and :timeoutable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :trackable, :validatable
|
||||
|
||||
@@ -12,8 +12,7 @@ CONTENT
|
||||
end
|
||||
|
||||
def model_exists?
|
||||
return @model_exists if instance_variable_defined?(:@model_exists)
|
||||
@model_exists = File.exists?(File.join(destination_root, model_path))
|
||||
File.exists?(File.join(destination_root, model_path))
|
||||
end
|
||||
|
||||
def model_path
|
||||
|
||||
4
lib/generators/devise_install_generator.rb
Normal file
4
lib/generators/devise_install_generator.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
# Remove this file after deprecation
|
||||
if caller.none? { |l| l =~ %r{lib/rails/generators\.rb:(\d+):in `lookup!'$} }
|
||||
warn "[WARNING] `rails g devise_install` is deprecated, please use `rails g devise:install` instead."
|
||||
end
|
||||
4
lib/generators/devise_views_generator.rb
Normal file
4
lib/generators/devise_views_generator.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
# Remove this file after deprecation
|
||||
if caller.none? { |l| l =~ %r{lib/rails/generators\.rb:(\d+):in `lookup!'$} }
|
||||
warn "[WARNING] `rails g devise_views` is deprecated, please use `rails g devise:views` instead."
|
||||
end
|
||||
@@ -15,37 +15,23 @@ Devise.setup do |config|
|
||||
require 'devise/orm/<%= options[:orm] %>'
|
||||
|
||||
# ==> Configuration for any authentication mechanism
|
||||
# Configure which keys are used when authenticating a user. The default is
|
||||
# Configure which keys are used when authenticating an user. By default is
|
||||
# just :email. You can configure it to use [:username, :subdomain], so for
|
||||
# authenticating a user, both parameters are required. Remember that those
|
||||
# authenticating an user, both parameters are required. Remember that those
|
||||
# parameters are used only when authenticating and not when retrieving from
|
||||
# session. If you need permissions, you should implement that in a before filter.
|
||||
# You can also supply a hash where the value is a boolean determining whether
|
||||
# or not authentication should be aborted when the value is not present.
|
||||
# config.authentication_keys = [ :email ]
|
||||
|
||||
# Configure parameters from the request object used for authentication. Each entry
|
||||
# given should be a request method and it will automatically be passed to the
|
||||
# find_for_authentication method and considered in your model lookup. For instance,
|
||||
# if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
|
||||
# The same considerations mentioned for authentication_keys also apply to request_keys.
|
||||
# config.request_keys = []
|
||||
|
||||
# Configure which authentication keys should be case-insensitive.
|
||||
# These keys will be downcased upon creating or modifying a user and when used
|
||||
# to authenticate or find a user. Default is :email.
|
||||
# config.case_insensitive_keys = [ :email ]
|
||||
|
||||
# Tell if authentication through request.params is enabled. True by default.
|
||||
# config.params_authenticatable = true
|
||||
|
||||
# Tell if authentication through HTTP Basic Auth is enabled. False by default.
|
||||
# config.http_authenticatable = false
|
||||
|
||||
# If http headers should be returned for AJAX requests. True by default.
|
||||
# Set this to true to use Basic Auth for AJAX requests. True by default.
|
||||
# config.http_authenticatable_on_xhr = true
|
||||
|
||||
# The realm used in Http Basic Authentication. "Application" by default.
|
||||
# The realm used in Http Basic Authentication
|
||||
# config.http_authentication_realm = "Application"
|
||||
|
||||
# ==> Configuration for :database_authenticatable
|
||||
@@ -53,13 +39,22 @@ Devise.setup do |config|
|
||||
# using other encryptors, it sets how many times you want the password re-encrypted.
|
||||
config.stretches = 10
|
||||
|
||||
# Define which will be the encryption algorithm. Devise also supports encryptors
|
||||
# from others authentication tools as :clearance_sha1, :authlogic_sha512 (then
|
||||
# you should set stretches above to 20 for default behavior) and :restful_authentication_sha1
|
||||
# (then you should set stretches to 10, and copy REST_AUTH_SITE_KEY to pepper)
|
||||
config.encryptor = :bcrypt
|
||||
|
||||
# Setup a pepper to generate the encrypted password.
|
||||
config.pepper = <%= ActiveSupport::SecureRandom.hex(64).inspect %>
|
||||
|
||||
# ==> Configuration for :confirmable
|
||||
# The time you want to give your user to confirm his account. During this time
|
||||
# he will be able to access your application without confirming. Default is 0.days
|
||||
# When confirm_within is zero, the user won't be able to sign in without confirming.
|
||||
# You can use this to let your user access some features of your application
|
||||
# without confirming the account, but blocking it after a certain period
|
||||
# (ie 2 days).
|
||||
# he will be able to access your application without confirming. Default is nil.
|
||||
# When confirm_within is zero, the user won't be able to sign in without confirming.
|
||||
# You can use this to let your user access some features of your application
|
||||
# without confirming the account, but blocking it after a certain period
|
||||
# (ie 2 days).
|
||||
# config.confirm_within = 2.days
|
||||
|
||||
# ==> Configuration for :rememberable
|
||||
@@ -72,12 +67,8 @@ Devise.setup do |config|
|
||||
# If true, extends the user's remember period when remembered via cookie.
|
||||
# config.extend_remember_period = false
|
||||
|
||||
# If true, uses the password salt as remember token. This should be turned
|
||||
# to false if you are not using database authenticatable.
|
||||
config.use_salt_as_remember_token = true
|
||||
|
||||
# ==> Configuration for :validatable
|
||||
# Range for password length. Default is 6..20.
|
||||
# Range for password length
|
||||
# config.password_length = 6..20
|
||||
|
||||
# Regex to use to validate the email address
|
||||
@@ -85,8 +76,8 @@ Devise.setup do |config|
|
||||
|
||||
# ==> Configuration for :timeoutable
|
||||
# The time you want to timeout the user session without activity. After this
|
||||
# time the user will be asked for credentials again. Default is 30 minutes.
|
||||
# config.timeout_in = 30.minutes
|
||||
# time the user will be asked for credentials again.
|
||||
# config.timeout_in = 10.minutes
|
||||
|
||||
# ==> Configuration for :lockable
|
||||
# Defines which strategy will be used to lock an account.
|
||||
@@ -108,66 +99,44 @@ Devise.setup do |config|
|
||||
# Time interval to unlock the account if :time is enabled as unlock_strategy.
|
||||
# config.unlock_in = 1.hour
|
||||
|
||||
# ==> Configuration for :encryptable
|
||||
# Allow you to use another encryption algorithm besides bcrypt (default). You can use
|
||||
# :sha1, :sha512 or encryptors from others authentication tools as :clearance_sha1,
|
||||
# :authlogic_sha512 (then you should set stretches above to 20 for default behavior)
|
||||
# and :restful_authentication_sha1 (then you should set stretches to 10, and copy
|
||||
# REST_AUTH_SITE_KEY to pepper)
|
||||
# config.encryptor = :sha512
|
||||
|
||||
# Setup a pepper to generate the encrypted password.
|
||||
# config.pepper = <%= ActiveSupport::SecureRandom.hex(64).inspect %>
|
||||
|
||||
# ==> Configuration for :token_authenticatable
|
||||
# Defines name of the authentication token params key
|
||||
# config.token_authentication_key = :auth_token
|
||||
|
||||
# If true, authentication through token does not store user in session and needs
|
||||
# to be supplied on each request. Useful if you are using the token as API token.
|
||||
# config.stateless_token = false
|
||||
|
||||
# ==> Scopes configuration
|
||||
# Turn scoped views on. Before rendering "sessions/new", it will first check for
|
||||
# "users/sessions/new". It's turned off by default because it's slower if you
|
||||
# are using only default views.
|
||||
# config.scoped_views = false
|
||||
# config.scoped_views = true
|
||||
|
||||
# Configure the default scope given to Warden. By default it's the first
|
||||
# devise role declared in your routes (usually :user).
|
||||
# devise role declared in your routes.
|
||||
# config.default_scope = :user
|
||||
|
||||
# Configure sign_out behavior.
|
||||
# Sign_out action can be scoped (i.e. /users/sign_out affects only :user scope).
|
||||
# The default is true, which means any logout action will sign out all active scopes.
|
||||
# config.sign_out_all_scopes = true
|
||||
# Configure sign_out behavior.
|
||||
# By default sign_out is scoped (i.e. /users/sign_out affects only :user scope).
|
||||
# In case of sign_out_all_scopes set to true any logout action will sign out all active scopes.
|
||||
# config.sign_out_all_scopes = false
|
||||
|
||||
# ==> Navigation configuration
|
||||
# Lists the formats that should be treated as navigational. Formats like
|
||||
# :html, should redirect to the sign in page when the user does not have
|
||||
# access, but formats like :xml or :json, should return 401.
|
||||
#
|
||||
# If you have any extra navigational formats, like :iphone or :mobile, you
|
||||
# should add them to the navigational formats lists.
|
||||
#
|
||||
# The :"*/*" format below is required to match Internet Explorer requests.
|
||||
# config.navigational_formats = [:"*/*", :html]
|
||||
|
||||
# The default HTTP method used to sign out a resource. Default is :get.
|
||||
# config.sign_out_via = :get
|
||||
|
||||
# ==> OmniAuth
|
||||
# Add a new OmniAuth provider. Check the wiki for more information on setting
|
||||
# up on your models and hooks.
|
||||
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
|
||||
# should add them to the navigational formats lists. Default is [:html]
|
||||
# config.navigational_formats = [:html, :iphone]
|
||||
|
||||
# ==> Warden configuration
|
||||
# If you want to use other strategies, that are not supported by Devise, or
|
||||
# change the failure app, you can configure them inside the config.warden block.
|
||||
# If you want to use other strategies, that are not (yet) supported by Devise,
|
||||
# you can configure them inside the config.warden block. The example below
|
||||
# allows you to setup OAuth, using http://github.com/roman/warden_oauth
|
||||
#
|
||||
# config.warden do |manager|
|
||||
# manager.failure_app = AnotherApp
|
||||
# manager.intercept_401 = false
|
||||
# manager.default_strategies(:scope => :user).unshift :some_external_strategy
|
||||
# manager.oauth(:twitter) do |twitter|
|
||||
# twitter.consumer_secret = <YOUR CONSUMER SECRET>
|
||||
# twitter.consumer_key = <YOUR CONSUMER KEY>
|
||||
# twitter.options :site => 'http://twitter.com'
|
||||
# end
|
||||
# manager.default_strategies(:scope => :user).unshift :twitter_oauth
|
||||
# end
|
||||
end
|
||||
|
||||
@@ -1,16 +1,51 @@
|
||||
require 'test_helper'
|
||||
require 'ostruct'
|
||||
|
||||
class MockController < ApplicationController
|
||||
attr_accessor :env
|
||||
|
||||
def request
|
||||
self
|
||||
end
|
||||
|
||||
def path
|
||||
''
|
||||
end
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def host_with_port
|
||||
"test.host:3000"
|
||||
end
|
||||
|
||||
def protocol
|
||||
"http"
|
||||
end
|
||||
|
||||
def script_name
|
||||
""
|
||||
end
|
||||
|
||||
def symbolized_path_parameters
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
class ControllerAuthenticableTest < ActionController::TestCase
|
||||
tests ApplicationController
|
||||
tests MockController
|
||||
|
||||
def setup
|
||||
@mock_warden = OpenStruct.new
|
||||
@controller.request.env['warden'] = @mock_warden
|
||||
@controller.env = { 'warden' => @mock_warden }
|
||||
end
|
||||
|
||||
test 'setup warden' do
|
||||
assert_not_nil @controller.warden
|
||||
end
|
||||
|
||||
test 'provide access to warden instance' do
|
||||
assert_equal @mock_warden, @controller.warden
|
||||
assert_equal @controller.warden, @controller.env['warden']
|
||||
end
|
||||
|
||||
test 'proxy signed_in? to authenticated' do
|
||||
@@ -19,9 +54,9 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
test 'proxy anybody_signed_in? to signed_in?' do
|
||||
Devise.mappings.keys.each do |scope| # :user, :admin, :manager
|
||||
Devise.mappings.keys.each { |scope| # :user, :admin, :manager
|
||||
@controller.expects(:signed_in?).with(scope)
|
||||
end
|
||||
}
|
||||
@controller.anybody_signed_in?
|
||||
end
|
||||
|
||||
@@ -55,18 +90,18 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||
@controller.authenticate_publisher_account!
|
||||
end
|
||||
|
||||
test 'proxy user_signed_in? to authenticate with user scope' do
|
||||
@mock_warden.expects(:authenticate).with(:scope => :user).returns("user")
|
||||
assert @controller.user_signed_in?
|
||||
test 'proxy user_signed_in? to authenticate? with user scope' do
|
||||
@mock_warden.expects(:authenticate?).with(:scope => :user)
|
||||
@controller.user_signed_in?
|
||||
end
|
||||
|
||||
test 'proxy admin_signed_in? to authenticatewith admin scope' do
|
||||
@mock_warden.expects(:authenticate).with(:scope => :admin)
|
||||
assert_not @controller.admin_signed_in?
|
||||
test 'proxy admin_signed_in? to authenticate? with admin scope' do
|
||||
@mock_warden.expects(:authenticate?).with(:scope => :admin)
|
||||
@controller.admin_signed_in?
|
||||
end
|
||||
|
||||
test 'proxy publisher_account_signed_in? to authenticate with namespaced publisher account scope' do
|
||||
@mock_warden.expects(:authenticate).with(:scope => :publisher_account)
|
||||
test 'proxy publisher_account_signed_in? to authenticate? with namespaced publisher account scope' do
|
||||
@mock_warden.expects(:authenticate?).with(:scope => :publisher_account)
|
||||
@controller.publisher_account_signed_in?
|
||||
end
|
||||
|
||||
@@ -100,13 +135,6 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||
@controller.sign_in(user)
|
||||
end
|
||||
|
||||
test 'sign in accepts bypass as option' do
|
||||
user = User.new
|
||||
@mock_warden.expects(:session_serializer).returns(serializer = mock())
|
||||
serializer.expects(:store).with(user, :user)
|
||||
@controller.sign_in(user, :bypass => true)
|
||||
end
|
||||
|
||||
test 'sign out proxy to logout on warden' do
|
||||
@mock_warden.expects(:user).with(:user).returns(true)
|
||||
@mock_warden.expects(:logout).with(:user).returns(true)
|
||||
@@ -119,13 +147,12 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||
@controller.sign_out(User.new)
|
||||
end
|
||||
|
||||
test 'sign out without args proxy to sign out all scopes' do
|
||||
@mock_warden.expects(:logout).with().returns(true)
|
||||
@controller.sign_out
|
||||
end
|
||||
|
||||
test 'sign out everybody proxy to logout on warden' do
|
||||
@mock_warden.expects(:logout).with().returns(true)
|
||||
Devise.mappings.keys.each { |scope|
|
||||
@mock_warden.expects(:user).with(scope).returns(true)
|
||||
}
|
||||
|
||||
@mock_warden.expects(:logout).with(*Devise.mappings.keys).returns(true)
|
||||
@controller.sign_out_all_scopes
|
||||
end
|
||||
|
||||
@@ -155,6 +182,14 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||
assert_equal admin_root_path, @controller.after_sign_in_path_for(:admin)
|
||||
end
|
||||
|
||||
test 'after update path defaults to root path if none by was specified for the given scope' do
|
||||
assert_equal root_path, @controller.after_update_path_for(:user)
|
||||
end
|
||||
|
||||
test 'after update path defaults to the scoped root path' do
|
||||
assert_equal admin_root_path, @controller.after_update_path_for(:admin)
|
||||
end
|
||||
|
||||
test 'after sign out path defaults to the root path' do
|
||||
assert_equal root_path, @controller.after_sign_out_path_for(:admin)
|
||||
assert_equal root_path, @controller.after_sign_out_path_for(:user)
|
||||
@@ -185,23 +220,12 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||
@controller.sign_in_and_redirect(admin)
|
||||
end
|
||||
|
||||
test 'sign out and redirect uses the configured after sign out path when signing out only the current scope' do
|
||||
swap Devise, :sign_out_all_scopes => false do
|
||||
@mock_warden.expects(:user).with(:admin).returns(true)
|
||||
@mock_warden.expects(:logout).with(:admin).returns(true)
|
||||
@controller.expects(:redirect_to).with(admin_root_path)
|
||||
@controller.instance_eval "def after_sign_out_path_for(resource); admin_root_path; end"
|
||||
@controller.sign_out_and_redirect(:admin)
|
||||
end
|
||||
end
|
||||
|
||||
test 'sign out and redirect uses the configured after sign out path when signing out all scopes' do
|
||||
swap Devise, :sign_out_all_scopes => true do
|
||||
@mock_warden.expects(:logout).with().returns(true)
|
||||
@controller.expects(:redirect_to).with(admin_root_path)
|
||||
@controller.instance_eval "def after_sign_out_path_for(resource); admin_root_path; end"
|
||||
@controller.sign_out_and_redirect(:admin)
|
||||
end
|
||||
test 'sign out and redirect uses the configured after sign out path' do
|
||||
@mock_warden.expects(:user).with(:admin).returns(true)
|
||||
@mock_warden.expects(:logout).with(:admin).returns(true)
|
||||
@controller.expects(:redirect_to).with(admin_root_path)
|
||||
@controller.instance_eval "def after_sign_out_path_for(resource); admin_root_path; end"
|
||||
@controller.sign_out_and_redirect(:admin)
|
||||
end
|
||||
|
||||
test 'is not a devise controller' do
|
||||
|
||||
@@ -22,16 +22,16 @@ class HelpersTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
test 'get resource instance variable from env' do
|
||||
@controller.instance_variable_set(:@user, user = User.new)
|
||||
assert_equal user, @controller.resource
|
||||
@controller.instance_variable_set(:@user, admin = Admin.new)
|
||||
assert_equal admin, @controller.resource
|
||||
end
|
||||
|
||||
test 'set resource instance variable from env' do
|
||||
user = @controller.send(:resource_class).new
|
||||
@controller.send(:resource=, user)
|
||||
admin = @controller.send(:resource_class).new
|
||||
@controller.send(:resource=, admin)
|
||||
|
||||
assert_equal user, @controller.send(:resource)
|
||||
assert_equal user, @controller.instance_variable_get(:@user)
|
||||
assert_equal admin, @controller.send(:resource)
|
||||
assert_equal admin, @controller.instance_variable_get(:@user)
|
||||
end
|
||||
|
||||
test 'resources methods are not controller actions' do
|
||||
@@ -39,34 +39,13 @@ class HelpersTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
test 'require no authentication tests current mapping' do
|
||||
@controller.expects(:resource_name).returns(:user).twice
|
||||
@mock_warden.expects(:authenticated?).with(:user).returns(true)
|
||||
@mock_warden.expects(:user).with(:user).returns(User.new)
|
||||
@controller.expects(:redirect_to).with(root_path)
|
||||
@controller.send :require_no_authentication
|
||||
end
|
||||
|
||||
test 'signed in resource returns signed in resource for current scope' do
|
||||
@mock_warden.expects(:authenticate).with(:scope => :user).returns(User.new)
|
||||
assert_kind_of User, @controller.signed_in_resource
|
||||
end
|
||||
|
||||
|
||||
test 'is a devise controller' do
|
||||
assert @controller.devise_controller?
|
||||
end
|
||||
|
||||
test 'does not issue blank flash messages' do
|
||||
MyController.send(:public, :set_flash_message)
|
||||
I18n.stubs(:t).returns(' ')
|
||||
@controller.set_flash_message :notice, :send_instructions
|
||||
assert flash[:notice].nil?
|
||||
MyController.send(:protected, :set_flash_message)
|
||||
end
|
||||
|
||||
test 'issues non-blank flash messages normally' do
|
||||
MyController.send(:public, :set_flash_message)
|
||||
I18n.stubs(:t).returns('non-blank')
|
||||
@controller.set_flash_message :notice, :send_instructions
|
||||
assert flash[:notice] == 'non-blank'
|
||||
MyController.send(:protected, :set_flash_message)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,7 +20,7 @@ class RoutesTest < ActionController::TestCase
|
||||
send(:"#{prepend_path}user_#{name}_url", :param => 123)
|
||||
|
||||
@request.path = nil
|
||||
# With an object
|
||||
# With an AR object
|
||||
assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
|
||||
send(:"#{prepend_path}user_#{name}_path")
|
||||
assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
|
||||
@@ -54,6 +54,5 @@ class RoutesTest < ActionController::TestCase
|
||||
assert_path_and_url :registration
|
||||
assert_path_and_url :registration, :new
|
||||
assert_path_and_url :registration, :edit
|
||||
assert_path_and_url :registration, :cancel
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,7 +17,7 @@ class FailureTest < ActiveSupport::TestCase
|
||||
'rack.input' => "",
|
||||
'warden' => OpenStruct.new(:message => nil)
|
||||
}.merge!(env_params)
|
||||
|
||||
|
||||
@response = Devise::FailureApp.call(env).to_a
|
||||
@request = ActionDispatch::Request.new(env)
|
||||
end
|
||||
@@ -28,11 +28,6 @@ class FailureTest < ActiveSupport::TestCase
|
||||
assert_equal 302, @response.first
|
||||
end
|
||||
|
||||
test 'return 302 status for wildcard requests' do
|
||||
call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
|
||||
assert_equal 302, @response.first
|
||||
end
|
||||
|
||||
test 'return to the default redirect location' do
|
||||
call_failure
|
||||
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
|
||||
@@ -77,76 +72,46 @@ class FailureTest < ActiveSupport::TestCase
|
||||
assert_equal 401, @response.first
|
||||
end
|
||||
|
||||
test 'return 401 status for unknown formats' do
|
||||
call_failure 'formats' => []
|
||||
assert_equal 401, @response.first
|
||||
end
|
||||
|
||||
test 'return WWW-authenticate headers if model allows' do
|
||||
test 'return WWW-authenticate headers' do
|
||||
call_failure('formats' => :xml)
|
||||
assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
|
||||
end
|
||||
|
||||
test 'does not return WWW-authenticate headers if model does not allow' do
|
||||
swap Devise, :http_authenticatable => false do
|
||||
call_failure('formats' => :xml)
|
||||
assert_nil @response.second["WWW-Authenticate"]
|
||||
test 'dont return WWW-authenticate on ajax call if http_authenticatable_on_xhr false' do
|
||||
swap Devise, :http_authenticatable_on_xhr => false do
|
||||
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
||||
assert_equal 302, @response.first
|
||||
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
||||
assert_nil @response.second['WWW-Authenticate']
|
||||
end
|
||||
end
|
||||
|
||||
test 'return WWW-authenticate on ajax call if http_authenticatable_on_xhr true' do
|
||||
swap Devise, :http_authenticatable_on_xhr => true do
|
||||
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
||||
assert_equal 401, @response.first
|
||||
assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
|
||||
end
|
||||
end
|
||||
|
||||
test 'uses the proxy failure message as response body' do
|
||||
call_failure('formats' => :xml, 'warden' => OpenStruct.new(:message => :invalid))
|
||||
assert_match '<error>Invalid email or password.</error>', @response.third.body
|
||||
end
|
||||
|
||||
test 'works for any non navigational format' do
|
||||
swap Devise, :navigational_formats => [] do
|
||||
call_failure('formats' => :html)
|
||||
assert_equal 401, @response.first
|
||||
end
|
||||
end
|
||||
|
||||
test 'uses the failure message as response body' do
|
||||
call_failure('formats' => :xml, 'warden' => OpenStruct.new(:message => :invalid))
|
||||
assert_match '<error>Invalid email or password.</error>', @response.third.body
|
||||
end
|
||||
|
||||
context 'on ajax call' do
|
||||
context 'when http_authenticatable_on_xhr is false' do
|
||||
test 'dont return 401 with navigational formats' do
|
||||
swap Devise, :http_authenticatable_on_xhr => false do
|
||||
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
||||
assert_equal 302, @response.first
|
||||
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
||||
end
|
||||
end
|
||||
|
||||
test 'dont return 401 with non navigational formats' do
|
||||
swap Devise, :http_authenticatable_on_xhr => false do
|
||||
call_failure('formats' => :json, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
||||
assert_equal 302, @response.first
|
||||
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when http_authenticatable_on_xhr is true' do
|
||||
test 'return 401' do
|
||||
swap Devise, :http_authenticatable_on_xhr => true do
|
||||
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
||||
assert_equal 401, @response.first
|
||||
end
|
||||
end
|
||||
|
||||
test 'skip WWW-Authenticate header' do
|
||||
swap Devise, :http_authenticatable_on_xhr => true do
|
||||
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
||||
assert_nil @response.second['WWW-Authenticate']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'With recall' do
|
||||
test 'calls the original controller' do
|
||||
env = {
|
||||
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in" },
|
||||
"action_dispatch.request.parameters" => { :controller => "devise/sessions" },
|
||||
"warden.options" => { :recall => "new", :attempted_path => "/users/sign_in" },
|
||||
"devise.mapping" => Devise.mappings[:user],
|
||||
"warden" => stub_everything
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
require "rubygems"
|
||||
require "rails/generators/test_case"
|
||||
require File.join(File.dirname(__FILE__), "../../lib/generators/devise/install_generator")
|
||||
require File.join(File.dirname(__FILE__), "../../lib/generators/devise/views_generator")
|
||||
@@ -1,14 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__),"generators_test_helper.rb")
|
||||
|
||||
class InstallGeneratorTest < Rails::Generators::TestCase
|
||||
tests Devise::Generators::InstallGenerator
|
||||
destination File.expand_path("../tmp", File.dirname(__FILE__))
|
||||
setup :prepare_destination
|
||||
|
||||
test "Assert all files are properly created" do
|
||||
run_generator
|
||||
assert_file "config/initializers/devise.rb"
|
||||
assert_file "config/locales/devise.en.yml"
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,37 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__),"generators_test_helper.rb")
|
||||
|
||||
class ViewsGeneratorTest < Rails::Generators::TestCase
|
||||
tests Devise::Generators::ViewsGenerator
|
||||
destination File.expand_path("../tmp", File.dirname(__FILE__))
|
||||
setup :prepare_destination
|
||||
|
||||
test "Assert all views are properly created with no params" do
|
||||
run_generator
|
||||
assert_files
|
||||
end
|
||||
|
||||
test "Assert all views are properly created with scope param param" do
|
||||
run_generator %w(users)
|
||||
assert_files "users"
|
||||
|
||||
run_generator %w(admins)
|
||||
assert_files "admins"
|
||||
end
|
||||
|
||||
def assert_files(scope = nil, template_engine = nil)
|
||||
scope = "devise" if scope.nil?
|
||||
assert_file "app/views/#{scope}/confirmations/new.html.erb"
|
||||
assert_file "app/views/#{scope}/mailer/confirmation_instructions.html.erb"
|
||||
assert_file "app/views/#{scope}/mailer/reset_password_instructions.html.erb"
|
||||
assert_file "app/views/#{scope}/mailer/unlock_instructions.html.erb"
|
||||
assert_file "app/views/#{scope}/passwords/edit.html.erb"
|
||||
assert_file "app/views/#{scope}/passwords/new.html.erb"
|
||||
assert_file "app/views/#{scope}/registrations/new.html.erb"
|
||||
assert_file "app/views/#{scope}/registrations/edit.html.erb"
|
||||
assert_file "app/views/#{scope}/sessions/new.html.erb"
|
||||
assert_file "app/views/#{scope}/shared/_links.erb"
|
||||
assert_file "app/views/#{scope}/unlocks/new.html.erb"
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
33
test/indifferent_hash.rb
Normal file
33
test/indifferent_hash.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
require 'test_helper'
|
||||
|
||||
class IndifferentHashTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@hash = Devise::IndifferentHash.new
|
||||
end
|
||||
|
||||
test "it overwrites getter and setter" do
|
||||
@hash[:foo] = "bar"
|
||||
assert_equal "bar", @hash["foo"]
|
||||
assert_equal "bar", @hash[:foo]
|
||||
|
||||
@hash["foo"] = "baz"
|
||||
assert_equal "baz", @hash["foo"]
|
||||
assert_equal "baz", @hash[:foo]
|
||||
end
|
||||
|
||||
test "it overwrites update" do
|
||||
@hash.update :foo => "bar"
|
||||
assert_equal "bar", @hash["foo"]
|
||||
assert_equal "bar", @hash[:foo]
|
||||
|
||||
@hash.update "foo" => "baz"
|
||||
assert_equal "baz", @hash["foo"]
|
||||
assert_equal "baz", @hash[:foo]
|
||||
end
|
||||
|
||||
test "it returns a Hash on to_hash" do
|
||||
@hash[:foo] = "bar"
|
||||
assert_equal Hash["foo", "bar"], @hash.to_hash
|
||||
assert_kind_of Hash, @hash.to_hash
|
||||
end
|
||||
end if defined?(Devise::IndifferentHash)
|
||||
@@ -1,6 +1,15 @@
|
||||
require 'test_helper'
|
||||
|
||||
class AuthenticationSanityTest < ActionController::IntegrationTest
|
||||
|
||||
def setup
|
||||
Devise.sign_out_all_scopes = false
|
||||
end
|
||||
|
||||
def teardown
|
||||
Devise.sign_out_all_scopes = false
|
||||
end
|
||||
|
||||
test 'home should be accessible without sign in' do
|
||||
visit '/'
|
||||
assert_response :success
|
||||
@@ -9,12 +18,14 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
|
||||
|
||||
test 'sign in as user should not authenticate admin scope' do
|
||||
sign_in_as_user
|
||||
|
||||
assert warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'sign in as admin should not authenticate user scope' do
|
||||
sign_in_as_admin
|
||||
|
||||
assert warden.authenticated?(:admin)
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
@@ -22,61 +33,59 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
|
||||
test 'sign in as both user and admin at same time' do
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
|
||||
assert warden.authenticated?(:user)
|
||||
assert warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'sign out as user should not touch admin authentication if sign_out_all_scopes is false' do
|
||||
swap Devise, :sign_out_all_scopes => false do
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
get destroy_user_session_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
assert warden.authenticated?(:admin)
|
||||
end
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
|
||||
get destroy_user_session_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
assert warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'sign out as admin should not touch user authentication if sign_out_all_scopes is false' do
|
||||
swap Devise, :sign_out_all_scopes => false do
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
|
||||
get destroy_admin_session_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
get destroy_admin_session_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test 'sign out as user should also sign out admin if sign_out_all_scopes is true' do
|
||||
swap Devise, :sign_out_all_scopes => true do
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
Devise.sign_out_all_scopes = true
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
|
||||
get destroy_user_session_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
get destroy_user_session_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'sign out as admin should also sign out user if sign_out_all_scopes is true' do
|
||||
swap Devise, :sign_out_all_scopes => true do
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
Devise.sign_out_all_scopes = true
|
||||
sign_in_as_user
|
||||
sign_in_as_admin
|
||||
|
||||
get destroy_admin_session_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
get destroy_admin_session_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test 'not signed in as admin should not be able to access admins actions' do
|
||||
get admins_path
|
||||
|
||||
assert_redirected_to new_admin_session_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'not signed in as admin should not be able to access private route restricted to admins' do
|
||||
get private_path
|
||||
|
||||
assert_redirected_to new_admin_session_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
@@ -85,6 +94,7 @@ class AuthenticationSanityTest < ActionController::IntegrationTest
|
||||
sign_in_as_user
|
||||
assert warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
|
||||
get private_path
|
||||
assert_redirected_to new_admin_session_path
|
||||
end
|
||||
@@ -227,25 +237,6 @@ class AuthenticationSessionTest < ActionController::IntegrationTest
|
||||
assert_equal "Cart", @controller.user_session[:cart]
|
||||
end
|
||||
|
||||
test 'does not explode when invalid user class is stored in session' do
|
||||
klass = User
|
||||
paths = ActiveSupport::Dependencies.autoload_paths.dup
|
||||
|
||||
begin
|
||||
sign_in_as_user
|
||||
assert warden.authenticated?(:user)
|
||||
|
||||
Object.send :remove_const, :User
|
||||
ActiveSupport::Dependencies.autoload_paths.clear
|
||||
|
||||
visit "/users"
|
||||
assert_not warden.authenticated?(:user)
|
||||
ensure
|
||||
Object.const_set(:User, klass)
|
||||
ActiveSupport::Dependencies.autoload_paths.replace(paths)
|
||||
end
|
||||
end
|
||||
|
||||
test 'session id is changed on sign in' do
|
||||
get '/users'
|
||||
session_id = request.session["session_id"]
|
||||
@@ -297,13 +288,25 @@ class AuthenticationWithScopesTest < ActionController::IntegrationTest
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test 'uses the mapping from router' do
|
||||
sign_in_as_user :visit => "/as/sign_in"
|
||||
assert warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'uses the mapping from nested devise_for call' do
|
||||
sign_in_as_user :visit => "/devise_for/sign_in"
|
||||
assert warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
end
|
||||
|
||||
class AuthenticationOthersTest < ActionController::IntegrationTest
|
||||
test 'uses the custom controller with the custom controller view' do
|
||||
get '/admin_area/sign_in'
|
||||
assert_contain 'Sign in'
|
||||
assert_contain 'Welcome to "admins/sessions" controller!'
|
||||
assert_contain 'Welcome to "sessions" controller!'
|
||||
assert_contain 'Welcome to "sessions/new" view!'
|
||||
end
|
||||
|
||||
@@ -312,11 +315,6 @@ class AuthenticationOthersTest < ActionController::IntegrationTest
|
||||
assert_equal 404, response.status
|
||||
end
|
||||
|
||||
test 'does not intercept Rails 401 responses' do
|
||||
get '/unauthenticated'
|
||||
assert_equal 401, response.status
|
||||
end
|
||||
|
||||
test 'render 404 on roles without mapping' do
|
||||
assert_raise AbstractController::ActionNotFound do
|
||||
get '/sign_in'
|
||||
@@ -330,111 +328,28 @@ class AuthenticationOthersTest < ActionController::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
test 'registration in xml format works when recognizing path' do
|
||||
test 'registration in xml format' do
|
||||
assert_nothing_raised do
|
||||
post user_registration_path(:format => 'xml', :user => {:email => "test@example.com", :password => "invalid"} )
|
||||
end
|
||||
end
|
||||
|
||||
test 'uses the mapping from router' do
|
||||
sign_in_as_user :visit => "/as/sign_in"
|
||||
assert warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
test 'does not explode when invalid user class is stored in session' do
|
||||
klass = User
|
||||
paths = ActiveSupport::Dependencies.autoload_paths.dup
|
||||
|
||||
test 'uses the mapping from nested devise_for call' do
|
||||
sign_in_as_user :visit => "/devise_for/sign_in"
|
||||
assert warden.authenticated?(:user)
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
end
|
||||
|
||||
class AuthenticationRequestKeysTest < ActionController::IntegrationTest
|
||||
test 'request keys are used on authentication' do
|
||||
host! 'foo.bar.baz'
|
||||
|
||||
swap Devise, :request_keys => [:subdomain] do
|
||||
User.expects(:find_for_authentication).with(:subdomain => 'foo', :email => 'user@test.com').returns(create_user)
|
||||
begin
|
||||
sign_in_as_user
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'invalid request keys raises NoMethodError' do
|
||||
swap Devise, :request_keys => [:unknown_method] do
|
||||
assert_raise NoMethodError do
|
||||
sign_in_as_user
|
||||
end
|
||||
Object.send :remove_const, :User
|
||||
ActiveSupport::Dependencies.autoload_paths.clear
|
||||
|
||||
visit "/users"
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'blank request keys cause authentication to abort' do
|
||||
host! 'test.com'
|
||||
|
||||
swap Devise, :request_keys => [:subdomain] do
|
||||
sign_in_as_user
|
||||
assert_contain "Invalid email or password."
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'blank request keys cause authentication to abort unless if marked as not required' do
|
||||
host! 'test.com'
|
||||
|
||||
swap Devise, :request_keys => { :subdomain => false } do
|
||||
sign_in_as_user
|
||||
assert warden.authenticated?(:user)
|
||||
ensure
|
||||
Object.const_set(:User, klass)
|
||||
ActiveSupport::Dependencies.autoload_paths.replace(paths)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class AuthenticationSignOutViaTest < ActionController::IntegrationTest
|
||||
def sign_in!(scope)
|
||||
sign_in_as_admin(:visit => send("new_#{scope}_session_path"))
|
||||
assert warden.authenticated?(scope)
|
||||
end
|
||||
|
||||
test 'allow sign out via delete when sign_out_via provides only delete' do
|
||||
sign_in!(:sign_out_via_delete)
|
||||
delete destroy_sign_out_via_delete_session_path
|
||||
assert_not warden.authenticated?(:sign_out_via_delete)
|
||||
end
|
||||
|
||||
test 'do not allow sign out via get when sign_out_via provides only delete' do
|
||||
sign_in!(:sign_out_via_delete)
|
||||
get destroy_sign_out_via_delete_session_path
|
||||
assert warden.authenticated?(:sign_out_via_delete)
|
||||
end
|
||||
|
||||
test 'allow sign out via post when sign_out_via provides only post' do
|
||||
sign_in!(:sign_out_via_post)
|
||||
post destroy_sign_out_via_post_session_path
|
||||
assert_not warden.authenticated?(:sign_out_via_post)
|
||||
end
|
||||
|
||||
test 'do not allow sign out via get when sign_out_via provides only post' do
|
||||
sign_in!(:sign_out_via_post)
|
||||
get destroy_sign_out_via_delete_session_path
|
||||
assert warden.authenticated?(:sign_out_via_post)
|
||||
end
|
||||
|
||||
test 'allow sign out via delete when sign_out_via provides delete and post' do
|
||||
sign_in!(:sign_out_via_delete_or_post)
|
||||
delete destroy_sign_out_via_delete_or_post_session_path
|
||||
assert_not warden.authenticated?(:sign_out_via_delete_or_post)
|
||||
end
|
||||
|
||||
test 'allow sign out via post when sign_out_via provides delete and post' do
|
||||
sign_in!(:sign_out_via_delete_or_post)
|
||||
post destroy_sign_out_via_delete_or_post_session_path
|
||||
assert_not warden.authenticated?(:sign_out_via_delete_or_post)
|
||||
end
|
||||
|
||||
test 'do not allow sign out via get when sign_out_via provides delete and post' do
|
||||
sign_in!(:sign_out_via_delete_or_post)
|
||||
get destroy_sign_out_via_delete_or_post_session_path
|
||||
assert warden.authenticated?(:sign_out_via_delete_or_post)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,6 @@
|
||||
require 'test_helper'
|
||||
|
||||
class DatabaseAuthenticationTest < ActionController::IntegrationTest
|
||||
test 'sign in with email of different case should succeed when email is in the list of case insensitive keys' do
|
||||
create_user(:email => 'Foo@Bar.com')
|
||||
|
||||
sign_in_as_user do
|
||||
fill_in 'email', :with => 'foo@bar.com'
|
||||
end
|
||||
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test 'sign in with email of different case should fail when email is NOT the list of case insensitive keys' do
|
||||
swap Devise, :case_insensitive_keys => [] do
|
||||
create_user(:email => 'Foo@Bar.com')
|
||||
|
||||
sign_in_as_user do
|
||||
fill_in 'email', :with => 'foo@bar.com'
|
||||
end
|
||||
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'sign in should not authenticate if not using proper authentication keys' do
|
||||
swap Devise, :authentication_keys => [:username] do
|
||||
sign_in_as_user
|
||||
|
||||
@@ -47,16 +47,6 @@ class HttpAuthenticationTest < ActionController::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
test 'sign in should authenticate with really long token' do
|
||||
token = "token_containing_so_many_characters_that_the_base64_encoding_will_wrap"
|
||||
user = create_user
|
||||
user.update_attribute :authentication_token, token
|
||||
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{token}:x")}"
|
||||
assert_response :success
|
||||
assert_match "<email>user@test.com</email>", response.body
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
|
||||
@@ -64,11 +54,11 @@ class HttpAuthenticationTest < ActionController::IntegrationTest
|
||||
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
|
||||
user
|
||||
end
|
||||
|
||||
|
||||
# Sign in with oauth2 token. This is just to test that it isn't misinterpreted as basic authentication
|
||||
def add_oauth2_header
|
||||
user = create_user
|
||||
get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => "OAuth #{ActiveSupport::Base64.encode64("#{user.email}:123456")}"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,107 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class OmniauthableIntegrationTest < ActionController::IntegrationTest
|
||||
FACEBOOK_INFO = {
|
||||
:id => '12345',
|
||||
:link => 'http://facebook.com/josevalim',
|
||||
:email => 'user@example.com',
|
||||
:first_name => 'Jose',
|
||||
:last_name => 'Valim',
|
||||
:website => 'http://blog.plataformatec.com.br'
|
||||
}
|
||||
|
||||
ACCESS_TOKEN = {
|
||||
:access_token => "plataformatec"
|
||||
}
|
||||
|
||||
setup do
|
||||
stub_facebook!
|
||||
Devise::OmniAuth.short_circuit_authorizers!
|
||||
end
|
||||
|
||||
teardown do
|
||||
Devise::OmniAuth.unshort_circuit_authorizers!
|
||||
Devise::OmniAuth.reset_stubs!
|
||||
end
|
||||
|
||||
def stub_facebook!
|
||||
Devise::OmniAuth.stub!(:facebook) do |b|
|
||||
b.post('/oauth/access_token') { [200, {}, ACCESS_TOKEN.to_json] }
|
||||
b.get('/me?access_token=plataformatec') { [200, {}, FACEBOOK_INFO.to_json] }
|
||||
end
|
||||
end
|
||||
|
||||
test "can access omniauth.auth in the env hash" do
|
||||
visit "/users/sign_in"
|
||||
click_link "Sign in with Facebook"
|
||||
|
||||
json = ActiveSupport::JSON.decode(response.body)
|
||||
|
||||
assert_equal "12345", json["uid"]
|
||||
assert_equal "facebook", json["provider"]
|
||||
assert_equal "josevalim", json["user_info"]["nickname"]
|
||||
assert_equal FACEBOOK_INFO, json["extra"]["user_hash"].symbolize_keys
|
||||
assert_equal "plataformatec", json["credentials"]["token"]
|
||||
end
|
||||
|
||||
test "cleans up session on sign up" do
|
||||
assert_no_difference "User.count" do
|
||||
visit "/users/sign_in"
|
||||
click_link "Sign in with Facebook"
|
||||
end
|
||||
|
||||
assert session["devise.facebook_data"]
|
||||
|
||||
assert_difference "User.count" do
|
||||
visit "/users/sign_up"
|
||||
fill_in "Password", :with => "123456"
|
||||
fill_in "Password confirmation", :with => "123456"
|
||||
click_button "Sign up"
|
||||
end
|
||||
|
||||
assert_current_url "/"
|
||||
assert_contain "You have signed up successfully."
|
||||
assert_contain "Hello User user@example.com"
|
||||
assert_not session["devise.facebook_data"]
|
||||
end
|
||||
|
||||
test "cleans up session on cancel" do
|
||||
assert_no_difference "User.count" do
|
||||
visit "/users/sign_in"
|
||||
click_link "Sign in with Facebook"
|
||||
end
|
||||
|
||||
assert session["devise.facebook_data"]
|
||||
visit "/users/cancel"
|
||||
assert !session["devise.facebook_data"]
|
||||
end
|
||||
|
||||
test "cleans up session on sign in" do
|
||||
assert_no_difference "User.count" do
|
||||
visit "/users/sign_in"
|
||||
click_link "Sign in with Facebook"
|
||||
end
|
||||
|
||||
assert session["devise.facebook_data"]
|
||||
user = sign_in_as_user
|
||||
assert !session["devise.facebook_data"]
|
||||
end
|
||||
|
||||
test "handles callback error parameter according to the specification" do
|
||||
visit "/users/auth/facebook/callback?error=access_denied"
|
||||
assert_current_url "/users/sign_in"
|
||||
assert_contain 'Could not authorize you from Facebook because "Access denied".'
|
||||
end
|
||||
|
||||
test "handles other exceptions from omniauth" do
|
||||
Devise::OmniAuth.stub!(:facebook) do |b|
|
||||
b.post('/oauth/access_token') { [401, {}, {}.to_json] }
|
||||
end
|
||||
|
||||
visit "/users/sign_in"
|
||||
click_link "Sign in with facebook"
|
||||
|
||||
assert_current_url "/users/sign_in"
|
||||
assert_contain 'Could not authorize you from Facebook because "Invalid credentials".'
|
||||
end
|
||||
end
|
||||
@@ -9,7 +9,9 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
|
||||
def request_forgot_password(&block)
|
||||
visit_new_password_path
|
||||
|
||||
assert_response :success
|
||||
assert_template 'passwords/new'
|
||||
assert_not warden.authenticated?(:user)
|
||||
|
||||
fill_in 'email', :with => 'user@test.com'
|
||||
@@ -17,42 +19,19 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
click_button 'Send me reset password instructions'
|
||||
end
|
||||
|
||||
def reset_password(options={}, &block)
|
||||
visit edit_user_password_path(:reset_password_token => options[:reset_password_token]) unless options[:visit] == false
|
||||
def reset_password(options={}, &block)
|
||||
unless options[:visit] == false
|
||||
visit edit_user_password_path(:reset_password_token => options[:reset_password_token])
|
||||
end
|
||||
assert_response :success
|
||||
assert_template 'passwords/edit'
|
||||
|
||||
fill_in 'New password', :with => '987654321'
|
||||
fill_in 'Confirm new password', :with => '987654321'
|
||||
fill_in 'Password', :with => '987654321'
|
||||
fill_in 'Password confirmation', :with => '987654321'
|
||||
yield if block_given?
|
||||
click_button 'Change my password'
|
||||
end
|
||||
|
||||
test 'reset password with email of different case should succeed when email is in the list of case insensitive keys' do
|
||||
create_user(:email => 'Foo@Bar.com')
|
||||
|
||||
request_forgot_password do
|
||||
fill_in 'email', :with => 'foo@bar.com'
|
||||
end
|
||||
|
||||
assert_current_url '/users/sign_in'
|
||||
assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
|
||||
end
|
||||
|
||||
test 'reset password with email of different case should fail when email is NOT the list of case insensitive keys' do
|
||||
swap Devise, :case_insensitive_keys => [] do
|
||||
create_user(:email => 'Foo@Bar.com')
|
||||
|
||||
request_forgot_password do
|
||||
fill_in 'email', :with => 'foo@bar.com'
|
||||
end
|
||||
|
||||
assert_response :success
|
||||
assert_current_url '/users/password'
|
||||
assert_have_selector "input[type=email][value='foo@bar.com']"
|
||||
assert_contain 'not found'
|
||||
end
|
||||
end
|
||||
|
||||
test 'authenticated user should not be able to visit forgot password page' do
|
||||
sign_in_as_user
|
||||
assert warden.authenticated?(:user)
|
||||
@@ -67,7 +46,7 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
create_user
|
||||
request_forgot_password
|
||||
|
||||
assert_current_url '/users/sign_in'
|
||||
assert_template 'sessions/new'
|
||||
assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
|
||||
end
|
||||
|
||||
@@ -77,14 +56,16 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
end
|
||||
|
||||
assert_response :success
|
||||
assert_current_url '/users/password'
|
||||
assert_have_selector "input[type=email][value='invalid.test@test.com']"
|
||||
assert_contain 'not found'
|
||||
assert_template 'passwords/new'
|
||||
assert_have_selector 'input[type=text][value=\'invalid.test@test.com\']'
|
||||
assert_contain 'Email not found'
|
||||
end
|
||||
|
||||
test 'authenticated user should not be able to visit edit password page' do
|
||||
sign_in_as_user
|
||||
|
||||
get edit_user_password_path
|
||||
|
||||
assert_response :redirect
|
||||
assert_redirected_to root_path
|
||||
assert warden.authenticated?(:user)
|
||||
@@ -95,7 +76,7 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
reset_password :reset_password_token => 'invalid_reset_password'
|
||||
|
||||
assert_response :success
|
||||
assert_current_url '/users/password'
|
||||
assert_template 'passwords/edit'
|
||||
assert_have_selector '#error_explanation'
|
||||
assert_contain /Reset password token(.*)invalid/
|
||||
assert_not user.reload.valid_password?('987654321')
|
||||
@@ -105,11 +86,11 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
user = create_user
|
||||
request_forgot_password
|
||||
reset_password :reset_password_token => user.reload.reset_password_token do
|
||||
fill_in 'Confirm new password', :with => 'other_password'
|
||||
fill_in 'Password confirmation', :with => 'other_password'
|
||||
end
|
||||
|
||||
assert_response :success
|
||||
assert_current_url '/users/password'
|
||||
assert_template 'passwords/edit'
|
||||
assert_have_selector '#error_explanation'
|
||||
assert_contain 'Password doesn\'t match confirmation'
|
||||
assert_not user.reload.valid_password?('987654321')
|
||||
@@ -120,7 +101,7 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
request_forgot_password
|
||||
reset_password :reset_password_token => user.reload.reset_password_token
|
||||
|
||||
assert_current_url '/'
|
||||
assert_template 'home/index'
|
||||
assert_contain 'Your password was changed successfully.'
|
||||
assert user.reload.valid_password?('987654321')
|
||||
end
|
||||
@@ -129,7 +110,7 @@ class PasswordTest < ActionController::IntegrationTest
|
||||
user = create_user
|
||||
request_forgot_password
|
||||
reset_password :reset_password_token => user.reload.reset_password_token do
|
||||
fill_in 'Confirm new password', :with => 'other_password'
|
||||
fill_in 'Password confirmation', :with => 'other_password'
|
||||
end
|
||||
assert_response :success
|
||||
assert_have_selector '#error_explanation'
|
||||
|
||||
@@ -13,7 +13,7 @@ class RegistrationTest < ActionController::IntegrationTest
|
||||
fill_in 'password confirmation', :with => 'new_user123'
|
||||
click_button 'Sign up'
|
||||
|
||||
assert_contain 'Welcome! You have signed up successfully.'
|
||||
assert_contain 'You have signed up successfully.'
|
||||
assert warden.authenticated?(:admin)
|
||||
|
||||
admin = Admin.last :order => "id"
|
||||
@@ -28,7 +28,8 @@ class RegistrationTest < ActionController::IntegrationTest
|
||||
fill_in 'password confirmation', :with => 'new_user123'
|
||||
click_button 'Sign up'
|
||||
|
||||
assert_contain 'You have signed up successfully. However, we could not sign you in because your account is unconfirmed.'
|
||||
assert_contain 'You have signed up successfully'
|
||||
assert_contain 'Sign in'
|
||||
assert_not_contain 'You have to confirm your account before continuing'
|
||||
|
||||
assert_not warden.authenticated?(:user)
|
||||
@@ -50,7 +51,6 @@ class RegistrationTest < ActionController::IntegrationTest
|
||||
assert_have_selector '#error_explanation'
|
||||
assert_contain "Email is invalid"
|
||||
assert_contain "Password doesn't match confirmation"
|
||||
assert_contain "2 errors prohibited"
|
||||
assert_nil User.first
|
||||
|
||||
assert_not warden.authenticated?(:user)
|
||||
@@ -66,7 +66,7 @@ class RegistrationTest < ActionController::IntegrationTest
|
||||
click_button 'Sign up'
|
||||
|
||||
assert_current_url '/users'
|
||||
assert_contain(/Email.*already.*taken/)
|
||||
assert_contain(/Email .* already.*taken/)
|
||||
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
@@ -98,20 +98,6 @@ class RegistrationTest < ActionController::IntegrationTest
|
||||
assert_equal "user.new@email.com", User.first.email
|
||||
end
|
||||
|
||||
test 'a signed in user should still be able to use the website after changing his password' do
|
||||
sign_in_as_user
|
||||
get edit_user_registration_path
|
||||
|
||||
fill_in 'password', :with => '12345678'
|
||||
fill_in 'password confirmation', :with => '12345678'
|
||||
fill_in 'current password', :with => '123456'
|
||||
click_button 'Update'
|
||||
|
||||
assert_contain 'You updated your account successfully.'
|
||||
get users_path
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test 'a signed in user should not change his current user with invalid password' do
|
||||
sign_in_as_user
|
||||
get edit_user_registration_path
|
||||
@@ -164,16 +150,4 @@ class RegistrationTest < ActionController::IntegrationTest
|
||||
|
||||
assert User.all.empty?
|
||||
end
|
||||
|
||||
test 'a user should be able to cancel sign up by deleting data in the session' do
|
||||
get "/set"
|
||||
assert_equal "something", @request.session["devise.foo_bar"]
|
||||
|
||||
get "/users/sign_up"
|
||||
assert_equal "something", @request.session["devise.foo_bar"]
|
||||
|
||||
get "/users/cancel"
|
||||
assert_nil @request.session["devise.foo_bar"]
|
||||
assert_redirected_to new_user_registration_path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class RememberMeTest < ActionController::IntegrationTest
|
||||
|
||||
def create_user_and_remember(add_to_token='')
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
@@ -9,14 +10,6 @@ class RememberMeTest < ActionController::IntegrationTest
|
||||
user
|
||||
end
|
||||
|
||||
def create_admin_and_remember
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
raw_cookie = Admin.serialize_into_cookie(admin)
|
||||
cookies['remember_admin_token'] = generate_signed_cookie(raw_cookie)
|
||||
admin
|
||||
end
|
||||
|
||||
def generate_signed_cookie(raw_cookie)
|
||||
request = ActionDispatch::TestRequest.new
|
||||
request.cookie_jar.signed['raw_cookie'] = raw_cookie
|
||||
@@ -36,33 +29,25 @@ class RememberMeTest < ActionController::IntegrationTest
|
||||
test 'do not remember the user if he has not checked remember me option' do
|
||||
user = sign_in_as_user
|
||||
assert_nil request.cookies["remember_user_cookie"]
|
||||
assert_nil user.reload.remember_token
|
||||
end
|
||||
|
||||
test 'generate remember token after sign in' do
|
||||
user = sign_in_as_user :remember_me => true
|
||||
assert request.cookies["remember_user_token"]
|
||||
assert user.reload.remember_token
|
||||
end
|
||||
|
||||
test 'generate remember token after sign in setting cookie options' do
|
||||
test 'generate remember token after sign in setting cookie domain' do
|
||||
# We test this by asserting the cookie is not sent after the redirect
|
||||
# since we changed the domain. This is the only difference with the
|
||||
# previous test.
|
||||
swap Devise, :cookie_options => { :domain => "omg.somewhere.com" } do
|
||||
swap User, :cookie_domain => "omg.somewhere.com" do
|
||||
user = sign_in_as_user :remember_me => true
|
||||
assert_nil request.cookies["remember_user_token"]
|
||||
end
|
||||
end
|
||||
|
||||
test 'generate remember token after sign in setting session options' do
|
||||
begin
|
||||
Rails.configuration.session_options[:domain] = "omg.somewhere.com"
|
||||
user = sign_in_as_user :remember_me => true
|
||||
assert_nil request.cookies["remember_user_token"]
|
||||
ensure
|
||||
Rails.configuration.session_options.delete(:domain)
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember the user before sign in' do
|
||||
user = create_user_and_remember
|
||||
get users_path
|
||||
@@ -89,29 +74,29 @@ class RememberMeTest < ActionController::IntegrationTest
|
||||
|
||||
test 'if both extend_remember_period and remember_across_browsers are true, sends the same token with a new expire date' do
|
||||
swap Devise, :remember_across_browsers => true, :extend_remember_period => true, :remember_for => 1.year do
|
||||
admin = create_admin_and_remember
|
||||
token = admin.remember_token
|
||||
user = create_user_and_remember
|
||||
token = user.remember_token
|
||||
|
||||
admin.remember_created_at = old = 10.minutes.ago
|
||||
admin.save!
|
||||
user.remember_created_at = old = 10.minutes.ago
|
||||
user.save!
|
||||
|
||||
get root_path
|
||||
assert (cookie_expires("remember_admin_token") - 1.year) > (old + 5.minutes)
|
||||
assert_equal token, signed_cookie("remember_admin_token").last
|
||||
get users_path
|
||||
assert (cookie_expires("remember_user_token") - 1.year) > (old + 5.minutes)
|
||||
assert_equal token, signed_cookie("remember_user_token").last
|
||||
end
|
||||
end
|
||||
|
||||
test 'if both extend_remember_period and remember_across_browsers are false, sends a new token with old expire date' do
|
||||
swap Devise, :remember_across_browsers => false, :extend_remember_period => false, :remember_for => 1.year do
|
||||
admin = create_admin_and_remember
|
||||
token = admin.remember_token
|
||||
user = create_user_and_remember
|
||||
token = user.remember_token
|
||||
|
||||
admin.remember_created_at = old = 10.minutes.ago
|
||||
admin.save!
|
||||
user.remember_created_at = old = 10.minutes.ago
|
||||
user.save!
|
||||
|
||||
get root_path
|
||||
assert (cookie_expires("remember_admin_token") - 1.year) < (old + 5.minutes)
|
||||
assert_not_equal token, signed_cookie("remember_admin_token").last
|
||||
get users_path
|
||||
assert (cookie_expires("remember_user_token") - 1.year) < (old + 5.minutes)
|
||||
assert_not_equal token, signed_cookie("remember_user_token").last
|
||||
end
|
||||
end
|
||||
|
||||
@@ -139,41 +124,23 @@ class RememberMeTest < ActionController::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
test 'forget the user before sign out' do
|
||||
user = create_user_and_remember
|
||||
get users_path
|
||||
assert warden.authenticated?(:user)
|
||||
get destroy_user_session_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
assert_nil user.reload.remember_token
|
||||
assert_nil warden.cookies['remember_user_token']
|
||||
end
|
||||
|
||||
test 'do not remember the user anymore after forget' do
|
||||
user = create_user_and_remember
|
||||
get users_path
|
||||
assert warden.authenticated?(:user)
|
||||
|
||||
get destroy_user_session_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
assert_nil warden.cookies['remember_user_token']
|
||||
|
||||
get users_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
assert_nil warden.cookies['remember_user_token']
|
||||
end
|
||||
|
||||
test 'do not remember the admin anymore after forget' do
|
||||
admin = create_admin_and_remember
|
||||
get root_path
|
||||
assert warden.authenticated?(:admin)
|
||||
|
||||
get destroy_admin_session_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
assert_nil warden.cookies['remember_admin_token']
|
||||
|
||||
get root_path
|
||||
assert_not warden.authenticated?(:admin)
|
||||
assert_nil warden.cookies['remember_admin_token']
|
||||
end
|
||||
|
||||
test 'changing user password expires remember me token' do
|
||||
user = create_user_and_remember
|
||||
user.password = "another_password"
|
||||
user.password_confirmation = "another_password"
|
||||
user.save!
|
||||
|
||||
get users_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -76,14 +76,5 @@ class SessionTimeoutTest < ActionController::IntegrationTest
|
||||
assert_contain 'Session expired!'
|
||||
end
|
||||
end
|
||||
|
||||
test 'time out not triggered if remembered' do
|
||||
user = sign_in_as_user :remember_me => true
|
||||
get expire_user_path(user)
|
||||
assert_not_nil last_request_at
|
||||
|
||||
get users_path
|
||||
assert_response :success
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -7,23 +7,12 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
|
||||
sign_in_as_new_user_with_token
|
||||
|
||||
assert_response :success
|
||||
assert_current_url "/users?secret_token=#{VALID_AUTHENTICATION_TOKEN}"
|
||||
assert_template 'users/index'
|
||||
assert_contain 'Welcome'
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'authenticate with valid authentication token key but does not store if stateless' do
|
||||
swap Devise, :token_authentication_key => :secret_token, :stateless_token => true do
|
||||
sign_in_as_new_user_with_token
|
||||
assert warden.authenticated?(:user)
|
||||
|
||||
get users_path
|
||||
assert_redirected_to new_user_session_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'authenticate with valid authentication token key and value through http' do
|
||||
swap Devise, :token_authentication_key => :secret_token do
|
||||
sign_in_as_new_user_with_token(:http_auth => true)
|
||||
|
||||
@@ -35,10 +35,6 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase
|
||||
assert_equal ['test@example.com'], mail.from
|
||||
end
|
||||
|
||||
test 'setup reply to as copy from sender' do
|
||||
assert_equal ['test@example.com'], mail.reply_to
|
||||
end
|
||||
|
||||
test 'setup subject from I18n' do
|
||||
store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :subject => 'Account Confirmation' } } } do
|
||||
assert_equal 'Account Confirmation', mail.subject
|
||||
|
||||
@@ -38,10 +38,6 @@ class ResetPasswordInstructionsTest < ActionMailer::TestCase
|
||||
assert_equal ['test@example.com'], mail.from
|
||||
end
|
||||
|
||||
test 'setup reply to as copy from sender' do
|
||||
assert_equal ['test@example.com'], mail.reply_to
|
||||
end
|
||||
|
||||
test 'setup subject from I18n' do
|
||||
store_translations :en, :devise => { :mailer => { :reset_password_instructions => { :subject => 'Reset instructions' } } } do
|
||||
assert_equal 'Reset instructions', mail.subject
|
||||
|
||||
@@ -38,10 +38,6 @@ class UnlockInstructionsTest < ActionMailer::TestCase
|
||||
assert_equal ['test@example.com'], mail.from
|
||||
end
|
||||
|
||||
test 'setup reply to as copy from sender' do
|
||||
assert_equal ['test@example.com'], mail.reply_to
|
||||
end
|
||||
|
||||
test 'setup subject from I18n' do
|
||||
store_translations :en, :devise => { :mailer => { :unlock_instructions => { :subject => 'Yo unlock instructions' } } } do
|
||||
assert_equal 'Yo unlock instructions', mail.subject
|
||||
|
||||
@@ -12,42 +12,22 @@ class MappingTest < ActiveSupport::TestCase
|
||||
mapping = Devise.mappings[:user]
|
||||
assert_equal User, mapping.to
|
||||
assert_equal User.devise_modules, mapping.modules
|
||||
assert_equal "users", mapping.scoped_path
|
||||
assert_equal :users, mapping.plural
|
||||
assert_equal :user, mapping.singular
|
||||
assert_equal "users", mapping.path
|
||||
assert_equal "/users", mapping.fullpath
|
||||
end
|
||||
|
||||
test 'store options with namespace' do
|
||||
mapping = Devise.mappings[:publisher_account]
|
||||
assert_equal Admin, mapping.to
|
||||
assert_equal "publisher/accounts", mapping.scoped_path
|
||||
assert_equal :publisher_account, mapping.singular
|
||||
assert_equal "accounts", mapping.path
|
||||
assert_equal "/publisher/accounts", mapping.fullpath
|
||||
end
|
||||
|
||||
test 'allows path to be given' do
|
||||
assert_equal "admin_area", Devise.mappings[:admin].path
|
||||
end
|
||||
|
||||
test 'sign_out_via defaults to :get' do
|
||||
assert_equal :get, Devise.mappings[:user].sign_out_via
|
||||
end
|
||||
|
||||
test 'allows custom sign_out_via to be given' do
|
||||
assert_equal :delete, Devise.mappings[:sign_out_via_delete].sign_out_via
|
||||
assert_equal :post, Devise.mappings[:sign_out_via_post].sign_out_via
|
||||
assert_equal [:delete, :post], Devise.mappings[:sign_out_via_delete_or_post].sign_out_via
|
||||
end
|
||||
|
||||
test 'allows custom singular to be given' do
|
||||
assert_equal "accounts", Devise.mappings[:manager].path
|
||||
end
|
||||
|
||||
test 'has strategies depending on the model declaration' do
|
||||
assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
|
||||
assert_equal [:rememberable, :database_authenticatable], Devise.mappings[:admin].strategies
|
||||
assert_equal [:database_authenticatable], Devise.mappings[:admin].strategies
|
||||
end
|
||||
|
||||
test 'find scope for a given object' do
|
||||
@@ -100,20 +80,6 @@ class MappingTest < ActiveSupport::TestCase
|
||||
assert mapping.recoverable?
|
||||
assert mapping.lockable?
|
||||
assert_not mapping.confirmable?
|
||||
assert_not mapping.omniauthable?
|
||||
assert_not mapping.rememberable?
|
||||
end
|
||||
|
||||
test 'find mapping by path' do
|
||||
assert_raise RuntimeError do
|
||||
Devise::Mapping.find_by_path!('/accounts/facebook/callback')
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
Devise::Mapping.find_by_path!('/:locale/accounts/login')
|
||||
end
|
||||
|
||||
assert_nothing_raised do
|
||||
Devise::Mapping.find_by_path!('/accounts/facebook/callback', :path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -48,7 +48,7 @@ class ConfirmableTest < ActiveSupport::TestCase
|
||||
assert_blank user.errors[:email]
|
||||
|
||||
assert_not user.confirm!
|
||||
assert_equal "was already confirmed, please try signing in", user.errors[:email].join
|
||||
assert_equal "was already confirmed", user.errors[:email].join
|
||||
end
|
||||
|
||||
test 'should find and confirm an user automatically' do
|
||||
@@ -76,7 +76,7 @@ class ConfirmableTest < ActiveSupport::TestCase
|
||||
user.save
|
||||
confirmed_user = User.confirm_by_token(user.confirmation_token)
|
||||
assert confirmed_user.confirmed?
|
||||
assert_equal "was already confirmed, please try signing in", confirmed_user.errors[:email].join
|
||||
assert_equal "was already confirmed", confirmed_user.errors[:email].join
|
||||
end
|
||||
|
||||
test 'should send confirmation instructions by email' do
|
||||
@@ -160,7 +160,7 @@ class ConfirmableTest < ActiveSupport::TestCase
|
||||
user.confirm!
|
||||
assert_not user.resend_confirmation_token
|
||||
assert user.confirmed?
|
||||
assert_equal 'was already confirmed, please try signing in', user.errors[:email].join
|
||||
assert_equal 'was already confirmed', user.errors[:email].join
|
||||
end
|
||||
|
||||
test 'confirm time should fallback to devise confirm in default configuration' do
|
||||
|
||||
@@ -2,31 +2,53 @@ require 'test_helper'
|
||||
require 'digest/sha1'
|
||||
|
||||
class DatabaseAuthenticatableTest < ActiveSupport::TestCase
|
||||
test 'should downcase case insensitive keys when saving' do
|
||||
# case_insensitive_keys is set to :email by default.
|
||||
email = 'Foo@Bar.com'
|
||||
user = new_user(:email => email)
|
||||
|
||||
assert_equal email, user.email
|
||||
user.save!
|
||||
assert_equal email.downcase, user.email
|
||||
|
||||
def encrypt_password(user, pepper=User.pepper, stretches=User.stretches, encryptor=User.encryptor_class)
|
||||
encryptor.digest('123456', stretches, user.password_salt, pepper)
|
||||
end
|
||||
|
||||
|
||||
def swap_with_encryptor(klass, encryptor, options={})
|
||||
klass.instance_variable_set(:@encryptor_class, nil)
|
||||
|
||||
swap klass, options.merge(:encryptor => encryptor) do
|
||||
begin
|
||||
yield
|
||||
ensure
|
||||
klass.instance_variable_set(:@encryptor_class, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test 'should respond to password and password confirmation' do
|
||||
user = new_user
|
||||
assert user.respond_to?(:password)
|
||||
assert user.respond_to?(:password_confirmation)
|
||||
end
|
||||
|
||||
test 'should generate encrypted password while setting password' do
|
||||
test 'should generate encrypted password and salt while setting password' do
|
||||
user = new_user
|
||||
assert_present user.password_salt
|
||||
assert_present user.encrypted_password
|
||||
end
|
||||
|
||||
test 'allow authenticatable_salt to work even with nil encrypted password' do
|
||||
user = User.new
|
||||
user.encrypted_password = nil
|
||||
assert_nil user.authenticatable_salt
|
||||
test 'should not change password salt when updating' do
|
||||
user = create_user
|
||||
salt = user.password_salt
|
||||
user.expects(:password_salt=).never
|
||||
user.save!
|
||||
assert_equal salt, user.password_salt
|
||||
end
|
||||
|
||||
test 'should generate a base64 hash using SecureRandom for password salt' do
|
||||
swap_with_encryptor User, :sha1 do
|
||||
ActiveSupport::SecureRandom.expects(:base64).with(15).returns('friendly_token')
|
||||
assert_equal 'friendly_token', new_user.password_salt
|
||||
end
|
||||
end
|
||||
|
||||
test 'should not generate salt if password is blank' do
|
||||
assert_blank new_user(:password => nil).password_salt
|
||||
assert_blank new_user(:password => '').password_salt
|
||||
end
|
||||
|
||||
test 'should not generate encrypted password if password is blank' do
|
||||
@@ -42,12 +64,47 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
|
||||
assert_not_equal encrypted_password, user.encrypted_password
|
||||
end
|
||||
|
||||
test 'should fallback to sha1 as default encryption' do
|
||||
user = new_user
|
||||
assert_equal encrypt_password(user), user.encrypted_password
|
||||
end
|
||||
|
||||
test 'should fallback to devise pepper default configuration' do
|
||||
begin
|
||||
Devise.pepper = ''
|
||||
user = new_user
|
||||
assert_equal encrypt_password(user), user.encrypted_password
|
||||
assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
|
||||
|
||||
Devise.pepper = 'new_pepper'
|
||||
user = new_user
|
||||
assert_equal encrypt_password(user, 'new_pepper'), user.encrypted_password
|
||||
assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
|
||||
ensure
|
||||
Devise.pepper = nil
|
||||
end
|
||||
end
|
||||
|
||||
test 'should respect encryptor configuration' do
|
||||
swap_with_encryptor User, :sha512 do
|
||||
user = create_user
|
||||
assert_equal user.encrypted_password, encrypt_password(user, User.pepper, User.stretches, ::Devise::Encryptors::Sha512)
|
||||
end
|
||||
end
|
||||
|
||||
test 'should test for a valid password' do
|
||||
user = create_user
|
||||
assert user.valid_password?('123456')
|
||||
assert_not user.valid_password?('654321')
|
||||
end
|
||||
|
||||
test 'should not validate password when salt is nil' do
|
||||
admin = create_admin
|
||||
admin.password_salt = nil
|
||||
admin.save
|
||||
assert_not admin.valid_password?('123456')
|
||||
end
|
||||
|
||||
test 'should respond to current password' do
|
||||
assert new_user.respond_to?(:current_password)
|
||||
end
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class EncryptableTest < ActiveSupport::TestCase
|
||||
def encrypt_password(admin, pepper=Admin.pepper, stretches=Admin.stretches, encryptor=Admin.encryptor_class)
|
||||
encryptor.digest('123456', stretches, admin.password_salt, pepper)
|
||||
end
|
||||
|
||||
def swap_with_encryptor(klass, encryptor, options={})
|
||||
klass.instance_variable_set(:@encryptor_class, nil)
|
||||
|
||||
swap klass, options.merge(:encryptor => encryptor) do
|
||||
begin
|
||||
yield
|
||||
ensure
|
||||
klass.instance_variable_set(:@encryptor_class, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test 'should generate salt while setting password' do
|
||||
assert_present create_admin.password_salt
|
||||
end
|
||||
|
||||
test 'should not change password salt when updating' do
|
||||
admin = create_admin
|
||||
salt = admin.password_salt
|
||||
admin.expects(:password_salt=).never
|
||||
admin.save!
|
||||
assert_equal salt, admin.password_salt
|
||||
end
|
||||
|
||||
test 'should generate a base64 hash using SecureRandom for password salt' do
|
||||
swap_with_encryptor Admin, :sha1 do
|
||||
ActiveSupport::SecureRandom.expects(:base64).with(44).returns('friendly_token')
|
||||
assert_equal 'friendly_token', create_admin.password_salt
|
||||
end
|
||||
end
|
||||
|
||||
test 'should not generate salt if password is blank' do
|
||||
assert_blank create_admin(:password => nil).password_salt
|
||||
assert_blank create_admin(:password => '').password_salt
|
||||
end
|
||||
|
||||
test 'should encrypt password again if password has changed' do
|
||||
admin = create_admin
|
||||
encrypted_password = admin.encrypted_password
|
||||
admin.password = admin.password_confirmation = 'new_password'
|
||||
admin.save!
|
||||
assert_not_equal encrypted_password, admin.encrypted_password
|
||||
end
|
||||
|
||||
test 'should respect encryptor configuration' do
|
||||
swap_with_encryptor Admin, :sha512 do
|
||||
admin = create_admin
|
||||
assert_equal admin.encrypted_password, encrypt_password(admin, Admin.pepper, Admin.stretches, ::Devise::Encryptors::Sha512)
|
||||
end
|
||||
end
|
||||
|
||||
test 'should not validate password when salt is nil' do
|
||||
admin = create_admin
|
||||
admin.password_salt = nil
|
||||
admin.save
|
||||
assert_not admin.valid_password?('123456')
|
||||
end
|
||||
end
|
||||
@@ -178,27 +178,11 @@ class LockableTest < ActiveSupport::TestCase
|
||||
assert_equal 'not found', unlock_user.errors[:email].join
|
||||
end
|
||||
|
||||
test 'should find a user to send unlock instructions by authentication_keys' do
|
||||
swap Devise, :authentication_keys => [:username, :email] do
|
||||
user = create_user
|
||||
unlock_user = User.send_unlock_instructions(:email => user.email, :username => user.username)
|
||||
assert_equal unlock_user, user
|
||||
end
|
||||
end
|
||||
|
||||
test 'should require all authentication_keys' do
|
||||
swap Devise, :authentication_keys => [:username, :email] do
|
||||
user = create_user
|
||||
unlock_user = User.send_unlock_instructions(:email => user.email)
|
||||
assert_not unlock_user.persisted?
|
||||
assert_equal "can't be blank", unlock_user.errors[:username].join
|
||||
end
|
||||
end
|
||||
|
||||
test 'should not be able to send instructions if the user is not locked' do
|
||||
user = create_user
|
||||
assert_not user.resend_unlock_token
|
||||
assert_not user.access_locked?
|
||||
assert_equal 'was not locked', user.errors[:email].join
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -85,23 +85,6 @@ class RecoverableTest < ActiveSupport::TestCase
|
||||
assert_not reset_password_user.persisted?
|
||||
assert_equal "not found", reset_password_user.errors[:email].join
|
||||
end
|
||||
|
||||
test 'should find a user to send instructions by authentication_keys' do
|
||||
swap Devise, :authentication_keys => [:username, :email] do
|
||||
user = create_user
|
||||
reset_password_user = User.send_reset_password_instructions(:email => user.email, :username => user.username)
|
||||
assert_equal reset_password_user, user
|
||||
end
|
||||
end
|
||||
|
||||
test 'should require all authentication_keys' do
|
||||
swap Devise, :authentication_keys => [:username, :email] do
|
||||
user = create_user
|
||||
reset_password_user = User.send_reset_password_instructions(:email => user.email)
|
||||
assert_not reset_password_user.persisted?
|
||||
assert_equal "can't be blank", reset_password_user.errors[:username].join
|
||||
end
|
||||
end
|
||||
|
||||
test 'should reset reset_password_token before send the reset instructions email' do
|
||||
user = create_user
|
||||
|
||||
@@ -1,279 +1,218 @@
|
||||
require 'test_helper'
|
||||
|
||||
module SharedRememberableTest
|
||||
extend ActiveSupport::Testing::Declarative
|
||||
|
||||
test 'should respond to remember_me attribute' do
|
||||
assert resource_class.new.respond_to?(:remember_me)
|
||||
assert resource_class.new.respond_to?(:remember_me=)
|
||||
end
|
||||
|
||||
test 'forget_me should clear remember_created_at' do
|
||||
resource = create_resource
|
||||
resource.remember_me!
|
||||
assert_not resource.remember_created_at.nil?
|
||||
resource.forget_me!
|
||||
assert resource.remember_created_at.nil?
|
||||
end
|
||||
|
||||
test 'remember is expired if not created at timestamp is set' do
|
||||
assert create_resource.remember_expired?
|
||||
end
|
||||
|
||||
test 'serialize should return nil if no resource is found' do
|
||||
assert_nil resource_class.serialize_from_cookie([0], "123")
|
||||
end
|
||||
|
||||
test 'remember me return nil if is a valid resource with invalid token' do
|
||||
resource = create_resource
|
||||
assert_nil resource_class.serialize_from_cookie([resource.id], "123")
|
||||
end
|
||||
|
||||
test 'remember for should fallback to devise remember for default configuration' do
|
||||
swap Devise, :remember_for => 1.day do
|
||||
resource = create_resource
|
||||
resource.remember_me!
|
||||
assert_not resource.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember expires at should sum date of creation with remember for configuration' do
|
||||
swap Devise, :remember_for => 3.days do
|
||||
resource = create_resource
|
||||
resource.remember_me!
|
||||
assert_equal 3.days.from_now.to_date, resource.remember_expires_at.to_date
|
||||
|
||||
Devise.remember_for = 5.days
|
||||
assert_equal 5.days.from_now.to_date, resource.remember_expires_at.to_date
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember should be expired if remember_for is zero' do
|
||||
swap Devise, :remember_for => 0.days do
|
||||
Devise.remember_for = 0.days
|
||||
resource = create_resource
|
||||
resource.remember_me!
|
||||
assert resource.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember should be expired if it was created before limit time' do
|
||||
swap Devise, :remember_for => 1.day do
|
||||
resource = create_resource
|
||||
resource.remember_me!
|
||||
resource.remember_created_at = 2.days.ago
|
||||
resource.save
|
||||
assert resource.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember should not be expired if it was created whitin the limit time' do
|
||||
swap Devise, :remember_for => 30.days do
|
||||
resource = create_resource
|
||||
resource.remember_me!
|
||||
resource.remember_created_at = (30.days.ago + 2.minutes)
|
||||
resource.save
|
||||
assert_not resource.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
|
||||
swap Devise, :remember_for => 5.minutes do
|
||||
resource = create_resource
|
||||
resource.remember_me!(false)
|
||||
assert resource.remember_created_at
|
||||
|
||||
resource.remember_created_at = old = 10.minutes.ago
|
||||
resource.save
|
||||
|
||||
resource.remember_me!(false)
|
||||
assert_not_equal old.to_i, resource.remember_created_at.to_i
|
||||
end
|
||||
end
|
||||
|
||||
test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
|
||||
swap Devise, :remember_for => 1.year do
|
||||
resource = create_resource
|
||||
resource.remember_me!(false)
|
||||
assert resource.remember_created_at
|
||||
|
||||
resource.remember_created_at = old = 10.minutes.ago.utc
|
||||
resource.save
|
||||
|
||||
resource.remember_me!(false)
|
||||
assert_equal old.to_i, resource.remember_created_at.to_i
|
||||
end
|
||||
end
|
||||
|
||||
test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
|
||||
swap Devise, :remember_for => 1.year do
|
||||
resource = create_resource
|
||||
resource.remember_me!(true)
|
||||
assert resource.remember_created_at
|
||||
|
||||
resource.remember_created_at = old = 10.minutes.ago
|
||||
resource.save
|
||||
|
||||
resource.remember_me!(true)
|
||||
assert_not_equal old, resource.remember_created_at
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class RememberableTest < ActiveSupport::TestCase
|
||||
include SharedRememberableTest
|
||||
|
||||
def resource_class
|
||||
Admin
|
||||
end
|
||||
|
||||
def create_resource
|
||||
create_admin
|
||||
test 'should respond to remember_me attribute' do
|
||||
user = new_user
|
||||
assert user.respond_to?(:remember_me)
|
||||
end
|
||||
|
||||
test 'remember_me should generate a new token and save the record without validating' do
|
||||
admin = create_admin
|
||||
admin.expects(:valid?).never
|
||||
token = admin.remember_token
|
||||
admin.remember_me!
|
||||
assert_not_equal token, admin.remember_token
|
||||
assert_not admin.changed?
|
||||
user = create_user
|
||||
user.expects(:valid?).never
|
||||
token = user.remember_token
|
||||
user.remember_me!
|
||||
assert_not_equal token, user.remember_token
|
||||
assert_not user.changed?
|
||||
end
|
||||
|
||||
test 'forget_me should clear remember token and save the record without validating' do
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
assert_not admin.remember_token.nil?
|
||||
admin.expects(:valid?).never
|
||||
admin.forget_me!
|
||||
assert admin.remember_token.nil?
|
||||
assert_not admin.changed?
|
||||
end
|
||||
|
||||
test 'serialize into cookie' do
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
assert_equal [admin.to_key, admin.remember_token], Admin.serialize_into_cookie(admin)
|
||||
end
|
||||
|
||||
test 'serialize from cookie' do
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
assert_equal admin, Admin.serialize_from_cookie(admin.to_key, admin.remember_token)
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is true, remember_me! should create a new token if no token exists' do
|
||||
swap Devise, :remember_across_browsers => true, :remember_for => 1.year do
|
||||
admin = create_admin
|
||||
assert_equal nil, admin.remember_token
|
||||
admin.remember_me!
|
||||
assert_not_equal nil, admin.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is true, remember_me! should create a new token if a token exists but has expired' do
|
||||
swap Devise, :remember_across_browsers => true, :remember_for => 1.day do
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
admin.remember_created_at = 2.days.ago
|
||||
admin.save
|
||||
token = admin.remember_token
|
||||
admin.remember_me!
|
||||
assert_not_equal token, admin.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is true, remember_me! should not create a new token if a token exists and has not expired' do
|
||||
swap Devise, :remember_across_browsers => true, :remember_for => 2.days do
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
admin.remember_created_at = 1.day.ago
|
||||
admin.save
|
||||
token = admin.remember_token
|
||||
admin.remember_me!
|
||||
assert_equal token, admin.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is false, remember_me! should create a new token if no token exists' do
|
||||
swap Devise, :remember_across_browsers => false do
|
||||
admin = create_admin
|
||||
assert_equal nil, admin.remember_token
|
||||
admin.remember_me!
|
||||
assert_not_equal nil, admin.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists but has expired' do
|
||||
swap Devise, :remember_across_browsers => false, :remember_for => 1.day do
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
admin.remember_created_at = 2.days.ago
|
||||
admin.save
|
||||
token = admin.remember_token
|
||||
admin.remember_me!
|
||||
assert_not_equal token, admin.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists and has not expired' do
|
||||
swap Devise, :remember_across_browsers => false, :remember_for => 2.days do
|
||||
admin = create_admin
|
||||
admin.remember_me!
|
||||
admin.remember_created_at = 1.day.ago
|
||||
admin.save
|
||||
token = admin.remember_token
|
||||
admin.remember_me!
|
||||
assert_not_equal token, admin.remember_token
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class WithSaltRememberableTest < ActiveSupport::TestCase
|
||||
include SharedRememberableTest
|
||||
|
||||
setup do
|
||||
assert_not User.new.respond_to?(:remember_token)
|
||||
end
|
||||
|
||||
def resource_class
|
||||
User
|
||||
end
|
||||
|
||||
def create_resource
|
||||
create_user
|
||||
end
|
||||
|
||||
test 'remember_me should not generate a new token if using salt' do
|
||||
user = create_user
|
||||
user.expects(:valid?).never
|
||||
user.remember_me!
|
||||
end
|
||||
|
||||
test 'forget_me should not clear remember token if using salt' do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
assert_not user.remember_token.nil?
|
||||
user.expects(:valid?).never
|
||||
user.forget_me!
|
||||
assert user.remember_token.nil?
|
||||
assert_not user.changed?
|
||||
end
|
||||
|
||||
test 'forget_me should clear remember_created_at' do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
assert_not user.remember_created_at.nil?
|
||||
user.forget_me!
|
||||
assert user.remember_created_at.nil?
|
||||
end
|
||||
|
||||
test 'forget should do nothing if no remember token exists' do
|
||||
user = create_user
|
||||
user.expects(:save).never
|
||||
user.forget_me!
|
||||
end
|
||||
|
||||
test 'serialize into cookie' do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
assert_equal [user.to_key, user.authenticatable_salt], User.serialize_into_cookie(user)
|
||||
assert_equal [user.id, user.remember_token], User.serialize_into_cookie(user)
|
||||
end
|
||||
|
||||
test 'serialize from cookie' do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt)
|
||||
assert_equal user, User.serialize_from_cookie(user.id, user.remember_token)
|
||||
end
|
||||
|
||||
test 'raises a RuntimeError if authenticatable_salt is nil' do
|
||||
user = User.new
|
||||
user.encrypted_password = nil
|
||||
assert_raise RuntimeError do
|
||||
user.rememberable_value
|
||||
test 'serialize should return nil if no user is found' do
|
||||
assert_nil User.serialize_from_cookie(0, "123")
|
||||
end
|
||||
|
||||
test 'remember me return nil if is a valid user with invalid token' do
|
||||
user = create_user
|
||||
assert_nil User.serialize_from_cookie(user.id, "123")
|
||||
end
|
||||
|
||||
test 'remember for should fallback to devise remember for default configuration' do
|
||||
swap Devise, :remember_for => 1.day do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
assert_not user.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember expires at should sum date of creation with remember for configuration' do
|
||||
swap Devise, :remember_for => 3.days do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
assert_equal 3.days.from_now.to_date, user.remember_expires_at.to_date
|
||||
|
||||
Devise.remember_for = 5.days
|
||||
assert_equal 5.days.from_now.to_date, user.remember_expires_at.to_date
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember should be expired if remember_for is zero' do
|
||||
swap Devise, :remember_for => 0.days do
|
||||
Devise.remember_for = 0.days
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
assert user.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember should be expired if it was created before limit time' do
|
||||
swap Devise, :remember_for => 1.day do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
user.remember_created_at = 2.days.ago
|
||||
user.save
|
||||
assert user.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'remember should not be expired if it was created whitin the limit time' do
|
||||
swap Devise, :remember_for => 30.days do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
user.remember_created_at = (30.days.ago + 2.minutes)
|
||||
user.save
|
||||
assert_not user.remember_expired?
|
||||
end
|
||||
end
|
||||
|
||||
test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
|
||||
swap Devise, :remember_for => 5.minutes do
|
||||
user = create_user
|
||||
user.remember_me!(false)
|
||||
assert user.remember_created_at
|
||||
|
||||
user.remember_created_at = old = 10.minutes.ago
|
||||
user.save
|
||||
|
||||
user.remember_me!(false)
|
||||
assert_not_equal old.to_i, user.remember_created_at.to_i
|
||||
end
|
||||
end
|
||||
|
||||
test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
|
||||
swap Devise, :remember_for => 1.year do
|
||||
user = create_user
|
||||
user.remember_me!(false)
|
||||
assert user.remember_created_at
|
||||
|
||||
user.remember_created_at = old = 10.minutes.ago.utc
|
||||
user.save
|
||||
|
||||
user.remember_me!(false)
|
||||
assert_equal old.to_i, user.remember_created_at.to_i
|
||||
end
|
||||
end
|
||||
|
||||
test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
|
||||
swap Devise, :remember_for => 1.year do
|
||||
user = create_user
|
||||
user.remember_me!(true)
|
||||
assert user.remember_created_at
|
||||
|
||||
user.remember_created_at = old = 10.minutes.ago
|
||||
user.save
|
||||
|
||||
user.remember_me!(true)
|
||||
assert_not_equal old, user.remember_created_at
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is true, remember_me! should create a new token if no token exists' do
|
||||
swap Devise, :remember_across_browsers => true, :remember_for => 1.year do
|
||||
user = create_user
|
||||
assert_equal nil, user.remember_token
|
||||
user.remember_me!
|
||||
assert_not_equal nil, user.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is true, remember_me! should create a new token if a token exists but has expired' do
|
||||
swap Devise, :remember_across_browsers => true, :remember_for => 1.day do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
user.remember_created_at = 2.days.ago
|
||||
user.save
|
||||
token = user.remember_token
|
||||
user.remember_me!
|
||||
assert_not_equal token, user.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is true, remember_me! should not create a new token if a token exists and has not expired' do
|
||||
swap Devise, :remember_across_browsers => true, :remember_for => 2.days do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
user.remember_created_at = 1.day.ago
|
||||
user.save
|
||||
token = user.remember_token
|
||||
user.remember_me!
|
||||
assert_equal token, user.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is false, remember_me! should create a new token if no token exists' do
|
||||
swap Devise, :remember_across_browsers => false do
|
||||
user = create_user
|
||||
assert_equal nil, user.remember_token
|
||||
user.remember_me!
|
||||
assert_not_equal nil, user.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists but has expired' do
|
||||
swap Devise, :remember_across_browsers => false, :remember_for => 1.day do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
user.remember_created_at = 2.days.ago
|
||||
user.save
|
||||
token = user.remember_token
|
||||
user.remember_me!
|
||||
assert_not_equal token, user.remember_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists and has not expired' do
|
||||
swap Devise, :remember_across_browsers => false, :remember_for => 2.days do
|
||||
user = create_user
|
||||
user.remember_me!
|
||||
user.remember_created_at = 1.day.ago
|
||||
user.save
|
||||
token = user.remember_token
|
||||
user.remember_me!
|
||||
assert_not_equal token, user.remember_token
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class Configurable < User
|
||||
devise :database_authenticatable, :encryptable, :confirmable, :rememberable, :timeoutable, :lockable,
|
||||
devise :database_authenticatable, :confirmable, :rememberable, :timeoutable, :lockable,
|
||||
:stretches => 15, :pepper => 'abcdef', :confirm_within => 5.days,
|
||||
:remember_for => 7.days, :timeout_in => 15.minutes, :unlock_in => 10.days
|
||||
end
|
||||
@@ -26,16 +26,16 @@ class ActiveRecordTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test 'can cherry pick modules' do
|
||||
assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :rememberable, :encryptable
|
||||
assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable
|
||||
end
|
||||
|
||||
test 'chosen modules are inheritable' do
|
||||
assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :rememberable, :encryptable
|
||||
assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable
|
||||
end
|
||||
|
||||
test 'order of module inclusion' do
|
||||
correct_module_order = [:database_authenticatable, :rememberable, :encryptable, :recoverable, :registerable, :lockable, :timeoutable]
|
||||
incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable, :encryptable, :rememberable]
|
||||
correct_module_order = [:database_authenticatable, :recoverable, :registerable, :lockable, :timeoutable]
|
||||
incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable]
|
||||
|
||||
assert_include_modules Admin, *incorrect_module_order
|
||||
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
class OmniAuthRoutesTest < ActionController::TestCase
|
||||
tests ApplicationController
|
||||
|
||||
def assert_path(action, provider, with_param=true)
|
||||
# Resource param
|
||||
assert_equal @controller.send(action, :user, provider),
|
||||
@controller.send("user_#{action}", provider)
|
||||
|
||||
# With an object
|
||||
assert_equal @controller.send(action, User.new, provider),
|
||||
@controller.send("user_#{action}", provider)
|
||||
|
||||
if with_param
|
||||
# Default url params
|
||||
assert_equal @controller.send(action, :user, provider, :param => 123),
|
||||
@controller.send("user_#{action}", provider, :param => 123)
|
||||
end
|
||||
end
|
||||
|
||||
test 'should alias omniauth_callback to mapped user auth_callback' do
|
||||
assert_path :omniauth_callback_path, :facebook
|
||||
end
|
||||
|
||||
test 'should alias omniauth_authorize to mapped user auth_authorize' do
|
||||
assert_path :omniauth_authorize_path, :facebook, false
|
||||
end
|
||||
|
||||
test 'should generate authorization path' do
|
||||
assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook)
|
||||
|
||||
assert_raise ArgumentError do
|
||||
@controller.omniauth_authorize_path(:user, :github)
|
||||
end
|
||||
end
|
||||
|
||||
test 'should generate authorization path with params' do
|
||||
assert_match "/users/auth/open_id?openid_url=http%3A%2F%2Fyahoo.com",
|
||||
@controller.omniauth_authorize_path(:user, :open_id, :openid_url => "http://yahoo.com")
|
||||
end
|
||||
|
||||
test 'should not add a "?" if no param was sent' do
|
||||
assert_equal "/users/auth/open_id",
|
||||
@controller.omniauth_authorize_path(:user, :open_id)
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,3 @@
|
||||
require 'shared_admin'
|
||||
|
||||
class Admin < ActiveRecord::Base
|
||||
include Shim
|
||||
include SharedAdmin
|
||||
devise :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :unlock_strategy => :time
|
||||
end
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user