2011-04-17 114 views
1

我试图在CodeIgniter应用程序中实现CAS认证,尽管我找不到目前是否有任何库设置为它。我管理的只是包括课程,并添加了一些肮脏的修复,但如果有人知道一个合适的库,我认为这将是一个更干净的解决方案。CodeIgniter的CAS认证库

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

谢谢大家!

回答

3

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

您还可以通过安装火花: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); 
    } 
} 
+0

你有没有考虑过把它添加到github? – beardedlinuxgeek 2012-09-18 17:56:57

+1

谢谢,我想也许我应该等待别人认为这很有趣。我想时间已经到了,我会很快做到的! :) – elias 2012-09-19 16:38:19

+1

我已经设置了一个[GitHub repo](http://getsparks.org/packages/cas-auth-library/show)和一个[spark](http://getsparks.org/packages/cas- AUTH-库/显示)。 – elias 2012-09-19 23:23:27

0

我推荐使用Ion Auth Library,它建立在Redux Auth之上,它变得过时了。 Ion Auth重量轻,易于定制,并可满足您的需求。 Ion Auth是CodeIgniter的最佳认证库之一。

+0

我必须同意这一点。 Ion Auth非常轻巧,易于实施和定制。此外,CodeIgniter应该有一些内置的身份验证(http://codeigniter.uservoice.com)。 – Fuseblown 2011-04-17 23:57:21

+0

Ion Auth非常好,但是您仍然需要编写自己的CAS组件。 – icchanobot 2011-04-18 01:49:03

+0

Ion Auth确实看起来非常稳固,我坚持使用CAS的唯一原因是因为它需要与其他人的记录集成。将看看这个并尝试并纳入CAS。谢谢 – Twade 2011-04-18 22:37:29

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

+0

您链接的客户端是我目前使用的客户端。我面临的唯一问题是,包含库我需要多个CAS库的副本,因为我的主机可以防止文件链接,并在包含文件夹时包含错误。我想有一个专门的图书馆会使它更简单,包括 – Twade 2011-04-18 22:38:48