debian-mirror-gitlab/lib/gitlab/o_auth/auth_hash.rb

74 lines
1.7 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
# 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-09-11 14:41:01 +05:30
@uid ||= Gitlab::Utils.force_utf8(auth_hash.uid.to_s)
2015-04-26 12:48:37 +05:30
end
def provider
2015-09-11 14:41:01 +05:30
@provider ||= Gitlab::Utils.force_utf8(auth_hash.provider.to_s)
2015-04-26 12:48:37 +05:30
end
def info
auth_hash.info
end
2015-09-11 14:41:01 +05:30
def get_info(key)
value = info.try(key)
Gitlab::Utils.force_utf8(value) if value
value
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
def name
@name ||= get_info(:name) || "#{get_info(:first_name)} #{get_info(:last_name)}"
2015-04-26 12:48:37 +05:30
end
def username
2015-09-11 14:41:01 +05:30
@username ||= username_and_email[:username].to_s
2015-04-26 12:48:37 +05:30
end
def email
2015-09-11 14:41:01 +05:30
@email ||= username_and_email[:email].to_s
2015-04-26 12:48:37 +05:30
end
def password
2015-09-11 14:41:01 +05:30
@password ||= Gitlab::Utils.force_utf8(Devise.friendly_token[0, 8].downcase)
end
private
def username_and_email
@username_and_email ||= begin
username = get_info(:nickname) || get_info(:username)
email = get_info(:email)
username ||= generate_username(email) if email
email ||= generate_temporarily_email(username) if username
{
username: username,
email: email
}
end
2015-04-26 12:48:37 +05:30
end
# Get the first part of the email address (before @)
# In addtion in removes illegal characters
2015-09-11 14:41:01 +05:30
def generate_username(email)
2015-04-26 12:48:37 +05:30
email.match(/^[^@]*/)[0].parameterize
end
2015-09-11 14:41:01 +05:30
def generate_temporarily_email(username)
2015-04-26 12:48:37 +05:30
"temp-email-for-oauth-#{username}@gitlab.localhost"
end
end
end
end