File: auth_hash.rb

package info (click to toggle)
ruby-omniauth 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 400 kB
  • sloc: ruby: 2,483; makefile: 7
file content (53 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download | duplicates (4)
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
require 'omniauth/key_store'

module OmniAuth
  # The AuthHash is a normalized schema returned by all OmniAuth
  # strategies. It maps as much user information as the provider
  # is able to provide into the InfoHash (stored as the `'info'`
  # key).
  class AuthHash < OmniAuth::KeyStore
    def self.subkey_class
      Hashie::Mash
    end

    # Tells you if this is considered to be a valid
    # OmniAuth AuthHash. The requirements for that
    # are that it has a provider name, a uid, and a
    # valid info hash. See InfoHash#valid? for
    # more details there.
    def valid?
      uid? && provider? && info? && info.valid?
    end

    def regular_writer(key, value)
      value = InfoHash.new(value) if key.to_s == 'info' && value.is_a?(::Hash) && !value.is_a?(InfoHash)
      super
    end

    class InfoHash < OmniAuth::KeyStore
      def self.subkey_class
        Hashie::Mash
      end

      def name
        return self[:name] if self[:name]
        return "#{first_name} #{last_name}".strip if first_name? || last_name?
        return nickname if nickname?
        return email if email?

        nil
      end

      def name?
        !!name
      end
      alias valid? name?

      def to_hash
        hash = super
        hash['name'] ||= name
        hash
      end
    end
  end
end