CodeIgniter 的 CAS 身份验证库

发布于 2024-11-02 05:35:57 字数 314 浏览 1 评论 0原文

我正在尝试在 CodeIgniter 应用程序中实现 CAS 身份验证,但我找不到当前是否有为其设置的库。我通过只包含类并添加一些肮脏的修复来进行管理,但如果有人知道合适的库,我认为这将是一个更干净的解决方案。

我一直在浏览这里以及谷歌上的一系列帖子,但似乎缺少我需要的东西。唯一相关的地方是 VCU Libraries 上的帖子,但其中不包括该库下载链接。

谢谢大家!

I am trying to implement CAS authentication in a CodeIgniter application though I cannot find if there are any libraries currently set up for it. I am managing by just including the class and adding in a few dirty fixes though if anyone knows of a proper library I think it would be a cleaner solution.

I have been looking through a range of posts on here as well as all over Google but seem to be coming up short on what I need. The only place of any relevance is a post on VCU Libraries but that did not include the library download link.

Thanks everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天旳寂寞 2024-11-09 05:35:57

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

您还可以通过sparks安装:http://getsparks。 org/packages/cas-auth-library/versions/HEAD/show

我启动了一个 CAS 库来简化 CAS 的设置CodeIgniter 的身份验证,依赖于现有的 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);
    }
}

UPDATE: You can find the latest version of the library at Github: https://github.com/eliasdorneles/code-igniter-cas-library

You can also install via sparks: http://getsparks.org/packages/cas-auth-library/versions/HEAD/show

I've started a CAS library to simplify setting up CAS authentication for CodeIgniter, that relies on the existing phpCAS.
To start using it, you just have installation phpCAS in some accessible directory, put the library file in application/libraries/Cas.php and create a config file config/cas.php like this:

<?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

Then, in your controllers you would be able to do this:

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

Here is the library file (has to be named 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);
    }
}
把昨日还给我 2024-11-09 05:35:57

我建议使用 Ion Auth Library,它基于 Redux Auth 构建,但已经过时了。 Ion Auth 重量轻,易于定制,并且可以完成您需要的事情。 Ion Auth 是 CodeIgniter 最好的身份验证库之一。

I recommend using Ion Auth Library, it's built upon Redux Auth, which became outdated. Ion Auth is light weight, easy to customize, and does the things you need. Ion Auth is one of the best authentication libraries for CodeIgniter.

面如桃花 2024-11-09 05:35:57

究竟是什么不适用于 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

What exactly is not working with the VCU library?

Anything you can do in PHP, you can do in CodeIgniter.
So you can just use the PHP CAS client:
http://www.jasig.org/phpcas-121-final-release

And here is a example of how to authenticate.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文