2015-12-24 10:12:23 +01:00
|
|
|
class FranceConnect::ParticulierController < ApplicationController
|
|
|
|
def login
|
|
|
|
client = FranceConnectParticulierClient.new
|
|
|
|
|
|
|
|
session[:state] = SecureRandom.hex(16)
|
|
|
|
session[:nonce] = SecureRandom.hex(16)
|
|
|
|
|
|
|
|
authorization_uri = client.authorization_uri(
|
|
|
|
scope: [:profile, :email],
|
|
|
|
state: session[:state],
|
|
|
|
nonce: session[:nonce]
|
|
|
|
)
|
2016-03-24 17:49:44 +01:00
|
|
|
redirect_to URI.parse(authorization_uri).to_s
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
def callback
|
2018-01-11 19:04:39 +01:00
|
|
|
return redirect_to new_user_session_path if !params.has_key?(:code)
|
2016-01-05 12:18:00 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
user_infos = FranceConnectService.retrieve_user_informations_particulier(params[:code])
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
if user_infos.present?
|
2016-01-21 17:06:09 +01:00
|
|
|
france_connect_information = FranceConnectInformation.find_by_france_connect_particulier user_infos
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
france_connect_information = FranceConnectInformation.create(
|
|
|
|
{gender: user_infos[:gender],
|
|
|
|
given_name: user_infos[:given_name],
|
|
|
|
family_name: user_infos[:family_name],
|
|
|
|
email_france_connect: user_infos[:email],
|
|
|
|
birthdate: user_infos[:birthdate],
|
|
|
|
birthplace: user_infos[:birthplace],
|
|
|
|
france_connect_particulier_id: user_infos[:france_connect_particulier_id]}
|
|
|
|
) if france_connect_information.nil?
|
|
|
|
|
|
|
|
user = france_connect_information.user
|
|
|
|
salt = FranceConnectSaltService.new(france_connect_information).salt
|
|
|
|
|
|
|
|
return redirect_to france_connect_particulier_new_path(fci_id: france_connect_information.id, salt: salt) if user.nil?
|
|
|
|
|
|
|
|
connect_france_connect_particulier user
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
2016-01-21 17:06:09 +01:00
|
|
|
rescue Rack::OAuth2::Client::Error => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
redirect_france_connect_error_connection
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
def new
|
2018-01-11 19:04:39 +01:00
|
|
|
return redirect_france_connect_error_connection if !valid_salt_and_fci_id_params?
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
@user = User.new(france_connect_information: france_connect_information).decorate
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
redirect_france_connect_error_connection
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-05 12:18:00 +01:00
|
|
|
def check_email
|
2018-01-11 19:04:39 +01:00
|
|
|
return redirect_france_connect_error_connection if !valid_salt_and_fci_id_params?
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
user = User.find_by_email(params[:user][:email_france_connect])
|
2016-01-19 17:19:38 +01:00
|
|
|
|
|
|
|
return create if user.nil?
|
2016-01-05 12:18:00 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
if params[:user][:password].present?
|
2016-01-05 12:18:00 +01:00
|
|
|
|
2016-01-19 17:19:38 +01:00
|
|
|
if user.valid_password?(params[:user][:password])
|
2016-01-21 17:06:09 +01:00
|
|
|
user.france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
|
2016-01-05 12:18:00 +01:00
|
|
|
return connect_france_connect_particulier user
|
|
|
|
else
|
|
|
|
flash.now.alert = 'Mot de passe invalide'
|
|
|
|
end
|
|
|
|
end
|
2016-01-19 17:19:38 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
france_connect_information.update_attribute(:email_france_connect, params[:user][:email_france_connect])
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
@user = User.new(france_connect_information: france_connect_information).decorate
|
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
def create
|
|
|
|
user = User.new email: params[:user][:email_france_connect]
|
|
|
|
user.password = Devise.friendly_token[0, 20]
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
if !user.valid?
|
2016-01-21 17:06:09 +01:00
|
|
|
flash.alert = 'Email non valide'
|
2016-11-15 04:42:32 +01:00
|
|
|
|
|
|
|
return redirect_to france_connect_particulier_new_path fci_id: params[:fci_id], salt: params[:salt], user: {email_france_connect: params[:user]['email_france_connect']}
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
user.save
|
|
|
|
FranceConnectInformation.find(params[:fci_id]).update_attribute(:user, user)
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
connect_france_connect_particulier user
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
private
|
2016-01-05 12:18:00 +01:00
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
def connect_france_connect_particulier user
|
2016-01-26 16:48:33 +01:00
|
|
|
sign_out :user if user_signed_in?
|
|
|
|
sign_out :gestionnaire if gestionnaire_signed_in?
|
|
|
|
sign_out :administrateur if administrateur_signed_in?
|
|
|
|
|
2015-12-24 10:12:23 +01:00
|
|
|
sign_in user
|
|
|
|
|
|
|
|
user.loged_in_with_france_connect = 'particulier'
|
|
|
|
user.save
|
|
|
|
|
|
|
|
redirect_to stored_location_for(current_user) || signed_in_root_path(current_user)
|
|
|
|
end
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
def redirect_france_connect_error_connection
|
|
|
|
flash.alert = t('errors.messages.france_connect.connexion')
|
|
|
|
redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_salt_and_fci_id_params?
|
|
|
|
france_connect_information = FranceConnectInformation.find(params[:fci_id])
|
|
|
|
FranceConnectSaltService.new(france_connect_information).valid? params[:salt]
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|