2013-09-03 17:04:27 -04:00
|
|
|
# OAuth extension for User model
|
|
|
|
#
|
|
|
|
# * Find GitLab user based on omniauth uid and provider
|
|
|
|
# * Create new user from omniauth data
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module OAuth
|
2017-03-01 06:00:37 -05:00
|
|
|
SignupDisabledError = Class.new(StandardError)
|
2014-11-27 06:34:39 -05:00
|
|
|
|
2013-09-03 17:04:27 -04:00
|
|
|
class User
|
2014-10-10 06:03:32 -04:00
|
|
|
attr_accessor :auth_hash, :gl_user
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def initialize(auth_hash)
|
|
|
|
self.auth_hash = auth_hash
|
2017-06-06 11:39:54 -04:00
|
|
|
update_email
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def persisted?
|
2014-10-16 14:08:30 -04:00
|
|
|
gl_user.try(:persisted?)
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def new?
|
2014-10-17 07:08:02 -04:00
|
|
|
!persisted?
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def valid?
|
2014-10-16 14:08:30 -04:00
|
|
|
gl_user.try(:valid?)
|
2014-09-03 09:59:50 -04:00
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2016-02-18 17:01:07 -05:00
|
|
|
def save(provider = 'OAuth')
|
2014-10-16 14:08:30 -04:00
|
|
|
unauthorized_to_create unless gl_user
|
|
|
|
|
2017-02-22 18:07:29 -05:00
|
|
|
block_after_save = needs_blocking?
|
|
|
|
|
2017-06-23 05:34:07 -04:00
|
|
|
Users::UpdateService.new(gl_user).execute!
|
2017-02-22 13:43:48 -05:00
|
|
|
|
2017-02-22 18:07:29 -05:00
|
|
|
gl_user.block if block_after_save
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2016-02-18 17:01:07 -05:00
|
|
|
log.info "(#{provider}) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}"
|
2014-10-10 06:03:32 -04:00
|
|
|
gl_user
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2016-11-30 12:23:04 -05:00
|
|
|
log.info "(#{provider}) Error saving user #{auth_hash.uid} (#{auth_hash.email}): #{gl_user.errors.full_messages}"
|
2014-10-10 06:03:32 -04:00
|
|
|
return self, e.record.errors
|
|
|
|
end
|
|
|
|
|
|
|
|
def gl_user
|
2014-10-16 14:08:30 -04:00
|
|
|
@user ||= find_by_uid_and_provider
|
|
|
|
|
2015-06-02 06:01:29 -04:00
|
|
|
if auto_link_ldap_user?
|
|
|
|
@user ||= find_or_create_ldap_user
|
|
|
|
end
|
|
|
|
|
2014-10-17 07:08:02 -04:00
|
|
|
if signup_enabled?
|
2014-10-16 14:08:30 -04:00
|
|
|
@user ||= build_new_user
|
|
|
|
end
|
2014-10-17 07:08:02 -04:00
|
|
|
|
2016-04-11 18:26:01 -04:00
|
|
|
if external_provider? && @user
|
|
|
|
@user.external = true
|
2016-04-11 11:16:15 -04:00
|
|
|
end
|
|
|
|
|
2014-10-16 14:08:30 -04:00
|
|
|
@user
|
2014-09-03 09:59:50 -04:00
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
protected
|
2014-10-17 07:08:02 -04:00
|
|
|
|
2015-06-02 06:01:29 -04:00
|
|
|
def find_or_create_ldap_user
|
|
|
|
return unless ldap_person
|
2015-06-05 06:32:01 -04:00
|
|
|
|
|
|
|
# If a corresponding person exists with same uid in a LDAP server,
|
2016-06-07 12:19:19 -04:00
|
|
|
# check if the user already has a GitLab account.
|
2016-06-08 19:09:43 -04:00
|
|
|
user = Gitlab::LDAP::User.find_by_uid_and_provider(ldap_person.dn, ldap_person.provider)
|
|
|
|
if user
|
2016-06-07 12:19:19 -04:00
|
|
|
# Case when a LDAP user already exists in Gitlab. Add the OAuth identity to existing account.
|
2016-06-08 19:09:43 -04:00
|
|
|
log.info "LDAP account found for user #{user.username}. Building new #{auth_hash.provider} identity."
|
2016-06-16 16:11:09 -04:00
|
|
|
user.identities.find_or_initialize_by(extern_uid: auth_hash.uid, provider: auth_hash.provider)
|
2015-06-02 06:01:29 -04:00
|
|
|
else
|
2016-06-08 19:09:43 -04:00
|
|
|
log.info "No existing LDAP account was found in GitLab. Checking for #{auth_hash.provider} account."
|
2016-06-07 12:19:19 -04:00
|
|
|
user = find_by_uid_and_provider
|
|
|
|
if user.nil?
|
2016-06-08 19:09:43 -04:00
|
|
|
log.info "No user found using #{auth_hash.provider} provider. Creating a new one."
|
2016-06-07 12:19:19 -04:00
|
|
|
user = build_new_user
|
|
|
|
end
|
|
|
|
log.info "Correct account has been found. Adding LDAP identity to user: #{user.username}."
|
2015-06-02 06:01:29 -04:00
|
|
|
user.identities.new(provider: ldap_person.provider, extern_uid: ldap_person.dn)
|
|
|
|
end
|
2015-06-05 06:32:01 -04:00
|
|
|
|
2015-06-02 06:01:29 -04:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_link_ldap_user?
|
|
|
|
Gitlab.config.omniauth.auto_link_ldap_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def creating_linked_ldap_user?
|
|
|
|
auto_link_ldap_user? && ldap_person
|
|
|
|
end
|
|
|
|
|
|
|
|
def ldap_person
|
|
|
|
return @ldap_person if defined?(@ldap_person)
|
|
|
|
|
2015-06-17 12:06:27 -04:00
|
|
|
# Look for a corresponding person with same uid in any of the configured LDAP providers
|
|
|
|
Gitlab::LDAP::Config.providers.each do |provider|
|
2015-06-02 06:01:29 -04:00
|
|
|
adapter = Gitlab::LDAP::Adapter.new(provider)
|
2017-07-12 04:41:41 -04:00
|
|
|
@ldap_person = find_ldap_person(auth_hash, adapter)
|
2015-06-17 12:06:27 -04:00
|
|
|
break if @ldap_person
|
2015-06-02 06:01:29 -04:00
|
|
|
end
|
2015-06-17 12:06:27 -04:00
|
|
|
@ldap_person
|
2015-06-02 06:01:29 -04:00
|
|
|
end
|
|
|
|
|
2017-07-12 04:41:41 -04:00
|
|
|
def find_ldap_person(auth_hash, adapter)
|
|
|
|
by_uid = Gitlab::LDAP::Person.find_by_uid(auth_hash.uid, adapter)
|
|
|
|
# The `uid` might actually be a DN. Try it next.
|
|
|
|
by_uid || Gitlab::LDAP::Person.find_by_dn(auth_hash.uid, adapter)
|
|
|
|
end
|
|
|
|
|
2015-06-02 06:01:29 -04:00
|
|
|
def ldap_config
|
2015-06-05 06:32:01 -04:00
|
|
|
Gitlab::LDAP::Config.new(ldap_person.provider) if ldap_person
|
2015-06-02 06:01:29 -04:00
|
|
|
end
|
|
|
|
|
2014-10-17 07:08:02 -04:00
|
|
|
def needs_blocking?
|
2015-06-05 06:32:01 -04:00
|
|
|
new? && block_after_signup?
|
2014-10-17 07:08:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def signup_enabled?
|
2016-02-18 17:01:07 -05:00
|
|
|
providers = Gitlab.config.omniauth.allow_single_sign_on
|
2016-02-18 17:02:43 -05:00
|
|
|
if providers.is_a?(Array)
|
|
|
|
providers.include?(auth_hash.provider)
|
|
|
|
else
|
|
|
|
providers
|
|
|
|
end
|
2014-10-17 07:08:02 -04:00
|
|
|
end
|
|
|
|
|
2016-04-11 11:16:15 -04:00
|
|
|
def external_provider?
|
|
|
|
Gitlab.config.omniauth.external_providers.include?(auth_hash.provider)
|
|
|
|
end
|
|
|
|
|
2014-10-17 07:08:02 -04:00
|
|
|
def block_after_signup?
|
2015-06-05 06:32:01 -04:00
|
|
|
if creating_linked_ldap_user?
|
|
|
|
ldap_config.block_auto_created_users
|
2016-01-19 10:25:38 -05:00
|
|
|
else
|
2015-06-05 06:32:01 -04:00
|
|
|
Gitlab.config.omniauth.block_auto_created_users
|
|
|
|
end
|
2014-10-17 07:08:02 -04:00
|
|
|
end
|
|
|
|
|
2014-09-04 06:55:10 -04:00
|
|
|
def auth_hash=(auth_hash)
|
|
|
|
@auth_hash = AuthHash.new(auth_hash)
|
|
|
|
end
|
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def find_by_uid_and_provider
|
2014-11-25 11:15:30 -05:00
|
|
|
identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid)
|
|
|
|
identity && identity.user
|
2014-10-10 06:03:32 -04:00
|
|
|
end
|
2014-09-03 11:33:03 -04:00
|
|
|
|
2014-10-10 06:03:32 -04:00
|
|
|
def build_new_user
|
2017-03-27 05:37:24 -04:00
|
|
|
user_params = user_attributes.merge(extern_uid: auth_hash.uid, provider: auth_hash.provider, skip_confirmation: true)
|
2017-04-24 17:12:14 -04:00
|
|
|
Users::BuildService.new(nil, user_params).execute(skip_authorization: true)
|
2014-09-03 11:33:03 -04:00
|
|
|
end
|
|
|
|
|
2014-09-03 09:59:50 -04:00
|
|
|
def user_attributes
|
2015-06-02 06:01:29 -04:00
|
|
|
# Give preference to LDAP for sensitive information when creating a linked account
|
2015-06-05 06:32:01 -04:00
|
|
|
if creating_linked_ldap_user?
|
2016-01-19 10:25:38 -05:00
|
|
|
username = ldap_person.username.presence
|
|
|
|
email = ldap_person.email.first.presence
|
2015-06-02 06:01:29 -04:00
|
|
|
end
|
2016-01-07 13:14:41 -05:00
|
|
|
|
2016-01-19 10:25:38 -05:00
|
|
|
username ||= auth_hash.username
|
|
|
|
email ||= auth_hash.email
|
|
|
|
|
2016-01-07 13:14:41 -05:00
|
|
|
name = auth_hash.name
|
|
|
|
name = ::Namespace.clean_path(username) if name.strip.empty?
|
2016-01-19 10:25:38 -05:00
|
|
|
|
2015-06-05 06:32:01 -04:00
|
|
|
{
|
2016-01-07 13:14:41 -05:00
|
|
|
name: name,
|
2015-06-02 06:01:29 -04:00
|
|
|
username: ::Namespace.clean_path(username),
|
|
|
|
email: email,
|
2015-02-13 07:33:28 -05:00
|
|
|
password: auth_hash.password,
|
|
|
|
password_confirmation: auth_hash.password,
|
|
|
|
password_automatically_set: true
|
2014-09-03 09:59:50 -04:00
|
|
|
}
|
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
|
2017-06-06 11:39:54 -04:00
|
|
|
def sync_email_from_provider?
|
|
|
|
auth_hash.provider.to_s == Gitlab.config.omniauth.sync_email_from_provider.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_email
|
|
|
|
if auth_hash.has_email? && sync_email_from_provider?
|
|
|
|
if persisted?
|
|
|
|
gl_user.skip_reconfirmation!
|
|
|
|
gl_user.email = auth_hash.email
|
|
|
|
end
|
|
|
|
|
|
|
|
gl_user.external_email = true
|
|
|
|
gl_user.email_provider = auth_hash.provider
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-03 09:59:50 -04:00
|
|
|
def log
|
|
|
|
Gitlab::AppLogger
|
|
|
|
end
|
|
|
|
|
2014-11-27 06:34:39 -05:00
|
|
|
def unauthorized_to_create
|
2015-05-12 06:50:06 -04:00
|
|
|
raise SignupDisabledError
|
2014-10-16 14:08:30 -04:00
|
|
|
end
|
2013-09-03 17:04:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|