1 # frozen_string_literal: true
3 class UsersController < ApplicationController
10 skip_before_action :verify_authenticity_token, :only => [:auth_success]
11 before_action :authorize_web
12 before_action :set_locale
13 before_action :check_database_readable
17 before_action :check_database_writable, :only => [:new, :go_public]
18 before_action :require_cookies, :only => [:new]
20 allow_thirdparty_images :only => :show
21 allow_social_login :only => :new
24 @user = User.find_by(:display_name => params[:display_name])
26 if @user && (@user.visible? || current_user&.administrator?)
27 @title = @user.display_name
28 @heatmap_frame = @user.public_heatmap?
30 render_unknown_user params[:display_name]
36 @referer = safe_referer(params[:referer])
38 parse_oauth_referer @referer
41 # The user is logged in already, so don't show them the signup
42 # page, instead send them to the home page
43 redirect_to @referer || { :controller => "site", :action => "index" }
44 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
45 @email_hmac = params[:email_hmac]
47 self.current_user = User.new(:email => params[:email],
48 :display_name => params[:nickname],
49 :auth_provider => params[:auth_provider],
50 :auth_uid => params[:auth_uid])
52 if current_user.valid? || current_user.errors[:email].empty?
53 flash.now[:notice] = render_to_string :partial => "auth_association"
55 flash.now[:warning] = t ".duplicate_social_email"
57 elsif check_signup_allowed?
58 self.current_user = User.new
60 render :action => "blocked"
65 self.current_user = User.new(user_params)
67 if check_signup_allowed?(current_user.email)
68 if current_user.auth_uid.present?
69 # We are creating an account with external authentication and
70 # no password was specified so create a random one
71 current_user.pass_crypt = SecureRandom.base64(16)
72 current_user.pass_crypt_confirmation = current_user.pass_crypt
75 if current_user.invalid?
76 # Something is wrong with a new user, so rerender the form
77 render :action => "new"
79 # Save the user record
80 if save_new_user params[:email_hmac]
81 SIGNUP_IP_LIMITER&.update(request.remote_ip)
82 SIGNUP_EMAIL_LIMITER&.update(canonical_email(current_user.email))
84 flash[:matomo_goal] = Settings.matomo["goals"]["signup"] if defined?(Settings.matomo)
86 referer = welcome_path(welcome_options(params[:referer]))
88 if current_user.status == "active"
89 successful_login(current_user, referer)
91 session[:pending_user] = current_user.id
93 :user => current_user,
94 :token => current_user.generate_token_for(:new_user),
96 ).signup_confirm.deliver_later
97 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
100 render :action => "new", :referer => params[:referer]
104 render :action => "blocked"
109 current_user.data_public = true
111 flash[:notice] = t ".flash success"
112 redirect_to account_path
116 # omniauth success callback
118 referer = request.env["omniauth.params"]["referer"]
119 auth_info = request.env["omniauth.auth"]
121 provider = auth_info[:provider]
122 uid = auth_info[:uid]
123 name = auth_info[:info][:name]
124 email = auth_info[:info][:email]
126 email_verified = case provider
127 when "google", "apple", "facebook", "microsoft", "github", "wikipedia"
133 if settings = session.delete(:new_user_settings)
134 current_user.auth_provider = provider
135 current_user.auth_uid = uid
137 update_user(current_user, settings)
141 session[:user_errors] = current_user.errors.as_json
143 redirect_to account_path
145 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
147 if user.nil? && provider == "google"
148 openid_url = auth_info[:extra][:id_info]["openid_id"]
149 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
150 user&.update(:auth_provider => provider, :auth_uid => uid)
156 unconfirmed_login(user, referer)
157 when "active", "confirmed"
158 successful_login(user, referer)
160 failed_login({ :partial => "sessions/suspended_flash" }, user.display_name, referer)
162 failed_login(t("sessions.new.auth failure"), user.display_name, referer)
165 email_hmac = UsersController.message_hmac(email) if email_verified && email
166 redirect_to :action => "new", :nickname => name, :email => email, :email_hmac => email_hmac,
167 :auth_provider => provider, :auth_uid => uid, :referer => referer
173 # omniauth failure callback
175 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t(".unknown_error"))
177 origin = safe_referer(params[:origin]) if params[:origin]
179 redirect_to origin || login_url
182 def self.message_hmac(text)
183 sha256 = Digest::SHA256.new
184 sha256 << Rails.application.key_generator.generate_key("openstreetmap/email_address")
186 Base64.urlsafe_encode64(sha256.digest)
191 def save_new_user(email_hmac)
192 current_user.data_public = true
193 current_user.description = "" if current_user.description.nil?
194 current_user.creation_address = request.remote_ip
195 current_user.languages = if request.cookies["_osm_locale"]
196 Locale.list(request.cookies["_osm_locale"])
198 http_accept_language.user_preferred_languages
200 current_user.terms_agreed = Time.now.utc
201 current_user.tou_agreed = Time.now.utc
202 current_user.terms_seen = true
204 if current_user.auth_uid.blank?
205 current_user.auth_provider = nil
206 current_user.auth_uid = nil
207 elsif email_hmac && ActiveSupport::SecurityUtils.secure_compare(email_hmac, UsersController.message_hmac(current_user.email))
208 current_user.activate
214 def welcome_options(referer = nil)
215 uri = URI(referer) if referer.present?
217 return { "oauth_return_url" => uri&.to_s } if uri&.path == oauth_authorization_path
220 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
221 editor = Rack::Utils.parse_query(uri.query).slice("editor")
222 return { "zoom" => m[1], "lat" => m[2], "lon" => m[3] }.merge(editor)
230 # return permitted user parameters
232 params.expect(:user => [:email, :display_name,
233 :auth_provider, :auth_uid,
234 :pass_crypt, :pass_crypt_confirmation])
239 def check_signup_allowed?(email = nil)
240 domain = if email.nil?
243 email.split("@").last
246 mx_servers = if domain.nil?
249 domain_mx_servers(domain)
252 return true if Acl.allow_account_creation?(request.remote_ip, :domain => domain, :mx => mx_servers)
254 blocked = Acl.no_account_creation?(request.remote_ip, :domain => domain, :mx => mx_servers)
256 blocked ||= SIGNUP_IP_LIMITER && !SIGNUP_IP_LIMITER.allow?(request.remote_ip)
258 blocked ||= email && SIGNUP_EMAIL_LIMITER && !SIGNUP_EMAIL_LIMITER.allow?(canonical_email(email))
260 logger.info "Blocked signup from #{request.remote_ip} for #{email}" if blocked