]> git.openstreetmap.org Git - rails.git/blob - app/controllers/sessions_controller.rb
Improve fallback behaviour for unsafe referer redirects
[rails.git] / app / controllers / sessions_controller.rb
1 class SessionsController < ApplicationController
2   include SessionMethods
3
4   layout "site"
5
6   before_action :disable_terms_redirect, :only => [:destroy]
7   before_action :authorize_web
8   before_action :set_locale
9   before_action :check_database_readable
10   before_action :require_cookies, :only => [:new]
11
12   authorize_resource :class => false
13
14   def new
15     append_content_security_policy_directives(
16       :form_action => %w[*]
17     )
18
19     session[:referer] = safe_referer(params[:referer]) if params[:referer]
20   end
21
22   def create
23     session[:remember_me] ||= params[:remember_me]
24     session[:referer] = safe_referer(params[:referer]) if params[:referer]
25     password_authentication(params[:username], params[:password])
26   end
27
28   def destroy
29     @title = t "sessions.destroy.title"
30
31     if request.post?
32       if session[:token]
33         token = UserToken.find_by(:token => session[:token])
34         token&.destroy
35         session.delete(:token)
36       end
37
38       session.delete(:user)
39       session_expires_automatically
40
41       referer = safe_referer(params[:referer]) if params[:referer]
42
43       if referer
44         redirect_to referer
45       else
46         redirect_to :controller => "site", :action => "index"
47       end
48     end
49   end
50
51   private
52
53   ##
54   # handle password authentication
55   def password_authentication(username, password)
56     if (user = User.authenticate(:username => username, :password => password))
57       successful_login(user)
58     elsif (user = User.authenticate(:username => username, :password => password, :pending => true))
59       unconfirmed_login(user)
60     elsif User.authenticate(:username => username, :password => password, :suspended => true)
61       failed_login t("sessions.new.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe, username
62     else
63       failed_login t("sessions.new.auth failure"), username
64     end
65   end
66 end