CAS认证库codeIgniter [英] CAS Authentication Library for CodeIgniter

查看:430
本文介绍了CAS认证库codeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在codeIgniter应用CAS认证但如果目前有设立任何图书馆,我找不到。我只是通过包括类和一些肮脏的修复,虽然增加,如果任何人正确的图书馆我想知道这将是一个更清洁的解决方案管理。

我一直在寻找通过一系列职位放在这里,以及所有对谷歌,但似乎短我需要什么来了。什么关联的地方是在 VCU库但是,这并不包括库下载链接。

谢谢大家!


解决方案

  

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


  
  

您还可以通过安装火花: http://getsparks.org /包/ CAS-AUTH-库/版本/ HEAD /显示


我已经开始了一个CAS库来简化C $ cIgniter,这依赖于现有的 phpCAS
要开始使用它,你就必须在一些访问的目录安装phpCAS,放在应用程序/库库文件/ Cas.php 并创建一个配置文件配置/ cas.php 是这样的:

 <如果PHP(定义(基本路径')!)出口('没有直接的脚本允许访问');
$配置['cas_server_url'] ='https://yourserver.com/cas';
$配置['phpcas_path'] ='/path/to/phpCAS-1.3.1';
$配置['cas_disable_server_validation'] = TRUE;
// $配置['cas_debug'] = TRUE; //< - 用它来使phpCAS调试模式

然后,在你的控制器,你将能够做到这一点:

 功能指数(){
    $这个 - >负载>库(CAS);
    $这个 - >&磁带式GT; force_auth();
    $ USER = $这个 - >&磁带式GT;用户();
    回声你好,$用户可>用户登陆!;
}

下面是库​​文件(已经被命名为 Cas.php

 <如果PHP(定义(基本路径')!)出口('没有直接的脚本允许访问');功能cas_show_config_error(){
    show_error(CAS认证配置不正确< BR />< BR />
    请检查您的配置为以下文件:
    < code>配置/ cas.php< / code>
    最低配置要求:
    < UL>
       <立GT;<环境监察及GT; cas_server_url< / EM计算值:在<强而GT; URL< / STRONG>您的CAS服务器16中; /立GT;
       <立GT;<环境监察及GT; phpcas_path< / EM计算值:路径一安装
           < A HREF = \\的https://wiki.jasig.org/display/CASC/phpCAS \\> phpCAS库< / A>< /李>
        <李>和&的LT之一; EM> cas_disable_server_validation< / EM>和<&EM GT; cas_ca_cert_file< / EM方式>< /李>
    < / UL>
    );
}类CAS大全MSDS {    公共职能__construct(){
        如果(!function_exists('curl_init')){
            show_error('<强>错误:< / STRONG>您需要安装PHP模块<强>卷曲< / STRONG>
                能够使用CAS认证');
        }
        $ CI =&安培; get_instance();
        $这个 - > CI = $ CI;
        $ CI-> config->负载(CAS);        $这个 - > phpcas_path = $ CI-> config->项目('phpcas_path');
        $这个 - > cas_server_url = $ CI-> config->项目('cas_server_url');        如果(空($这个 - > phpcas_path)
            或filter_var($这个 - > cas_server_url,FILTER_VALIDATE_URL)=== FALSE){
            cas_show_config_error();
        }
        $ cas_lib_file = $这个 - > phpcas_path。 /CAS.php';
        如果(!file_exists($ cas_lib_file)){
            show_error(找不到文件:其中code基$ cas_lib_file< / code>中。);
        }
        require_once $ cas_lib_file;        如果($ CI-> config->项目('cas_debug')){
            phpCAS :: setDebug();
        }        //初始化客户端CAS
        $默认=阵列('路径'=>'','口'=> 443);
        $ cas_url = array_merge($默认,parse_url($这个 - > cas_server_url));        phpCAS ::客户端(CAS_VERSION_2_0,$ cas_url ['主机'],
            $ cas_url ['口'],$ cas_url ['路径']);        //配置SSL行为
        如果($ CI-> config->项目('cas_disable_server_validation')){
            phpCAS :: setNoCasServerValidation();
        }其他{
            $ ca_cert_file = $ CI-> config->项目('cas_server_ca_cert');
            如果(空($ ca_cert_file)){
                cas_show_config_error();
            }
            phpCAS :: setCasServerCACert($ ca_cert_file);
        }
    }    / **
      *触发CAS认证,如果用户还没有通过身份验证。
      * /
    公共职能force_auth()
    {
        phpCAS :: forceAuthentication();
    }    / **
     *返回一个对象,用户登陆和属性。
     *显示AERROR如果验证之前调用。
     * /
    公共职能的用户()
    {
        如果(phpCAS :: isAuthenticated()){
            $用户登陆= phpCAS ::的getUser();
            $属性= phpCAS ::的getAttributes();
            回声有属性?
            的var_dump(phpCAS :: hasAttributes());
            回报(对象)阵列('用户登陆'=> $用户登陆,
                '属性'=> $属性);
        }其他{
            show_error(用户还没有验证。);
        }
    }    / **
     *注销并重定向到主站点的URL,
     *或到URL传递作为参数
     * /
    公共职能注销($网址='')
    {
        如果(空($网址)){
            $这个 - > CI->负载>帮手(URL);
            $ URL = BASE_URL();
        }
        phpCAS :: logoutWithRedirectService($网址);
    }
}

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!

解决方案

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);
    }
}

这篇关于CAS认证库codeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆