CodeIgniter的CAS认证库

时间:2011-04-17 17:03:29

标签: php authentication codeigniter cas

我正在尝试在CodeIgniter应用程序中实现CAS身份验证,但我找不到当前是否有任何库设置它。我正在管理,只是包括该类并添加一些脏的修复,但如果有人知道一个合适的库我认为这将是一个更清洁的解决方案。

我一直在寻找这里以及谷歌各地的一系列帖子,但似乎在我需要的东西上做得不够。唯一相关的地方是VCU Libraries上的帖子,但不包括图书馆下载链接。

谢谢大家!

3 个答案:

答案 0 :(得分:3)

  

更新:您可以在Github找到最新版本的库:https://github.com/eliasdorneles/code-igniter-cas-library

     

您也可以通过sparks http://getsparks.org/packages/cas-auth-library/versions/HEAD/show

安装

我已经启动了一个CAS库来简化为CodeIgniter设置CAS身份验证,它依赖于现有的phpCAS。 要开始使用它,只需在某个可访问的目录中安装phpCAS,将库文件放在application/libraries/Cas.php中并创建一个配置文件config/cas.php,如下所示:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['cas_server_url'] = 'https://yourserver.com/cas';
$config['phpcas_path'] = '/path/to/phpCAS-1.3.1';
$config['cas_disable_server_validation'] = TRUE;
// $config['cas_debug'] = TRUE; // <--  use this to enable phpCAS debug mode

然后,在您的控制器中,您将能够这样做:

function index() {
    $this->load->library('cas');
    $this->cas->force_auth();
    $user = $this->cas->user();
    echo "Hello, $user->userlogin!";
}

这是库文件(必须命名为 Cas.php ):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function cas_show_config_error(){
    show_error("CAS authentication is not properly configured.<br /><br />
    Please, check your configuration for the following file:
    <code>config/cas.php</code>
    The minimum configuration requires:
    <ul>
       <li><em>cas_server_url</em>: the <strong>URL</strong> of your CAS server</li>
       <li><em>phpcas_path</em>: path to a installation of
           <a href=\"https://wiki.jasig.org/display/CASC/phpCAS\">phpCAS library</a></li>
        <li>and one of <em>cas_disable_server_validation</em> and <em>cas_ca_cert_file</em>.</li>
    </ul>
    ");
}

class Cas {

    public function __construct(){
        if (!function_exists('curl_init')){
            show_error('<strong>ERROR:</strong> You need to install the PHP module <strong>curl</strong>
                to be able to use CAS authentication.');
        }
        $CI =& get_instance();
        $this->CI = $CI;
        $CI->config->load('cas');

        $this->phpcas_path = $CI->config->item('phpcas_path');
        $this->cas_server_url = $CI->config->item('cas_server_url');

        if (empty($this->phpcas_path) 
            or filter_var($this->cas_server_url, FILTER_VALIDATE_URL) === FALSE) {
            cas_show_config_error();
        }
        $cas_lib_file = $this->phpcas_path . '/CAS.php';
        if (!file_exists($cas_lib_file)){
            show_error("Could not find file: <code>" . $cas_lib_file. "</code>");
        }
        require_once $cas_lib_file;

        if ($CI->config->item('cas_debug')) {
            phpCAS::setDebug();
        }

        // init CAS client
        $defaults = array('path' => '', 'port' => 443);
        $cas_url = array_merge($defaults, parse_url($this->cas_server_url));

        phpCAS::client(CAS_VERSION_2_0, $cas_url['host'],
            $cas_url['port'], $cas_url['path']);

        // configures SSL behavior
        if ($CI->config->item('cas_disable_server_validation')){
            phpCAS::setNoCasServerValidation();
        } else {
            $ca_cert_file = $CI->config->item('cas_server_ca_cert');
            if (empty($ca_cert_file)) {
                cas_show_config_error();
            }
            phpCAS::setCasServerCACert($ca_cert_file);
        }
    }

    /**
      * Trigger CAS authentication if user is not yet authenticated.
      */
    public function force_auth()
    {
        phpCAS::forceAuthentication();
    }

    /**
     *  Return an object with userlogin and attributes.
     *  Shows aerror if called before authentication.
     */
    public function user()
    {
        if (phpCAS::isAuthenticated()) {
            $userlogin = phpCAS::getUser();
            $attributes = phpCAS::getAttributes();
            echo "has attributes? ";
            var_dump(phpCAS::hasAttributes());
            return (object) array('userlogin' => $userlogin,
                'attributes' => $attributes);
        } else {
            show_error("User was not authenticated yet.");
        }
    }

    /**
     *  Logout and redirect to the main site URL,
     *  or to the URL passed as argument
     */
    public function logout($url = '')
    {
        if (empty($url)) {
            $this->CI->load->helper('url');
            $url = base_url();
        }
        phpCAS::logoutWithRedirectService($url);
    }
}

答案 1 :(得分:0)

我建议使用Ion Auth Library,它基于Redux Auth,它已经过时了。 Ion Auth重量轻,易于定制,可满足您的需求。 Ion Auth是CodeIgniter最好的身份验证库之一。

答案 2 :(得分:0)

究竟什么不能用于VCU库?

您可以在PHP中执行任何操作,您可以在CodeIgniter中执行。 所以你可以使用PHP CAS客户端: http://www.jasig.org/phpcas-121-final-release

以下是如何进行身份验证的示例。

https://source.jasig.org/cas-clients/phpcas/trunk/docs/examples/example_simple.php