Sign in with a user linked by france connect sub (openid)

instead of looking linked user by email because :

- follows FC recommendation to fetch ds account by openid
- the email is not a valid key as many user can share the same FCI email.

The following scenario is now working

A user A (email: 1@mail.com) uses FC to connect to DS
=> It is connected as 1@mail.com

Another user B (email: generic@mail.com) uses FC to connect
=> It is connected as generic@mail.com

The first user A change its FC email to generic@mail.com and connect to DS
=> It is still connected as 1@mail.com
This commit is contained in:
simon lehericey 2021-10-11 11:39:14 +02:00
parent 06dee2e023
commit 6826bf03b0
4 changed files with 40 additions and 45 deletions

View file

@ -11,7 +11,10 @@ class FranceConnect::ParticulierController < ApplicationController
def callback
fci = FranceConnectService.find_or_retrieve_france_connect_information(params[:code])
if fci.user.nil?
fci.associate_user!
end
user = fci.user

View file

@ -21,9 +21,6 @@ class FranceConnectInformation < ApplicationRecord
validates :france_connect_particulier_id, presence: true, allow_blank: false, allow_nil: false
def associate_user!
user = User.find_by(email: email_france_connect.downcase)
if user.nil?
begin
user = User.create!(
email: email_france_connect.downcase,
@ -36,7 +33,6 @@ class FranceConnectInformation < ApplicationRecord
# At the 2nd call, user is nil but exception is raised at the creation of the user
# because the first call has already created a user
end
end
update_attribute('user_id', user.id)
touch # needed to update updated_at column

View file

@ -1,6 +1,6 @@
describe FranceConnect::ParticulierController, type: :controller do
let(:birthdate) { '20150821' }
let(:email) { 'test@test.com' }
let(:email) { 'email_from_fc@test.com' }
let(:user_info) do
{
@ -49,15 +49,18 @@ describe FranceConnect::ParticulierController, type: :controller do
.and_return(FranceConnectInformation.new(user_info))
end
context 'when france_connect_particulier_id exist in database' do
let!(:france_connect_information) { create(:france_connect_information, :with_user, user_info) }
let(:user) { france_connect_information.user }
context 'when france_connect_particulier_id exists in database' do
let!(:fci) { FranceConnectInformation.create!(user_info.merge(user_id: fc_user.id)) }
context 'and is linked to an user' do
let(:fc_user) { create(:user, email: 'associated_user@a.com') }
it { expect { subject }.not_to change { FranceConnectInformation.count } }
it do
it 'signs in with the fci associated user' do
subject
expect(user.reload.loged_in_with_france_connect).to eq(User.loged_in_with_france_connects.fetch(:particulier))
expect(controller.current_user).to eq(fc_user)
expect(fc_user.reload.loged_in_with_france_connect).to eq(User.loged_in_with_france_connects.fetch(:particulier))
end
context 'and the user has a stored location' do
@ -66,14 +69,17 @@ describe FranceConnect::ParticulierController, type: :controller do
it { is_expected.to redirect_to(stored_location) }
end
end
context 'and is linked an instructeur' do
let(:fc_user) { create(:instructeur, email: 'another_email@a.com').user }
context 'and the user is also instructeur' do
let!(:instructeur) { create(:instructeur, email: email) }
before { subject }
it { expect(response).to redirect_to(new_user_session_path) }
it { expect(flash[:alert]).to be_present }
it do
expect(response).to redirect_to(new_user_session_path)
expect(flash[:alert]).to be_present
end
end
end

View file

@ -18,15 +18,5 @@ describe FranceConnectInformation, type: :model do
expect(fci.user.email).to eq(fci.email_france_connect)
end
end
context 'when a user with same email (but who is not an instructeur) exist' do
let(:user) { create(:user) }
let(:fci) { build(:france_connect_information, email_france_connect: user.email) }
let(:subject) { fci.associate_user! }
before { subject }
it { expect(fci.user).to eq(user) }
end
end
end