-
Notifications
You must be signed in to change notification settings - Fork 0
implemented disable totp for a user #1237
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
Open
raphael-goetz
wants to merge
2
commits into
main
Choose a base branch
from
#649-disable-totp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Mutations | ||
| module Users | ||
| module Mfa | ||
| module Totp | ||
| class Disable < BaseMutation | ||
| description 'Disables TOTP MFA for the user' | ||
|
|
||
| field :user, ::Types::UserType, null: true, description: 'The modified user' | ||
|
|
||
| argument :current_totp, String, | ||
| required: true, | ||
| description: 'The current totp at the time to verify the mfa authentication device' | ||
|
|
||
| def resolve(current_totp:) | ||
| ::Users::Mfa::Totp::DisableService.new( | ||
| current_authentication, | ||
| current_totp | ||
| ).execute.to_mutation_response(success_key: :user) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Users | ||
| module Mfa | ||
| module Totp | ||
| class DisableService | ||
| include Sagittarius::Database::Transactional | ||
|
|
||
| attr_reader :current_authentication, :current_user, :current_totp | ||
|
|
||
| def initialize(current_authentication, current_totp) | ||
| @current_authentication = current_authentication | ||
| @current_user = current_authentication.user | ||
| @current_totp = current_totp | ||
| end | ||
|
|
||
| def execute | ||
| unless Ability.allowed?(current_authentication, :manage_mfa, current_user) | ||
| return ServiceResponse.error(error_code: :missing_permission) | ||
| end | ||
|
|
||
| return ServiceResponse.error(error_code: :totp_secret_not_set) if current_user.totp_secret.nil? | ||
|
|
||
| totp = ROTP::TOTP.new(current_user.totp_secret) | ||
|
|
||
| return ServiceResponse.error(error_code: :wrong_totp) unless totp.verify(current_totp) | ||
|
Comment on lines
+24
to
+26
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is |
||
|
|
||
| transactional do | ||
| current_user.totp_secret = nil | ||
| unless current_user.save | ||
| return ServiceResponse.error(message: 'Error while saving user', error_code: :invalid_user, | ||
| details: current_user.errors) | ||
| end | ||
|
|
||
| AuditService.audit( | ||
| :mfa_disabled, | ||
| author_id: current_user.id, | ||
| entity: current_user, | ||
| details: { type: :totp }, | ||
| target: current_user | ||
| ) | ||
|
|
||
| ServiceResponse.success(message: 'TOTP disabled', | ||
| payload: current_user) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,20 @@ | ||
| --- | ||
| title: usersMfaTotpDisable | ||
| --- | ||
|
|
||
| Disables TOTP MFA for the user | ||
|
|
||
| ## Arguments | ||
|
|
||
| | Name | Type | Description | | ||
| |------|------|-------------| | ||
| | `clientMutationId` | [`String`](../scalar/string.md) | A unique identifier for the client performing the mutation. | | ||
| | `currentTotp` | [`String!`](../scalar/string.md) | The current totp at the time to verify the mfa authentication device | | ||
|
|
||
| ## Fields | ||
|
|
||
| | Name | Type | Description | | ||
| |------|------|-------------| | ||
| | `clientMutationId` | [`String`](../scalar/string.md) | A unique identifier for the client performing the mutation. | | ||
| | `errors` | [`[Error!]!`](../object/error.md) | Errors encountered during execution of the mutation. | | ||
| | `user` | [`User`](../object/user.md) | The modified user | |
This file contains hidden or 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,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Mutations::Users::Mfa::Totp::Disable do | ||
| it { expect(described_class.graphql_name).to eq('UsersMfaTotpDisable') } | ||
| end |
43 changes: 43 additions & 0 deletions
43
spec/requests/graphql/mutation/users/mfa/totp/disable_mutation_spec.rb
This file contains hidden or 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,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'usersMfaTotpDisable Mutation' do | ||
| include GraphqlHelpers | ||
|
|
||
| subject(:mutate!) { post_graphql mutation, variables: variables, current_user: current_user } | ||
|
|
||
| let(:mutation) do | ||
| <<~QUERY | ||
| mutation($input: UsersMfaTotpDisableInput!) { | ||
| usersMfaTotpDisable(input: $input) { | ||
| #{error_query} | ||
| user { | ||
| id | ||
| } | ||
| } | ||
| } | ||
| QUERY | ||
| end | ||
|
|
||
| let(:input) do | ||
| { | ||
| currentTotp: current_totp, | ||
| } | ||
| end | ||
|
|
||
| let(:variables) { { input: input } } | ||
| let(:secret) { ROTP::Base32.random } | ||
| let(:current_user) { create(:user, totp_secret: secret) } | ||
|
|
||
| context 'when totp is valid' do | ||
| let(:current_totp) { ROTP::TOTP.new(secret).now } | ||
|
|
||
| it 'disables totp' do | ||
| mutate! | ||
|
|
||
| expect(graphql_data_at(:users_mfa_totp_disable, :user, :id)).to be_present | ||
| expect(current_user.reload.totp_secret).to be_nil | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Users::Mfa::Totp::DisableService do | ||
| subject(:service_response) do | ||
| described_class.new(create_authentication(current_user), current_totp).execute | ||
| end | ||
|
|
||
| context 'when user is nil' do | ||
| let(:current_user) { nil } | ||
| let(:current_totp) { nil } | ||
|
|
||
| it { is_expected.not_to be_success } | ||
| it { expect(service_response.payload[:error_code]).to eq(:missing_permission) } | ||
| end | ||
|
|
||
| context 'when user is valid but totp secret is not set' do | ||
| let(:current_user) { create(:user) } | ||
| let(:current_totp) { nil } | ||
|
|
||
| it { is_expected.not_to be_success } | ||
| it { expect(service_response.payload[:error_code]).to eq(:totp_secret_not_set) } | ||
| end | ||
|
|
||
| context 'when user is valid and totp secret is set but totp is wrong' do | ||
| let(:secret) { ROTP::Base32.random } | ||
| let(:current_user) { create(:user, totp_secret: secret) } | ||
| let(:current_totp) { '00000' } | ||
|
|
||
| it { is_expected.not_to be_success } | ||
| it { expect(service_response.payload[:error_code]).to eq(:wrong_totp) } | ||
| it { is_expected.not_to create_audit_event } | ||
| end | ||
|
|
||
| context 'when user is valid and totp is valid' do | ||
| let(:secret) { ROTP::Base32.random } | ||
| let(:current_user) { create(:user, totp_secret: secret) } | ||
| let(:current_totp) { ROTP::TOTP.new(secret).now } | ||
|
|
||
| it { is_expected.to be_success } | ||
|
|
||
| it { | ||
| expect do | ||
| service_response | ||
| end.to change { | ||
| current_user.reload.totp_secret | ||
| }.from(secret).to(nil) | ||
| } | ||
|
|
||
| it { | ||
| is_expected.to create_audit_event( | ||
| :mfa_disabled, | ||
| author_id: current_user.id, | ||
| entity_type: 'User', | ||
| entity_id: current_user.id, | ||
| target_type: 'User', | ||
| target_id: current_user.id, | ||
| details: { type: 'totp' } | ||
| ) | ||
| } | ||
| end | ||
| end |
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.
We should use
Types::Input::MfaInputhere. You should be able to deactivate TOTP by verifying any configured MFA method (for example using a backup code in case you don't have access to your TOTP code generator)You can look in the
Users::Updatemutation and service for that