summaryrefslogtreecommitdiff
path: root/lib/gitlab/oauth/auth_hash.rb
blob: 0198f61f4275ed1f5e8551c99a93df8c938c5c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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
        auth_hash.uid.to_s
      end

      def provider
        auth_hash.provider
      end

      def info
        auth_hash.info
      end

      def name
        (info.name || full_name).to_s.force_encoding('utf-8')
      end

      def full_name
        "#{info.first_name} #{info.last_name}"
      end

      def username
        (info.try(:nickname) || generate_username).to_s.force_encoding('utf-8')
      end

      def email
        (info.try(:email) || generate_temporarily_email).downcase
      end

      def password
        @password ||= Devise.friendly_token[0, 8].downcase
      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