gitlab-org--gitlab-foss/lib/gitlab/o_auth/auth_hash.rb

60 lines
1.4 KiB
Ruby
Raw Normal View History

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
Gitlab::Utils.force_utf8(auth_hash.uid.to_s)
2014-09-04 11:00:27 +00:00
end
def provider
Gitlab::Utils.force_utf8(auth_hash.provider.to_s)
2014-09-04 11:00:27 +00:00
end
def info
auth_hash.info
end
def name
Gitlab::Utils.force_utf8((info.try(:name) || full_name).to_s)
2014-09-04 11:00:27 +00:00
end
def full_name
Gitlab::Utils.force_utf8("#{info.first_name} #{info.last_name}")
2014-09-04 11:00:27 +00:00
end
def username
Gitlab::Utils.force_utf8(
(info.try(:nickname) || generate_username).to_s
)
2014-09-04 11:00:27 +00:00
end
def email
Gitlab::Utils.force_utf8(
(info.try(:email) || generate_temporarily_email).downcase
)
2014-09-04 11:00:27 +00:00
end
def password
devise_friendly_token = Devise.friendly_token[0, 8].downcase
@password ||= Gitlab::Utils.force_utf8(devise_friendly_token)
2014-09-04 11:00:27 +00:00
end
# Get the first part of the email address (before @)
# In addtion in removes illegal characters
def generate_username
email.match(/^[^@]*/)[0].parameterize
end
def generate_temporarily_email
"temp-email-for-oauth-#{username}@gitlab.localhost"
end
end
end
end