Skip to content

Commit

Permalink
Follow up - 4c98b08 - Maintenance: Improve Two-Factor disabled method…
Browse files Browse the repository at this point in the history
…s handling.
  • Loading branch information
mantas committed May 6, 2024
1 parent bde57cb commit f829e8a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
Expand Up @@ -252,7 +252,7 @@ App.Config.set('Password', {
canChangePassword = App.Config.get('user_show_password_login') ||
controller.permissionCheck('admin.*')

twoFactorEnabled = App.Config.get('two_factor_authentication_method_authenticator_app') &&
twoFactorEnabled = App.TwoFactorMethods.isAnyAuthenticationMethodEnabled() &&
controller.permissionCheck('user_preferences.two_factor_authentication')

return false if !canChangePassword && !twoFactorEnabled
Expand Down
@@ -1,10 +1,12 @@
class App.TwoFactorMethods
@sortedMethods: ->
all_methods = App.Config.get('TwoFactorMethods')

_.sortBy all_methods, (elem) -> elem.order
_.sortBy App.Config.get('TwoFactorMethods'), (elem) -> elem.order

@methodByKey: (key) ->
_.findWhere App.Config.get('TwoFactorMethods'), { key: key }

@authenticationMethods: ->
_.where @sortedMethods(), { authenticationMethod: true }

@isAnyAuthenticationMethodEnabled: ->
_.some @authenticationMethods(), (elem) -> App.Config.get(elem.settingKey)
@@ -1,10 +1,12 @@
App.Config.set('AuthenticatorApp', {
key: 'authenticator_app'
identifier: 'AuthenticatorApp'
editable: true
label: __('Authenticator App')
description: __('Get the security code from the authenticator app on your device.')
helpMessage: __('Enter the code from your two-factor authenticator app.')
icon: 'mobile-code'
order: 2000
key: 'authenticator_app'
identifier: 'AuthenticatorApp'
editable: true
label: __('Authenticator App')
description: __('Get the security code from the authenticator app on your device.')
helpMessage: __('Enter the code from your two-factor authenticator app.')
icon: 'mobile-code'
order: 2000
authenticationMethod: true
settingKey: 'two_factor_authentication_method_authenticator_app'
}, 'TwoFactorMethods')
@@ -1,9 +1,11 @@
App.Config.set('RecoveryCodes', {
key: 'recovery_codes'
identifier: 'RecoveryCodes'
label: __('Recovery Codes')
description: __('Use one of your safely stored recovery codes.')
helpMessage: __('Enter one of your unused recovery codes.')
icon: 'mobile-code'
order: 2000
key: 'recovery_codes'
identifier: 'RecoveryCodes'
label: __('Recovery Codes')
description: __('Use one of your safely stored recovery codes.')
helpMessage: __('Enter one of your unused recovery codes.')
icon: 'mobile-code'
order: 2000
authenticationMethod: false
settingKey: 'two_factor_authentication_recovery_codes'
}, 'TwoFactorMethods')
@@ -1,10 +1,12 @@
App.Config.set('SecurityKeys', {
key: 'security_keys'
identifier: 'SecurityKeys'
editable: true
label: __('Security Keys')
description: __('Complete the sign-in with your security key.')
helpMessage: __('Complete the sign-in with your security key.')
icon: 'security-key'
order: 1000
key: 'security_keys'
identifier: 'SecurityKeys'
editable: true
label: __('Security Keys')
description: __('Complete the sign-in with your security key.')
helpMessage: __('Complete the sign-in with your security key.')
icon: 'security-key'
order: 1000
authenticationMethod: true
settingKey: 'two_factor_authentication_method_security_keys'
}, 'TwoFactorMethods')
13 changes: 12 additions & 1 deletion spec/system/profile/password_spec.rb
Expand Up @@ -35,6 +35,16 @@
.and have_text('Two-factor Authentication')
end

it 'shows two factor if another two factor method enabled' do
password_and_authenticate(password: false, two_factor: false, alternative_two_factor: true)

visit 'profile/password'

expect(page)
.to have_no_text('Change Your Password')
.and have_text('Two-factor Authentication')
end

context 'when user has no two factor permission' do
before do
user.roles.each { |role| role.permission_revoke('user_preferences.two_factor_authentication') }
Expand All @@ -58,8 +68,9 @@
end
end

def password_and_authenticate(password:, two_factor:)
def password_and_authenticate(password:, two_factor:, alternative_two_factor: false)
Setting.set('two_factor_authentication_method_authenticator_app', two_factor)
Setting.set('two_factor_authentication_method_security_keys', alternative_two_factor)
Setting.set('two_factor_authentication_enforce_role_ids', [])
Setting.set('user_show_password_login', password)
end
Expand Down

0 comments on commit f829e8a

Please sign in to comment.