You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

twofa_test.rb 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class TwofaTest < Redmine::IntegrationTest
  20. fixtures :projects, :users, :email_addresses
  21. test "should require twofa setup when configured" do
  22. with_settings twofa: "2" do
  23. log_user('jsmith', 'jsmith')
  24. follow_redirect!
  25. assert_redirected_to "/my/twofa/totp/activate/confirm"
  26. end
  27. end
  28. test 'should require to change password first when must_change_passwd is true' do
  29. User.find_by(login: 'jsmith').update_attribute(:must_change_passwd, true)
  30. with_settings twofa: '2' do
  31. log_user('jsmith', 'jsmith')
  32. follow_redirect!
  33. assert_redirected_to '/my/password'
  34. follow_redirect!
  35. # Skip the before action check_twofa_activation for '/my/password'
  36. # to avoid redirect loop
  37. assert_response :success
  38. end
  39. end
  40. test 'should allow logout even if twofa setup is required' do
  41. with_settings twofa: '2' do
  42. log_user('jsmith', 'jsmith')
  43. follow_redirect!
  44. assert_redirected_to '/my/twofa/totp/activate/confirm'
  45. follow_redirect!
  46. post '/logout'
  47. assert_redirected_to '/'
  48. follow_redirect!
  49. assert_response :success
  50. end
  51. end
  52. test "should generate and accept backup codes" do
  53. log_user('jsmith', 'jsmith')
  54. get "/my/account"
  55. assert_response :success
  56. post "/my/twofa/totp/activate/init"
  57. assert_redirected_to "/my/twofa/totp/activate/confirm"
  58. follow_redirect!
  59. assert_response :success
  60. totp = ROTP::TOTP.new User.find_by_login('jsmith').twofa_totp_key
  61. post "/my/twofa/totp/activate", params: {twofa_code: totp.now}
  62. assert_redirected_to "/my/account"
  63. follow_redirect!
  64. assert_response :success
  65. assert_select '.flash', /Two-factor authentication successfully enabled/i
  66. post "/my/twofa/backup_codes/init"
  67. assert_redirected_to "/my/twofa/backup_codes/confirm"
  68. follow_redirect!
  69. assert_response :success
  70. assert_select 'form', /Please enter your two-factor authentication code/i
  71. post "/my/twofa/backup_codes/create", params: {twofa_code: "wrong"}
  72. assert_redirected_to "/my/twofa/backup_codes/confirm"
  73. follow_redirect!
  74. assert_response :success
  75. assert_select 'form', /Please enter your two-factor authentication code/i
  76. # prevent replay attack prevention from kicking in
  77. User.find_by_login('jsmith').update_column :twofa_totp_last_used_at, 2.minutes.ago.to_i
  78. post "/my/twofa/backup_codes/create", params: {twofa_code: totp.now}
  79. assert_redirected_to "/my/twofa/backup_codes"
  80. follow_redirect!
  81. assert_response :success
  82. assert_select ".flash", /your backup codes have been generated/i
  83. assert code = response.body.scan(/<code>([a-z0-9]{4} [a-z0-9]{4} [a-z0-9]{4})<\/code>/).flatten.first
  84. post "/logout"
  85. follow_redirect!
  86. # prevent replay attack prevention from kicking in
  87. User.find_by_login('jsmith').update_column :twofa_totp_last_used_at, 2.minutes.ago.to_i
  88. # sign in with backup code
  89. get "/login"
  90. assert_nil session[:user_id]
  91. assert_response :success
  92. post "/login", params: {
  93. username: 'jsmith',
  94. password: 'jsmith'
  95. }
  96. assert_redirected_to "/account/twofa/confirm"
  97. follow_redirect!
  98. assert_select "#login-form h3", /two-factor authentication/i
  99. post "/account/twofa", params: {twofa_code: code}
  100. assert_redirected_to "/my/page"
  101. follow_redirect!
  102. assert_response :success
  103. end
  104. test "should configure totp and require code on login" do
  105. with_settings twofa: "2" do
  106. log_user('jsmith', 'jsmith')
  107. follow_redirect!
  108. assert_redirected_to "/my/twofa/totp/activate/confirm"
  109. follow_redirect!
  110. assert key = User.find_by_login('jsmith').twofa_totp_key
  111. assert key.present?
  112. totp = ROTP::TOTP.new key
  113. post "/my/twofa/totp/activate", params: {twofa_code: '123456789'}
  114. assert_redirected_to "/my/twofa/totp/activate/confirm"
  115. follow_redirect!
  116. post "/my/twofa/totp/activate", params: {twofa_code: totp.now}
  117. assert_redirected_to "/my/account"
  118. post "/logout"
  119. follow_redirect!
  120. # prevent replay attack prevention from kicking in
  121. User.find_by_login('jsmith').update_column :twofa_totp_last_used_at, 2.minutes.ago.to_i
  122. # sign in with totp
  123. get "/login"
  124. assert_nil session[:user_id]
  125. assert_response :success
  126. post "/login", params: {
  127. username: 'jsmith',
  128. password: 'jsmith'
  129. }
  130. assert_redirected_to "/account/twofa/confirm"
  131. follow_redirect!
  132. assert_select "#login-form h3", /two-factor authentication/i
  133. post "/account/twofa", params: {twofa_code: 'wrong code'}
  134. assert_redirected_to "/account/twofa/confirm"
  135. follow_redirect!
  136. assert_select "#login-form h3", /two-factor authentication/i
  137. assert_select ".flash", /code is invalid/i
  138. post "/account/twofa", params: {twofa_code: totp.now}
  139. assert_redirected_to "/my/page"
  140. follow_redirect!
  141. assert_response :success
  142. end
  143. end
  144. end