-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
app/graphql/graphql_devise/mutations/check_password_token.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,52 @@ | ||
module GraphqlDevise | ||
module Mutations | ||
class CheckPasswordToken < Base | ||
argument :reset_password_token, String, required: true | ||
argument :redirect_url, String, required: false | ||
|
||
def resolve(reset_password_token:, redirect_url: nil) | ||
resource = resource_class.with_reset_password_token(reset_password_token) | ||
raise_user_error(I18n.t('graphql_devise.passwords.reset_token_not_found')) if resource.blank? | ||
|
||
if resource.reset_password_period_valid? | ||
token_info = client_and_token(resource.create_token) | ||
|
||
resource.skip_confirmation! if confirmable_enabled? && !resource.confirmed_at | ||
resource.allow_password_change = true if recoverable_enabled? | ||
|
||
resource.save! | ||
|
||
yield resource if block_given? | ||
|
||
redirect_header_options = { reset_password: true } | ||
redirect_headers = controller.send( | ||
:build_redirect_headers, | ||
token_info.fetch(:token), | ||
token_info.fetch(:client_id), | ||
redirect_header_options | ||
) | ||
|
||
if redirect_url.present? | ||
controller.redirect_to(resource.build_auth_url(redirect_url, redirect_headers)) | ||
else | ||
set_auth_headers(resource) | ||
end | ||
|
||
{ authenticable: resource } | ||
else | ||
raise_user_error(I18n.t('graphql_devise.passwords.reset_token_expired')) | ||
end | ||
end | ||
|
||
private | ||
|
||
def client_and_token(token) | ||
if Gem::Version.new(DeviseTokenAuth::VERSION) <= Gem::Version.new('1.1.0') | ||
{ client_id: token.first, token: token.last } | ||
else | ||
{ client_id: token.client, token: token.token } | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Check Password Token Requests' do | ||
include_context 'with graphql query request' | ||
|
||
let(:user) { create(:user, :confirmed) } | ||
let(:redirect_url) { 'https://google.com' } | ||
let(:query) do | ||
<<-GRAPHQL | ||
mutation { | ||
userCheckPasswordToken( | ||
resetPasswordToken: "#{token}", | ||
redirectUrl: "#{redirect_url}" | ||
) { | ||
authenticable { email } | ||
} | ||
} | ||
GRAPHQL | ||
end | ||
|
||
context 'when reset password token is valid' do | ||
let(:token) { user.send(:set_reset_password_token) } | ||
|
||
context 'when redirect_url is not provided' do | ||
let(:redirect_url) { nil } | ||
|
||
it 'returns authenticable and credentials in the headers' do | ||
get_request | ||
|
||
expect(response).to include_auth_headers | ||
expect(json_response[:data][:userCheckPasswordToken]).to match( | ||
authenticable: { email: user.email } | ||
) | ||
end | ||
end | ||
|
||
context 'when redirect url is provided' do | ||
it 'redirects to redirect url' do | ||
expect do | ||
get_request | ||
|
||
user.reload | ||
end.to change { user.tokens.keys.count }.from(0).to(1).and( | ||
change(user, :allow_password_change).from(false).to(true) | ||
) | ||
|
||
expect(response).to redirect_to %r{\Ahttps://google.com} | ||
expect(response.body).to include("client=#{user.reload.tokens.keys.first}") | ||
expect(response.body).to include('access-token=') | ||
expect(response.body).to include('uid=') | ||
expect(response.body).to include('expiry=') | ||
end | ||
end | ||
|
||
context 'when token has expired' do | ||
it 'returns an expired token error' do | ||
travel_to 10.hours.ago do | ||
token | ||
end | ||
|
||
get_request | ||
|
||
expect(json_response[:errors]).to contain_exactly( | ||
hash_including(message: 'Reset password token is no longer valid.', extensions: { code: 'USER_ERROR' }) | ||
) | ||
end | ||
end | ||
end | ||
|
||
context 'when reset password token is not found' do | ||
let(:token) { user.send(:set_reset_password_token) + 'invalid' } | ||
|
||
it 'redirects to redirect url' do | ||
get_request | ||
|
||
expect(json_response[:errors]).to contain_exactly( | ||
hash_including(message: 'No user found for the specified reset token.', extensions: { code: 'USER_ERROR' }) | ||
) | ||
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