1

CodeIgniter アプリケーションに CAS 認証を実装しようとしていますが、現在セットアップされているライブラリがあるかどうかはわかりません。クラスを含めていくつかの汚い修正を追加するだけで管理していますが、誰かが適切なライブラリを知っていれば、それはよりクリーンなソリューションになると思います。

ここだけでなく Google のさまざまな投稿を調べてきましたが、必要なものが不足しているようです。関連性のある唯一の場所はVCU ライブラリに関する投稿ですが、ライブラリのダウンロード リンクは含まれていません。

みんな、ありがとう!

4

3 に答える 3

3

更新: Github でライブラリの最新バージョンを見つけることができます: https://github.com/eliasdorneles/code-igniter-cas-library

Sparks 経由でインストールすることもできます: http://getsparks.org/packages/cas-auth-library/versions/HEAD/show

既存のphpCASに依存する CodeIgniter の CAS 認証の設定を簡素化するために、CAS ライブラリを開始しました。使用を開始するには、アクセス可能なディレクトリに 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);
    }
}
于 2012-08-03T20:26:46.110 に答える
0

Ion Auth Libraryを使用することをお勧めします。これは、時代遅れになった Redux Auth に基づいて構築されています。Ion Auth は軽量で、カスタマイズが容易で、必要なことを実行します。Ion Auth は、CodeIgniter に最適な認証ライブラリの 1 つです。

于 2011-04-17T23:37:47.640 に答える
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

于 2011-04-18T01:55:50.223 に答える