Wednesday, January 20, 2010

Connecting to BYU LDAP with Ruby

require 'rubygems'
require 'net/ldap'

class AuthBYU
  def initialize
    # Off-campus connections MUST be secure. On campus connection can be insecure
    @ldap = Net::LDAP.new(:port => 636, :encryption => :simple_tls)
    #ldap = Net::LDAP.new(:port => 389)
  end

  def login(routey_id, routey_password)
    ldap.host = "ldap.byu.edu"
    ldap.auth "uid=#{routey_id},ou=People,o=BYU.edu", routey_password
    ldap.bind # true | false | throw timeout
  end

  def ry_login(*args)
    login(*args)
  end

  def cs_login(cs_id,cs_password)
    # Only works on-campus and only with insecure connections
    ldap = Net::LDAP.new(:port => 389)
    ldap.host = "ldap.cs.byu.edu"
    ldap.auth "uid=#{cs_id},ou=people,dc=cs,dc=byu,dc=edu", cs_password
    ldap.bind
  end

  def caedm_login(caedm_id, caedm_password)
    # I can't find the info for this one
    #ldap = Net::LDAP.new(:port => 389)
    #ldap.host = "????.et.byu.edu"
    #ldap.auth "uid=#{caedm_id},ou=accounts,ou=caedm,dc=et,dc=byu,dc=edu", caedm_password
    #ldap.bind
    false
  end

  private
    attr_accessor :ldap
end

# Test by hand
puts "Username:"
username = gets.chomp
puts "Password (Visible):"
password = gets.chomp

b = AuthBYU.new
if b.login username, password 
  puts "Is good\n"
else
  puts "Is not so good\n"
end

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home