代码之家  ›  专栏  ›  技术社区  ›  Justin Force jmbucknall

在Ruby on Rails应用程序中,外部目录认证放在哪里?

  •  1
  • Justin Force jmbucknall  · 技术社区  · 14 年前

    我将使用 net-ldap UsersController ?

    下面是一些验证代码的示例:

    require 'rubygems'
    require 'net/ldap'
    
    ldap = Net::LDAP.new
    ldap.encryption :simple_tls
    ldap.host = 'directory.company.com'
    ldap.port = 636
    ldap.auth 'uid=human,ou=people,o=mycompany', 'turboSecret'
    
    if ldap.bind
      puts 'win!'
    else
      puts 'fail :('
    end
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Justin Force jmbucknall    14 年前

    我把这个放进去了 app/controllers/sessions_controller.rb .

    会话控制器.rb

    class SessionsController < ApplicationController
    
      # Authenticate user against their LDAP directory
      # user and directory correspond to models in my app
      # password is a string
      # For complete source, see http://github.com/sidewaysmilk/auther
      def directory_login(user, password)
    
        directory = user.directory
    
        return nil if !directory
    
        directory.bind_dn['%s'] = user.login
    
        ldap = Net::LDAP.new
        ldap.host = directory.host
        ldap.port = directory.port
        ldap.encryption :simple_tls if directory.use_simple_tls?
        ldap.auth directory.bind_dn, password
        if ldap.bind
          return user 
        end
      end
    
    end