2015-10-06 11:21:20 +02:00
|
|
|
class FranceConnectController < ApplicationController
|
2015-10-06 16:02:30 +02:00
|
|
|
def login
|
2015-10-06 11:21:20 +02:00
|
|
|
client = FranceConnectClient.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]
|
|
|
|
)
|
|
|
|
redirect_to authorization_uri
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback
|
2015-10-06 16:17:15 +02:00
|
|
|
return redirect_to new_user_session_path unless params.has_key?(:code)
|
|
|
|
|
2015-10-06 11:47:42 +02:00
|
|
|
user_infos = FranceConnectService.retrieve_user_informations(params[:code])
|
2015-10-06 11:21:20 +02:00
|
|
|
|
|
|
|
unless user_infos.nil?
|
2015-10-07 10:29:44 +02:00
|
|
|
@user = User.find_for_france_connect(user_infos.email, user_infos.siret)
|
2015-10-06 11:21:20 +02:00
|
|
|
|
|
|
|
sign_in @user
|
|
|
|
|
2015-10-08 11:26:12 +02:00
|
|
|
@user.loged_in_with_france_connect = true
|
|
|
|
@user.save
|
2015-10-07 11:24:07 +02:00
|
|
|
|
2015-10-07 16:49:42 +02:00
|
|
|
redirect_to stored_location_for(current_user) || signed_in_root_path(current_user)
|
2015-10-06 11:21:20 +02:00
|
|
|
end
|
2015-10-06 16:47:25 +02:00
|
|
|
rescue Rack::OAuth2::Client::Error => e
|
|
|
|
Rails.logger.error e.message
|
2015-10-06 17:41:49 +02:00
|
|
|
flash.alert = t('errors.messages.france_connect.connexion')
|
2015-10-06 16:47:25 +02:00
|
|
|
redirect_to(new_user_session_path)
|
2015-10-06 11:21:20 +02:00
|
|
|
end
|
|
|
|
end
|