-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding in unlocks controller and specs. This should resolve #927. #971
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6b08b8e
Adding in unlocks controller and specs. This should resolve #927.
brycesenz 32eaf8f
Uncommenting specs
brycesenz abe7c82
Refactoring the UnlocksController, PasswordsController, and SessionsC…
brycesenz 390bc0c
Incorporating the changes/bug fix proposed by @MaicolBen during the c…
brycesenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
app/controllers/devise_token_auth/unlocks_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
module DeviseTokenAuth | ||
class UnlocksController < DeviseTokenAuth::ApplicationController | ||
skip_after_action :update_auth_header, :only => [:create, :show] | ||
|
||
# this action is responsible for generating unlock tokens and | ||
# sending emails | ||
def create | ||
unless resource_params[:email] | ||
return render_create_error_missing_email | ||
end | ||
|
||
# honor devise configuration for case_insensitive_keys | ||
if resource_class.case_insensitive_keys.include?(:email) | ||
@email = resource_params[:email].downcase | ||
else | ||
@email = resource_params[:email] | ||
end | ||
|
||
q = "uid = ? AND provider='email'" | ||
|
||
# fix for mysql default case insensitivity | ||
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql' | ||
q = "BINARY uid = ? AND provider='email'" | ||
end | ||
|
||
@resource = resource_class.where(q, @email).first | ||
|
||
@errors = nil | ||
@error_status = 400 | ||
|
||
if @resource | ||
yield @resource if block_given? | ||
|
||
@resource.send_unlock_instructions({ | ||
email: @email, | ||
provider: 'email', | ||
client_config: params[:config_name] | ||
}) | ||
|
||
if @resource.errors.empty? | ||
return render_create_success | ||
else | ||
@errors = @resource.errors | ||
end | ||
else | ||
@errors = [I18n.t("devise_token_auth.unlocks.user_not_found", email: @email)] | ||
@error_status = 404 | ||
end | ||
|
||
if @errors | ||
return render_create_error | ||
end | ||
end | ||
|
||
def show | ||
@resource = resource_class.unlock_access_by_token(params[:unlock_token]) | ||
|
||
if @resource && @resource.id | ||
client_id = SecureRandom.urlsafe_base64(nil, false) | ||
token = SecureRandom.urlsafe_base64(nil, false) | ||
token_hash = BCrypt::Password.create(token) | ||
expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i | ||
|
||
@resource.tokens[client_id] = { | ||
token: token_hash, | ||
expiry: expiry | ||
} | ||
|
||
@resource.save! | ||
yield @resource if block_given? | ||
|
||
redirect_to(@resource.build_auth_url(after_unlock_path_for(@resource), { | ||
token: token, | ||
client_id: client_id, | ||
unlock: true, | ||
config: params[:config] | ||
})) | ||
else | ||
render_show_error | ||
end | ||
end | ||
|
||
private | ||
def after_unlock_path_for(resource) | ||
#TODO: This should probably be a configuration option at the very least. | ||
'/' | ||
end | ||
|
||
def render_create_error_missing_email | ||
render json: { | ||
success: false, | ||
errors: [I18n.t("devise_token_auth.unlocks.missing_email")] | ||
}, status: 401 | ||
end | ||
|
||
def render_create_success | ||
render json: { | ||
success: true, | ||
message: I18n.t("devise_token_auth.unlocks.sended", email: @email) | ||
} | ||
end | ||
|
||
def render_create_error | ||
render json: { | ||
success: false, | ||
errors: @errors, | ||
}, status: @error_status | ||
end | ||
|
||
def render_show_error | ||
raise ActionController::RoutingError.new('Not Found') | ||
end | ||
|
||
def resource_params | ||
params.permit(:email, :unlock_token, :config) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
test/controllers/devise_token_auth/unlocks_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
require 'test_helper' | ||
|
||
# was the web request successful? | ||
# was the user redirected to the right page? | ||
# was the user successfully authenticated? | ||
# was the correct object stored in the response? | ||
# was the appropriate message delivered in the json payload? | ||
|
||
class DeviseTokenAuth::UnlocksControllerTest < ActionController::TestCase | ||
describe DeviseTokenAuth::UnlocksController do | ||
setup do | ||
@request.env['devise.mapping'] = Devise.mappings[:lockable_user] | ||
end | ||
|
||
teardown do | ||
@request.env['devise.mapping'] = Devise.mappings[:user] | ||
end | ||
|
||
before do | ||
@original_lock_strategy = Devise.lock_strategy | ||
@original_unlock_strategy = Devise.unlock_strategy | ||
@original_maximum_attempts = Devise.maximum_attempts | ||
Devise.lock_strategy = :failed_attempts | ||
Devise.unlock_strategy = :email | ||
Devise.maximum_attempts = 5 | ||
end | ||
|
||
after do | ||
Devise.lock_strategy = @original_lock_strategy | ||
Devise.maximum_attempts = @original_maximum_attempts | ||
Devise.unlock_strategy = @original_unlock_strategy | ||
end | ||
|
||
describe "Unlocking user" do | ||
before do | ||
@resource = lockable_users(:unlocked_user) | ||
end | ||
|
||
describe 'not email should return 401' do | ||
before do | ||
@auth_headers = @resource.create_new_auth_token | ||
@new_password = Faker::Internet.password | ||
|
||
xhr :post, :create, {} | ||
@data = JSON.parse(response.body) | ||
end | ||
|
||
test 'response should fail' do | ||
assert_equal 401, response.status | ||
end | ||
test 'error message should be returned' do | ||
assert @data["errors"] | ||
assert_equal @data["errors"], [I18n.t("devise_token_auth.passwords.missing_email")] | ||
end | ||
end | ||
|
||
describe 'request unlock' do | ||
describe 'unknown user should return 404' do | ||
before do | ||
xhr :post, :create, { | ||
email: 'chester@cheet.ah' | ||
} | ||
@data = JSON.parse(response.body) | ||
end | ||
test 'unknown user should return 404' do | ||
assert_equal 404, response.status | ||
end | ||
|
||
test 'errors should be returned' do | ||
assert @data["errors"] | ||
assert_equal @data["errors"], [I18n.t("devise_token_auth.passwords.user_not_found", email: 'chester@cheet.ah')] | ||
end | ||
end | ||
|
||
describe 'successfully requested unlock' do | ||
before do | ||
xhr :post, :create, { | ||
email: @resource.email | ||
} | ||
|
||
@data = JSON.parse(response.body) | ||
end | ||
|
||
test 'response should not contain extra data' do | ||
assert_nil @data["data"] | ||
end | ||
end | ||
|
||
describe 'case-sensitive email' do | ||
before do | ||
xhr :post, :create, { | ||
email: @resource.email | ||
} | ||
|
||
@mail = ActionMailer::Base.deliveries.last | ||
@resource.reload | ||
@data = JSON.parse(response.body) | ||
|
||
@mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1]) | ||
@mail_reset_token = @mail.body.match(/unlock_token=(.*)\"/)[1] | ||
end | ||
|
||
test 'response should return success status' do | ||
assert_equal 200, response.status | ||
end | ||
|
||
test 'response should contains message' do | ||
assert_equal @data["message"], I18n.t("devise_token_auth.unlocks.sended", email: @resource.email) | ||
end | ||
|
||
test 'action should send an email' do | ||
assert @mail | ||
end | ||
|
||
test 'the email should be addressed to the user' do | ||
assert_equal @mail.to.first, @resource.email | ||
end | ||
|
||
test 'the client config name should fall back to "default"' do | ||
assert_equal 'default', @mail_config_name | ||
end | ||
|
||
test 'the email body should contain a link with reset token as a query param' do | ||
user = LockableUser.unlock_access_by_token(@mail_reset_token) | ||
assert_equal user.id, @resource.id | ||
end | ||
|
||
describe 'unlock link failure' do | ||
test 'response should return 404' do | ||
assert_raises(ActionController::RoutingError) { | ||
xhr :get, :show, { | ||
unlock_token: "bogus" | ||
} | ||
} | ||
end | ||
end | ||
|
||
describe 'password reset link success' do | ||
before do | ||
xhr :get, :show, { | ||
unlock_token: @mail_reset_token | ||
} | ||
|
||
@resource.reload | ||
|
||
raw_qs = response.location.split('?')[1] | ||
@qs = Rack::Utils.parse_nested_query(raw_qs) | ||
|
||
@client_id = @qs["client_id"] | ||
@expiry = @qs["expiry"] | ||
@unlock = @qs["unlock"] | ||
@token = @qs["token"] | ||
@uid = @qs["uid"] | ||
end | ||
|
||
test 'respones should have success redirect status' do | ||
assert_equal 302, response.status | ||
end | ||
|
||
test 'response should contain auth params' do | ||
assert @client_id | ||
assert @expiry | ||
assert @unlock | ||
assert @token | ||
assert @uid | ||
end | ||
|
||
test 'response auth params should be valid' do | ||
assert @resource.valid_token?(@token, @client_id) | ||
end | ||
end | ||
end | ||
|
||
describe 'case-insensitive email' do | ||
before do | ||
@resource_class = LockableUser | ||
@request_params = { | ||
email: @resource.email.upcase | ||
} | ||
end | ||
|
||
test 'response should return success status if configured' do | ||
@resource_class.case_insensitive_keys = [:email] | ||
xhr :post, :create, @request_params | ||
assert_equal 200, response.status | ||
end | ||
|
||
test 'response should return failure status if not configured' do | ||
@resource_class.case_insensitive_keys = [] | ||
xhr :post, :create, @request_params | ||
assert_equal 404, response.status | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this message correct? I would put
request unlock without email