Module: User::Authentication::Ldap

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

Instance Method Summary collapse

Instance Method Details

#authenticate_with_ldap(login, password) ⇒ Object


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
# File 'app/models/user/authentication.rb', line 49

def authenticate_with_ldap(, password)
  # 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
    logger.warn "Authentication failed for user #{}: result code #{ldap.get_operation_result.code} message #{ldap.get_operation_result.message}"
    false
  end
end

#register_or_update_via_ldap(login) ⇒ Object


77
78
79
80
81
82
83
84
85
# File 'app/models/user/authentication.rb', line 77

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