Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow to map internal UID from attributes

Oliver Güntherae65b35c4eb5
diff --git a/app/controllers/concerns/accounts/omniauth_login.rb b/app/controllers/concerns/accounts/omniauth_login.rb
index 263e3242257..17bb2a345e5 100644
--- a/app/controllers/concerns/accounts/omniauth_login.rb
+++ b/app/controllers/concerns/accounts/omniauth_login.rb
@@ -202,8 +202,14 @@ module Accounts::OmniauthLogin
end
end
+ ##
+ # Allow strategies to map a value for uid instead
+ # of always taking the global UID.
+ # For SAML, the global UID may change with every session
+ # (in case of transient nameIds)
def identity_url_from_omniauth(auth)
- "#{auth[:provider]}:#{auth[:uid]}"
+ identifier = auth[:info][:uid] || auth[:uid]
+ "#{auth[:provider]}:#{identifier}"
end
# if the omni auth registration happened too long ago,
diff --git a/spec/controllers/concerns/omniauth_login_spec.rb b/spec/controllers/concerns/omniauth_login_spec.rb
index e753428cffb..c75b9da00b6 100644
--- a/spec/controllers/concerns/omniauth_login_spec.rb
+++ b/spec/controllers/concerns/omniauth_login_spec.rb
@@ -72,6 +72,35 @@ describe AccountController, type: :controller do
end
end
+ describe 'strategy uid mapping override' do
+ let(:omniauth_strategy) { double('Google Strategy') }
+ let(:omniauth_hash) do
+ OmniAuth::AuthHash.new(
+ provider: 'google',
+ uid: 'foo',
+ info: {
+ uid: 'internal',
+ email: '[email protected]',
+ first_name: 'what',
+ last_name: 'theheck'
+ }
+ )
+ end
+
+ before do
+ request.env['omniauth.auth'] = omniauth_hash
+ request.env['omniauth.strategy'] = omniauth_strategy
+ end
+
+ it 'takes the uid from the mapped attributes' do
+ post :omniauth_login, params: { provider: :google }
+
+ user = User.find_by_login('[email protected]')
+ expect(user).to be_an_instance_of(User)
+ expect(user.identity_url).to eq 'google:internal'
+ end
+ end
+
describe 'strategy attribute mapping override' do
let(:omniauth_strategy) { double('Google Strategy') }
let(:omniauth_hash) do
@@ -512,11 +541,20 @@ describe AccountController, type: :controller do
end
describe '#identity_url_from_omniauth' do
- let(:omniauth_hash) { { provider: 'developer', uid: 'veryuniqueid' } }
+ let(:omniauth_hash) { { provider: 'developer', uid: 'veryuniqueid', info: {} } }
it 'should return the correct identity_url' do
result = AccountController.new.send(:identity_url_from_omniauth, omniauth_hash)
expect(result).to eql('developer:veryuniqueid')
end
+
+ context 'with uid mapped from info' do
+ let(:omniauth_hash) { { provider: 'developer', uid: 'veryuniqueid', info: { uid: 'internal'} } }
+
+ it 'should return the correct identity_url' do
+ result = AccountController.new.send(:identity_url_from_omniauth, omniauth_hash)
+ expect(result).to eql('developer:internal')
+ end
+ end
end
end