2011-04-17 64 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