Class: AuthenticationManager

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/authentication_manager.rb

Class Method Summary collapse

Class Method Details

.authenticate(username, password) ⇒ Object

Attempt to authenticate user' with the provided password’. Return a User object if successful, nil otherwise



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'backend/app/model/authentication_manager.rb', line 19

def self.authenticate(username, password)
  authentication_sources.each do |source|
    begin
      user = User.find(:username => username)

      # System users are only authenticated locally.
      next if (user && user.is_system_user == 1 && source != DBAuth)

      #ANW-97: check if user is inactive
      next if (user && user.is_active_user != 1)

      # If configured prevent authentication attempts for existing users via a
      # different source. Use case: don't allow an LDAP user to authenticate via
      # the database because they had a password set at some point
      if AppConfig[:authentication_restricted_by_source] && user && user.source != 'local'
        if user.source != source.name
          Log.warn("Restricted source for #{user.username} [#{user.source}]: #{source.name}")
          next
        end
      end

      jsonmodel_user = source.authenticate(username, password)

      if !jsonmodel_user
        next
      end

      # Force their admin status based on what they already had
      jsonmodel_user.is_admin = (user && user.can?(:administer_system))

      if user
        begin
          user.update_from_json(jsonmodel_user,
                                :source => source.name,
                                :lock_version => user.lock_version)
        rescue Sequel::NoExistingObject => e
          # We'll swallow these because they only really mean that the user
          # logged in twice simultaneously.  As long as one of the updates
          # succeeded it doesn't really matter.
          user = User.find(:username => username)
        end
      else
        DB.attempt {
          user = User.create_from_json(jsonmodel_user, :source => source.name)
        }.and_if_constraint_fails {
          return authenticate(username, password)
        }
      end

      return user
    rescue
      Log.error("Error communicating with authentication source #{source.inspect}: #{$!}")
      Log.exception($!)
      next
    end
  end

  nil
end

.authenticate_token(username, token) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'backend/app/model/authentication_manager.rb', line 87

def self.authenticate_token(username, token)
  authentication_sources.each do |source|
    next unless source == DBAuth
    if (user = source.authenticate_token(username, token))
      return user
    end
  end
  nil
end

.authentication_sourcesObject



12
13
14
# File 'backend/app/model/authentication_manager.rb', line 12

def self.authentication_sources
  prepare_sources(AppConfig[:authentication_sources]) + [DBAuth]
end

.generate_token(username) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'backend/app/model/authentication_manager.rb', line 98

def self.generate_token(username)
  authentication_sources.each do |source|
    next unless source == DBAuth
    if (token = source.generate_token(username))
      return token
    end
  end
  nil
end

.matching_usernames(query) ⇒ Object



80
81
82
83
84
# File 'backend/app/model/authentication_manager.rb', line 80

def self.matching_usernames(query)
  authentication_sources.map {|source|
    source.matching_usernames(query)
  }.flatten(1).sort.uniq
end

.prepare_sources(sources) ⇒ Object



5
6
7
8
9
10
# File 'backend/app/model/authentication_manager.rb', line 5

def self.prepare_sources(sources)
  sources.map { |source|
    model = Kernel.const_get(source[:model].intern)
    model.new(source)
  }
end