Module: User::Authentication::Ldap

Defined in:
app/models/user/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_with_ldap(login, password) ⇒ Object

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/user/authentication.rb', line 54

def authenticate_with_ldap(, password) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  # TODO: - Extract LDAP specifics to configuration
  username = "uid=#{},ou=people,dc=sanger,dc=ac,dc=uk"
  ldap =
    Net::LDAP.new(
      host: configatron.ldap_server,
      port: configatron.ldap_secure_port,
      encryption: :simple_tls,
      auth: {
        method: :simple,
        username: username,
        password: password
      }
    )
  begin
    ldap.bind
  rescue StandardError => e
    raise e, "LDAP connection problem: #{e}", caller
  end
  password = '' # clear out in case of crashes
  if ldap.bind
    logger.info 'Authentication succeeded'
    true
  else
    code = ldap.get_operation_result.code
    message = ldap.get_operation_result.message
    logger.warn "Authentication failed for user #{}: result code #{code} message #{message}"
    false
  end
end

#register_or_update_via_ldap(login) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'app/models/user/authentication.rb', line 85

def register_or_update_via_ldap()
  u = find_or_create_by(login:)
  if u.nil?
    logger.error "Failed to find or create user #{}"
  else
    u.send(:update_profile_via_ldap) unless u.profile_complete?
  end
  u
end