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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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_relative '../test_helper'
  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. assert Setting.twofa_required?
  24. log_user('jsmith', 'jsmith')
  25. follow_redirect!
  26. assert_redirected_to "/my/twofa/totp/activate/confirm"
  27. end
  28. end
  29. test "should require twofa setup when required for administrators" do
  30. admin = User.find_by_login 'admin'
  31. user = User.find_by_login 'jsmith'
  32. assert_not admin.must_activate_twofa?
  33. assert_not user.must_activate_twofa?
  34. with_settings twofa: "3" do
  35. assert_not Setting.twofa_required?
  36. assert Setting.twofa_optional?
  37. assert Setting.twofa_required_for_administrators?
  38. assert admin.must_activate_twofa?
  39. assert_not user.must_activate_twofa?
  40. log_user('admin', 'admin')
  41. follow_redirect!
  42. assert_redirected_to "/my/twofa/totp/activate/confirm"
  43. end
  44. end
  45. test "should require twofa setup when required by group" do
  46. user = User.find_by_login 'jsmith'
  47. assert_not user.must_activate_twofa?
  48. group = Group.first
  49. group.update_column :twofa_required, true
  50. group.users << user
  51. user.reload
  52. with_settings twofa: "0" do
  53. assert_not Setting.twofa_optional?
  54. assert_not Setting.twofa_required?
  55. assert_not user.must_activate_twofa?
  56. end
  57. with_settings twofa: "1" do
  58. assert Setting.twofa_optional?
  59. assert_not Setting.twofa_required?
  60. assert user.must_activate_twofa?
  61. log_user('jsmith', 'jsmith')
  62. follow_redirect!
  63. assert_redirected_to "/my/twofa/totp/activate/confirm"
  64. end
  65. end
  66. test 'should require to change password first when must_change_passwd is true' do
  67. User.find_by(login: 'jsmith').update_attribute(:must_change_passwd, true)
  68. with_settings twofa: '2' do
  69. log_user('jsmith', 'jsmith')
  70. follow_redirect!
  71. assert_redirected_to '/my/password'
  72. follow_redirect!
  73. # Skip the before action check_twofa_activation for '/my/password'
  74. # to avoid redirect loop
  75. assert_response :success
  76. end
  77. end
  78. test 'should allow logout even if twofa setup is required' do
  79. with_settings twofa: '2' do
  80. log_user('jsmith', 'jsmith')
  81. follow_redirect!
  82. assert_redirected_to '/my/twofa/totp/activate/confirm'
  83. follow_redirect!
  84. post '/logout'
  85. assert_redirected_to '/'
  86. follow_redirect!
  87. assert_response :success
  88. end
  89. end
  90. test "should generate and accept backup codes" do
  91. log_user('jsmith', 'jsmith')
  92. get "/my/account"
  93. assert_response :success
  94. post "/my/twofa/totp/activate/init"
  95. assert_redirected_to "/my/twofa/totp/activate/confirm"
  96. follow_redirect!
  97. assert_response :success
  98. totp = ROTP::TOTP.new User.find_by_login('jsmith').twofa_totp_key
  99. post "/my/twofa/totp/activate", params: {twofa_code: totp.now}
  100. assert_redirected_to "/my/account"
  101. follow_redirect!
  102. assert_response :success
  103. assert_select '.flash', /Two-factor authentication successfully enabled/i
  104. post "/my/twofa/backup_codes/init"
  105. assert_redirected_to "/my/twofa/backup_codes/confirm"
  106. follow_redirect!
  107. assert_response :success
  108. assert_select 'form', /Please enter your two-factor authentication code/i
  109. post "/my/twofa/backup_codes/create", params: {twofa_code: "wrong"}
  110. assert_redirected_to "/my/twofa/backup_codes/confirm"
  111. follow_redirect!
  112. assert_response :success
  113. assert_select 'form', /Please enter your two-factor authentication code/i
  114. # prevent replay attack prevention from kicking in
  115. User.find_by_login('jsmith').update_column :twofa_totp_last_used_at, 2.minutes.ago.to_i
  116. post "/my/twofa/backup_codes/create", params: {twofa_code: totp.now}
  117. assert_redirected_to "/my/twofa/backup_codes"
  118. follow_redirect!
  119. assert_response :success
  120. assert_select ".flash", /your backup codes have been generated/i
  121. assert code = response.body.scan(/<code>([a-z0-9]{4} [a-z0-9]{4} [a-z0-9]{4})<\/code>/).flatten.first
  122. post "/logout"
  123. follow_redirect!
  124. # prevent replay attack prevention from kicking in
  125. User.find_by_login('jsmith').update_column :twofa_totp_last_used_at, 2.minutes.ago.to_i
  126. # sign in with backup code
  127. get "/login"
  128. assert_nil session[:user_id]
  129. assert_response :success
  130. post "/login", params: {
  131. username: 'jsmith',
  132. password: 'jsmith'
  133. }
  134. assert_redirected_to "/account/twofa/confirm"
  135. follow_redirect!
  136. assert_select "#login-form h3", /two-factor authentication/i
  137. post "/account/twofa", params: {twofa_code: code}
  138. assert_redirected_to "/my/page"
  139. follow_redirect!
  140. assert_response :success
  141. end
  142. test "should configure totp and require code on login" do
  143. with_settings twofa: "2" do
  144. log_user('jsmith', 'jsmith')
  145. follow_redirect!
  146. assert_redirected_to "/my/twofa/totp/activate/confirm"
  147. follow_redirect!
  148. assert key = User.find_by_login('jsmith').twofa_totp_key
  149. assert key.present?
  150. totp = ROTP::TOTP.new key
  151. post "/my/twofa/totp/activate", params: {twofa_code: '123456789'}
  152. assert_redirected_to "/my/twofa/totp/activate/confirm"
  153. follow_redirect!
  154. post "/my/twofa/totp/activate", params: {twofa_code: totp.now}
  155. assert_redirected_to "/my/account"
  156. post "/logout"
  157. follow_redirect!
  158. # prevent replay attack prevention from kicking in
  159. User.find_by_login('jsmith').update_column :twofa_totp_last_used_at, 2.minutes.ago.to_i
  160. # sign in with totp
  161. get "/login"
  162. assert_nil session[:user_id]
  163. assert_response :success
  164. post "/login", params: {
  165. username: 'jsmith',
  166. password: 'jsmith'
  167. }
  168. assert_redirected_to "/account/twofa/confirm"
  169. follow_redirect!
  170. assert_select "#login-form h3", /two-factor authentication/i
  171. post "/account/twofa", params: {twofa_code: 'wrong code'}
  172. assert_redirected_to "/account/twofa/confirm"
  173. follow_redirect!
  174. assert_select "#login-form h3", /two-factor authentication/i
  175. assert_select ".flash", /code is invalid/i
  176. post "/account/twofa", params: {twofa_code: totp.now}
  177. assert_redirected_to "/my/page"
  178. follow_redirect!
  179. assert_response :success
  180. end
  181. end
  182. def test_enable_twofa_should_destroy_tokens
  183. recovery_token = Token.create!(:user_id => 2, :action => 'recovery')
  184. autologin_token = Token.create!(:user_id => 2, :action => 'autologin')
  185. with_settings twofa: "2" do
  186. log_user('jsmith', 'jsmith')
  187. follow_redirect!
  188. assert_redirected_to "/my/twofa/totp/activate/confirm"
  189. follow_redirect!
  190. assert key = User.find_by_login('jsmith').twofa_totp_key
  191. assert key.present?
  192. totp = ROTP::TOTP.new key
  193. post "/my/twofa/totp/activate", params: {twofa_code: '123456789'}
  194. assert_redirected_to "/my/twofa/totp/activate/confirm"
  195. follow_redirect!
  196. post "/my/twofa/totp/activate", params: {twofa_code: totp.now}
  197. assert_redirected_to "/my/account"
  198. end
  199. assert_nil Token.find_by_id(recovery_token.id)
  200. assert_nil Token.find_by_id(autologin_token.id)
  201. end
  202. end