session_spec.rb 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Sessions endpoints', type: :request do
  4. describe 'GET /' do
  5. let(:headers) { {} }
  6. let(:session_key) { Zammad::Application::Initializer::SessionStore::SESSION_KEY }
  7. before do
  8. Setting.set('http_type', http_type)
  9. get '/', headers: headers
  10. end
  11. context "when Setting 'http_type' is set to 'https'" do
  12. let(:http_type) { 'https' }
  13. context "when it's not an HTTPS request" do
  14. it 'sets no Cookie' do
  15. expect(response.header['Set-Cookie']).to be_nil
  16. end
  17. end
  18. context "when it's an HTTPS request" do
  19. let(:headers) do
  20. {
  21. 'X-Forwarded-Proto' => 'https'
  22. }
  23. end
  24. it "sets Cookie with 'secure' flag" do
  25. expect(response.header['Set-Cookie']).to include(session_key).and include('; secure;')
  26. end
  27. end
  28. end
  29. context "when Setting 'http_type' is set to 'http'" do
  30. let(:http_type) { 'http' }
  31. context "when it's not an HTTPS request" do
  32. it 'sets Cookie' do
  33. expect(response.header['Set-Cookie']).to include(session_key).and not_include('; secure;')
  34. end
  35. end
  36. context "when it's an HTTPS request" do
  37. let(:headers) do
  38. {
  39. 'X-Forwarded-Proto' => 'https'
  40. }
  41. end
  42. it "sets Cookie without 'secure' flag" do
  43. expect(response.header['Set-Cookie']).to include(session_key).and not_include('; secure;')
  44. end
  45. end
  46. end
  47. end
  48. describe 'GET /signshow' do
  49. context 'user logged in' do
  50. subject(:user) { create(:agent, password: password) }
  51. let(:password) { SecureRandom.urlsafe_base64(20) }
  52. let(:fingerprint) { SecureRandom.urlsafe_base64(40) }
  53. before do
  54. setting if defined?(setting)
  55. params = {
  56. fingerprint: fingerprint,
  57. username: user.login,
  58. password: password
  59. }
  60. post '/api/v1/signin', params: params, as: :json
  61. end
  62. it 'leaks no sensitive data' do
  63. params = { fingerprint: fingerprint }
  64. get '/api/v1/signshow', params: params, as: :json
  65. expect(json_response['session']).not_to include('password')
  66. end
  67. context 'when after auth modules are triggered' do
  68. subject(:user) { create(:customer, roles: [role], password: password) }
  69. let(:role) { create(:role, name: '2FA') }
  70. context 'with no enforcing roles' do
  71. it 'returns nil' do
  72. expect(json_response['after_auth']).to be_nil
  73. end
  74. end
  75. context 'with enforcing roles' do
  76. let(:setting) do
  77. Setting.set('two_factor_authentication_enforce_role_ids', [role.id])
  78. Setting.set('two_factor_authentication_method_authenticator_app', true)
  79. end
  80. it 'returns the after auth information' do
  81. expect(json_response['after_auth']).to eq({ 'data' => {}, 'type' => 'TwoFactorConfiguration' })
  82. end
  83. end
  84. end
  85. end
  86. context 'user not logged in' do
  87. subject(:user) { nil }
  88. it 'contains only user related object manager attributes' do
  89. get '/api/v1/signshow', params: {}, as: :json
  90. expect(json_response['models'].keys).to match_array(%w[User])
  91. end
  92. end
  93. end
  94. describe 'GET /auth/sso (single sign-on)' do
  95. before do
  96. Setting.set('auth_sso', true)
  97. end
  98. context 'when SSO is disabled' do
  99. before do
  100. Setting.set('auth_sso', false)
  101. end
  102. let(:headers) { { 'X-Forwarded-User' => login } }
  103. let(:login) { User.last.login }
  104. it 'returns a new user-session response' do
  105. get '/auth/sso', as: :json, headers: headers
  106. expect(response).to have_http_status(:forbidden)
  107. end
  108. end
  109. context 'with invalid user login' do
  110. let(:login) { User.pluck(:login).max.next }
  111. context 'in "REMOTE_USER" request env var' do
  112. let(:env) { { 'REMOTE_USER' => login } }
  113. it 'returns unauthorized response' do
  114. get '/auth/sso', as: :json, env: env
  115. expect(response).to have_http_status(:unauthorized)
  116. end
  117. end
  118. context 'in "HTTP_REMOTE_USER" request env var' do
  119. let(:env) { { 'HTTP_REMOTE_USER' => login } }
  120. it 'returns unauthorized response' do
  121. get '/auth/sso', as: :json, env: env
  122. expect(response).to have_http_status(:unauthorized)
  123. end
  124. end
  125. context 'in "X-Forwarded-User" request header' do
  126. let(:headers) { { 'X-Forwarded-User' => login } }
  127. it 'returns unauthorized response' do
  128. get '/auth/sso', as: :json, headers: headers
  129. expect(response).to have_http_status(:unauthorized)
  130. end
  131. end
  132. end
  133. context 'with valid user login' do
  134. let(:user) { create(:agent) }
  135. let(:login) { user.login }
  136. context 'in Maintenance Mode' do
  137. before { Setting.set('maintenance_mode', true) }
  138. context 'in "REMOTE_USER" request env var' do
  139. let(:env) { { 'REMOTE_USER' => login } }
  140. it 'returns 403 Forbidden' do
  141. get '/auth/sso', as: :json, env: env
  142. expect(response).to have_http_status(:forbidden)
  143. expect(json_response).to include('error' => 'Maintenance mode enabled!')
  144. end
  145. end
  146. context 'in "HTTP_REMOTE_USER" request env var' do
  147. let(:env) { { 'HTTP_REMOTE_USER' => login } }
  148. it 'returns 403 Forbidden' do
  149. get '/auth/sso', as: :json, env: env
  150. expect(response).to have_http_status(:forbidden)
  151. expect(json_response).to include('error' => 'Maintenance mode enabled!')
  152. end
  153. end
  154. context 'in "X-Forwarded-User" request header' do
  155. let(:headers) { { 'X-Forwarded-User' => login } }
  156. it 'returns 403 Forbidden' do
  157. get '/auth/sso', as: :json, headers: headers
  158. expect(response).to have_http_status(:forbidden)
  159. expect(json_response).to include('error' => 'Maintenance mode enabled!')
  160. end
  161. end
  162. end
  163. context 'in "REMOTE_USER" request env var' do
  164. let(:env) { { 'REMOTE_USER' => login } }
  165. it 'returns a new user-session response' do
  166. get '/auth/sso', as: :json, env: env
  167. expect(response).to redirect_to('/#')
  168. end
  169. it 'sets the :user_id session parameter' do
  170. expect { get '/auth/sso', as: :json, env: env }
  171. .to change { request&.session&.fetch(:user_id) }.to(user.id)
  172. end
  173. end
  174. context 'in "HTTP_REMOTE_USER" request env var' do
  175. let(:env) { { 'HTTP_REMOTE_USER' => login } }
  176. it 'returns a new user-session response' do
  177. get '/auth/sso', as: :json, env: env
  178. expect(response).to redirect_to('/#')
  179. end
  180. it 'sets the :user_id session parameter' do
  181. expect { get '/auth/sso', as: :json, env: env }
  182. .to change { request&.session&.fetch(:user_id) }.to(user.id)
  183. end
  184. end
  185. context 'in "X-Forwarded-User" request header' do
  186. let(:headers) { { 'X-Forwarded-User' => login } }
  187. it 'returns a new user-session response' do
  188. get '/auth/sso', as: :json, headers: headers
  189. expect(response).to redirect_to('/#')
  190. end
  191. it 'sets the :user_id session parameter on the client' do
  192. expect { get '/auth/sso', as: :json, headers: headers }
  193. .to change { request&.session&.fetch(:user_id) }.to(user.id)
  194. end
  195. end
  196. end
  197. end
  198. describe 'POST /auth/two_factor_initiate_authentication/:method' do
  199. let(:user) { create(:user, password: 'dummy') }
  200. let(:params) { {} }
  201. let(:method) { 'security_keys' }
  202. let(:user_two_factor_preference) { nil }
  203. before do
  204. if defined?(user_two_factor_preference)
  205. user_two_factor_preference
  206. user.reload
  207. end
  208. post "/api/v1/auth/two_factor_initiate_authentication/#{method}", params: params, as: :json
  209. end
  210. context 'with missing params' do
  211. it 'returns an error' do
  212. expect(response).to have_http_status(:unprocessable_entity)
  213. end
  214. end
  215. context 'with valid params' do
  216. let(:user_two_factor_preference) { create(:user_two_factor_preference, :security_keys, user: user) }
  217. let(:params) { { username: user.login, password: password, method: method } }
  218. context 'with invalid user/password' do
  219. let(:password) { 'invalid' }
  220. it 'returns an error' do
  221. expect(response).to have_http_status(:unprocessable_entity)
  222. end
  223. end
  224. context 'with valid user/password' do
  225. let(:password) { 'dummy' }
  226. it 'returns options for initiation phase', :aggregate_failures do
  227. expect(response).to have_http_status(:ok)
  228. expect(json_response).to include('challenge')
  229. end
  230. end
  231. end
  232. end
  233. end