2014-09-04 11:00:27 +00:00
|
|
|
# Class to parse and transform the info provided by omniauth
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module OAuth
|
|
|
|
class AuthHash
|
|
|
|
attr_reader :auth_hash
|
|
|
|
def initialize(auth_hash)
|
|
|
|
@auth_hash = auth_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def uid
|
2015-08-17 16:09:45 +00:00
|
|
|
@uid ||= Gitlab::Utils.force_utf8(auth_hash.uid.to_s)
|
2014-09-04 11:00:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
2017-09-14 08:26:12 +00:00
|
|
|
@provider ||= auth_hash.provider.to_s
|
2014-09-04 11:00:27 +00:00
|
|
|
end
|
|
|
|
|
2015-08-17 16:09:45 +00:00
|
|
|
def name
|
|
|
|
@name ||= get_info(:name) || "#{get_info(:first_name)} #{get_info(:last_name)}"
|
2014-09-04 11:00:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2015-08-17 16:09:45 +00:00
|
|
|
@username ||= username_and_email[:username].to_s
|
2014-09-04 11:00:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def email
|
2015-08-17 16:09:45 +00:00
|
|
|
@email ||= username_and_email[:email].to_s
|
2014-09-04 11:00:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def password
|
2015-08-17 16:09:45 +00:00
|
|
|
@password ||= Gitlab::Utils.force_utf8(Devise.friendly_token[0, 8].downcase)
|
|
|
|
end
|
|
|
|
|
2017-08-29 08:57:41 +00:00
|
|
|
def location
|
|
|
|
location = get_info(:address)
|
|
|
|
if location.is_a?(Hash)
|
|
|
|
[location.locality.presence, location.country.presence].compact.join(', ')
|
|
|
|
else
|
|
|
|
location
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_attribute?(attribute)
|
|
|
|
if attribute == :location
|
|
|
|
get_info(:address).present?
|
|
|
|
else
|
|
|
|
get_info(attribute).present?
|
|
|
|
end
|
2016-01-19 15:25:38 +00:00
|
|
|
end
|
|
|
|
|
2015-08-17 16:09:45 +00:00
|
|
|
private
|
|
|
|
|
2015-09-08 09:58:48 +00:00
|
|
|
def info
|
|
|
|
auth_hash.info
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_info(key)
|
|
|
|
value = info[key]
|
|
|
|
Gitlab::Utils.force_utf8(value) if value
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2015-08-17 16:09:45 +00:00
|
|
|
def username_and_email
|
|
|
|
@username_and_email ||= begin
|
2016-01-19 15:25:38 +00:00
|
|
|
username = get_info(:username).presence || get_info(:nickname).presence
|
|
|
|
email = get_info(:email).presence
|
2015-08-17 16:09:45 +00:00
|
|
|
|
|
|
|
username ||= generate_username(email) if email
|
|
|
|
email ||= generate_temporarily_email(username) if username
|
|
|
|
|
|
|
|
{
|
|
|
|
username: username,
|
|
|
|
email: email
|
|
|
|
}
|
|
|
|
end
|
2014-09-04 11:00:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get the first part of the email address (before @)
|
|
|
|
# In addtion in removes illegal characters
|
2015-08-17 16:09:45 +00:00
|
|
|
def generate_username(email)
|
2016-06-29 13:23:44 +00:00
|
|
|
email.match(/^[^@]*/)[0].mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/, '').to_s
|
2014-09-04 11:00:27 +00:00
|
|
|
end
|
|
|
|
|
2015-08-17 16:09:45 +00:00
|
|
|
def generate_temporarily_email(username)
|
2014-09-04 11:00:27 +00:00
|
|
|
"temp-email-for-oauth-#{username}@gitlab.localhost"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|