sessions_controller_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require 'webauthn/fake_client'
  4. RSpec.describe Auth::SessionsController do
  5. render_views
  6. before do
  7. request.env['devise.mapping'] = Devise.mappings[:user]
  8. end
  9. describe 'GET #new' do
  10. it 'returns http success' do
  11. get :new
  12. expect(response).to have_http_status(200)
  13. end
  14. end
  15. describe 'DELETE #destroy' do
  16. let(:user) { Fabricate(:user) }
  17. context 'with a regular user' do
  18. it 'redirects to home after sign out' do
  19. sign_in(user, scope: :user)
  20. delete :destroy
  21. expect(response).to redirect_to(new_user_session_path)
  22. end
  23. it 'does not delete redirect location with continue=true' do
  24. sign_in(user, scope: :user)
  25. controller.store_location_for(:user, '/authorize')
  26. delete :destroy, params: { continue: 'true' }
  27. expect(controller.stored_location_for(:user)).to eq '/authorize'
  28. end
  29. end
  30. context 'with a suspended user' do
  31. before do
  32. user.account.suspend!
  33. end
  34. it 'redirects to home after sign out' do
  35. sign_in(user, scope: :user)
  36. delete :destroy
  37. expect(response).to redirect_to(new_user_session_path)
  38. end
  39. end
  40. end
  41. describe 'POST #create' do
  42. context 'when using PAM authentication', if: ENV['PAM_ENABLED'] == 'true' do
  43. context 'when using a valid password' do
  44. before do
  45. post :create, params: { user: { email: 'pam_user1', password: '123456' } }
  46. end
  47. it 'redirects to home and logs the user in' do
  48. expect(response).to redirect_to(root_path)
  49. expect(controller.current_user).to be_instance_of(User)
  50. end
  51. end
  52. context 'when using an invalid password' do
  53. before do
  54. post :create, params: { user: { email: 'pam_user1', password: 'WRONGPW' } }
  55. end
  56. it 'shows a login error and does not log the user in' do
  57. expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: I18n.t('activerecord.attributes.user.email'))
  58. expect(controller.current_user).to be_nil
  59. end
  60. end
  61. context 'when using a valid email and existing user' do
  62. let!(:user) do
  63. account = Fabricate.build(:account, username: 'pam_user1', user: nil)
  64. account.save!(validate: false)
  65. user = Fabricate(:user, email: 'pam@example.com', password: nil, account: account, external: true)
  66. user
  67. end
  68. before do
  69. post :create, params: { user: { email: user.email, password: '123456' } }
  70. end
  71. it 'redirects to home and logs the user in' do
  72. expect(response).to redirect_to(root_path)
  73. expect(controller.current_user).to eq user
  74. end
  75. end
  76. end
  77. context 'when using password authentication' do
  78. let(:user) { Fabricate(:user, email: 'foo@bar.com', password: 'abcdefgh') }
  79. context 'when using a valid password' do
  80. before do
  81. post :create, params: { user: { email: user.email, password: user.password } }
  82. end
  83. it 'redirects to home and logs the user in' do
  84. expect(response).to redirect_to(root_path)
  85. expect(controller.current_user).to eq user
  86. end
  87. end
  88. context 'when using a valid password on a previously-used account with a new IP address' do
  89. subject { post :create, params: { user: { email: user.email, password: user.password } } }
  90. let(:previous_ip) { '1.2.3.4' }
  91. let(:current_ip) { '4.3.2.1' }
  92. before do
  93. Fabricate(:login_activity, user: user, ip: previous_ip)
  94. allow(controller.request).to receive(:remote_ip).and_return(current_ip)
  95. user.update(current_sign_in_at: 1.month.ago)
  96. end
  97. it 'logs the user in and sends suspicious email and redirects home', :inline_jobs do
  98. emails = capture_emails { subject }
  99. expect(response)
  100. .to redirect_to(root_path)
  101. expect(controller.current_user)
  102. .to eq user
  103. expect(emails.size)
  104. .to eq(1)
  105. expect(emails.first)
  106. .to have_attributes(
  107. to: contain_exactly(user.email),
  108. subject: eq(I18n.t('user_mailer.suspicious_sign_in.subject'))
  109. )
  110. end
  111. end
  112. context 'when using email with uppercase letters' do
  113. before do
  114. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  115. end
  116. it 'redirects to home and logs the user in' do
  117. expect(response).to redirect_to(root_path)
  118. expect(controller.current_user).to eq user
  119. end
  120. end
  121. context 'when using an invalid password' do
  122. before do
  123. post :create, params: { user: { email: user.email, password: 'wrongpw' } }
  124. end
  125. it 'shows a login error and does not log the user in' do
  126. expect(flash[:alert]).to match I18n.t('devise.failure.invalid', authentication_keys: I18n.t('activerecord.attributes.user.email'))
  127. expect(controller.current_user).to be_nil
  128. end
  129. end
  130. context 'when using an unconfirmed password' do
  131. before do
  132. request.headers['Accept-Language'] = accept_language
  133. post :create, params: { user: { email: unconfirmed_user.email, password: unconfirmed_user.password } }
  134. end
  135. let(:unconfirmed_user) { user.tap { |u| u.update!(confirmed_at: nil) } }
  136. let(:accept_language) { 'fr' }
  137. it 'redirects to home' do
  138. expect(response).to redirect_to(root_path)
  139. end
  140. end
  141. context "when logging in from the user's page" do
  142. before do
  143. allow(controller).to receive(:single_user_mode?).and_return(single_user_mode)
  144. allow(controller).to receive(:stored_location_for).with(:user).and_return("/@#{user.account.username}")
  145. post :create, params: { user: { email: user.email, password: user.password } }
  146. end
  147. context 'with single user mode' do
  148. let(:single_user_mode) { true }
  149. it 'redirects to home' do
  150. expect(response).to redirect_to(root_path)
  151. end
  152. end
  153. context 'with non-single user mode' do
  154. let(:single_user_mode) { false }
  155. it "redirects back to the user's page" do
  156. expect(response).to redirect_to(short_account_path(username: user.account))
  157. end
  158. end
  159. end
  160. end
  161. context 'when using two-factor authentication' do
  162. context 'with OTP enabled as second factor' do
  163. let!(:user) do
  164. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh', otp_required_for_login: true, otp_secret: User.generate_otp_secret)
  165. end
  166. let!(:recovery_codes) do
  167. codes = user.generate_otp_backup_codes!
  168. user.save
  169. return codes
  170. end
  171. context 'when using email and password' do
  172. before do
  173. post :create, params: { user: { email: user.email, password: user.password } }
  174. end
  175. it 'renders two factor authentication page' do
  176. expect(controller).to render_template('two_factor')
  177. expect(controller).to render_template(partial: '_otp_authentication_form')
  178. end
  179. end
  180. context 'when using email and password after an unfinished log-in attempt to a 2FA-protected account' do
  181. let!(:other_user) do
  182. Fabricate(:user, email: 'z@y.com', password: 'abcdefgh', otp_required_for_login: true, otp_secret: User.generate_otp_secret)
  183. end
  184. before do
  185. post :create, params: { user: { email: other_user.email, password: other_user.password } }
  186. post :create, params: { user: { email: user.email, password: user.password } }
  187. end
  188. it 'renders two factor authentication page' do
  189. expect(controller).to render_template('two_factor')
  190. expect(controller).to render_template(partial: '_otp_authentication_form')
  191. end
  192. end
  193. context 'when using upcase email and password' do
  194. before do
  195. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  196. end
  197. it 'renders two factor authentication page' do
  198. expect(controller).to render_template('two_factor')
  199. expect(controller).to render_template(partial: '_otp_authentication_form')
  200. end
  201. end
  202. context 'when repeatedly using an invalid TOTP code before using a valid code' do
  203. before do
  204. stub_const('Auth::SessionsController::MAX_2FA_ATTEMPTS_PER_HOUR', 2)
  205. # Travel to the beginning of an hour to avoid crossing rate-limit buckets
  206. travel_to '2023-12-20T10:00:00Z'
  207. end
  208. it 'does not log the user in, sets a flash message, and sends a suspicious sign in email', :inline_jobs do
  209. emails = capture_emails do
  210. Auth::SessionsController::MAX_2FA_ATTEMPTS_PER_HOUR.times do
  211. post :create, params: { user: { otp_attempt: '1234' } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
  212. expect(controller.current_user).to be_nil
  213. end
  214. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
  215. end
  216. expect(controller.current_user)
  217. .to be_nil
  218. expect(flash[:alert])
  219. .to match I18n.t('users.rate_limited')
  220. expect(emails.size)
  221. .to eq(1)
  222. expect(emails.first)
  223. .to have_attributes(
  224. to: contain_exactly(user.email),
  225. subject: eq(I18n.t('user_mailer.failed_2fa.subject'))
  226. )
  227. end
  228. end
  229. context 'when using a valid OTP' do
  230. before do
  231. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
  232. end
  233. it 'redirects to home and logs the user in' do
  234. expect(response).to redirect_to(root_path)
  235. expect(controller.current_user).to eq user
  236. end
  237. end
  238. context 'when the server has an decryption error' do
  239. before do
  240. allow(user).to receive(:validate_and_consume_otp!).with(user.current_otp).and_raise(OpenSSL::Cipher::CipherError)
  241. allow(User).to receive(:find_by).with(id: user.id).and_return(user)
  242. post :create, params: { user: { otp_attempt: user.current_otp } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
  243. end
  244. it 'shows a login error and does not log the user in' do
  245. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  246. expect(controller.current_user).to be_nil
  247. end
  248. end
  249. context 'when using a valid recovery code' do
  250. before do
  251. post :create, params: { user: { otp_attempt: recovery_codes.first } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
  252. end
  253. it 'redirects to home and logs the user in' do
  254. expect(response).to redirect_to(root_path)
  255. expect(controller.current_user).to eq user
  256. end
  257. end
  258. context 'when using an invalid OTP' do
  259. before do
  260. post :create, params: { user: { otp_attempt: 'wrongotp' } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
  261. end
  262. it 'shows a login error and does not log the user in' do
  263. expect(flash[:alert]).to match I18n.t('users.invalid_otp_token')
  264. expect(controller.current_user).to be_nil
  265. end
  266. end
  267. end
  268. context 'with WebAuthn and OTP enabled as second factor' do
  269. let!(:user) do
  270. Fabricate(:user, email: 'x@y.com', password: 'abcdefgh', otp_required_for_login: true, otp_secret: User.generate_otp_secret)
  271. end
  272. let!(:webauthn_credential) do
  273. user.update(webauthn_id: WebAuthn.generate_user_id)
  274. public_key_credential = WebAuthn::Credential.from_create(fake_client.create)
  275. user.webauthn_credentials.create(
  276. nickname: 'SecurityKeyNickname',
  277. external_id: public_key_credential.id,
  278. public_key: public_key_credential.public_key,
  279. sign_count: '1000'
  280. )
  281. user.webauthn_credentials.take
  282. end
  283. let(:domain) { "#{Rails.configuration.x.use_https ? 'https' : 'http'}://#{Rails.configuration.x.web_domain}" }
  284. let(:fake_client) { WebAuthn::FakeClient.new(domain) }
  285. let(:challenge) { WebAuthn::Credential.options_for_get.challenge }
  286. let(:sign_count) { 1234 }
  287. let(:fake_credential) { fake_client.get(challenge: challenge, sign_count: sign_count) }
  288. before do
  289. user.generate_otp_backup_codes!
  290. user.save
  291. end
  292. context 'when using email and password' do
  293. before do
  294. post :create, params: { user: { email: user.email, password: user.password } }
  295. end
  296. it 'renders webauthn authentication page' do
  297. expect(controller).to render_template('two_factor')
  298. expect(controller).to render_template(partial: '_webauthn_form')
  299. end
  300. end
  301. context 'when using upcase email and password' do
  302. before do
  303. post :create, params: { user: { email: user.email.upcase, password: user.password } }
  304. end
  305. it 'renders webauthn authentication page' do
  306. expect(controller).to render_template('two_factor')
  307. expect(controller).to render_template(partial: '_webauthn_form')
  308. end
  309. end
  310. context 'when using a valid webauthn credential' do
  311. before do
  312. controller.session[:webauthn_challenge] = challenge
  313. post :create, params: { user: { credential: fake_credential } }, session: { attempt_user_id: user.id, attempt_user_updated_at: user.updated_at.to_s }
  314. end
  315. it 'instructs the browser to redirect to home, logs the user in, and updates the sign count' do
  316. expect(response.parsed_body[:redirect_path]).to eq(root_path)
  317. expect(controller.current_user).to eq user
  318. expect(webauthn_credential.reload.sign_count).to eq(sign_count)
  319. end
  320. end
  321. end
  322. end
  323. end
  324. end