アーカイブ
Tyzohブログ - ikarashiさんのエントリ
 ikarashiさんのエントリ配信

2007/06/12
[Secure-SBM:012] いよいよ認証

執筆者: ikarashi (9:36 am)
こんにちは,五十嵐です.管理者用のページができましたので,認証の仕組みに入っていきます.admin だけが /admin のページにアクセスできるようにします.

まず,DBIx::Class の認証の仕組みを使うために,Plugin を設定します.

lib/SecureSBM.pm
@@ -16,6 +16,14 @@
                Charsets::Japanese
 
                StackTrace
+
+               Authentication
+               Authentication::Store::DBIC
+               Authentication::Credential::Password
+
+               Session
+               Session::Store::FastMmap
+               Session::State::Cookie
                /;
 
 use Digest::SHA;
6 つの Plugin を読み込むように設定します.

つぎに,securesbm.yml ファイルにユーザ名やパスワードのデータベーステーブルがどのようになっているのかを設定します.

securesbm.yml
authentication:
  dbic:
    user_class:           SecureSBMDB::User
    user_field:           user_email
    password_field:       user_password
    password_type:        hashed
    password_hash_type:   SHA-1
何度も繰り返しますが,.yml のファイルはタブコードを使用せずに,空白でインデントを付けてください.この設定は前に設定した Plugin のためのものです.各フィールドの意味は次の通り.
user_class
スキーマのクラス

user_field
ログイン名として使用する名前

password_field
パスワードの格納されているカラム名

password_type
平文なのか hash なのか

password_hash_type
hash されている場合の種類
"perldoc Catalyst::Plugin::Authentication::Store::DBIC" と実行すると詳細を見ることができます.

これで認証を行うための準備ができました.Login コントローラと Logout コントローラを作成します.
$ ./script/securesbm_create.pl controller Login
(出力省略)
$ ./script/securesbm_create.pl controller Logout
(出力省略)
$ 
これで,lib/SecureSBM/Controller/Login.pm と同じディレクトリに Logout.pm が作成されます.

lib/SecureSBM/Controller/Login.pm には,ログインをチェックするためのサブルーチン(index)とログインフォームを出力するための loginform() を作成します.

lib/SecureSBM/Controller/Login.pm(index)
sub index : Private {
        my ($self, $c) = @_;

        # ログイン名とパスワードの取得
        my $loginname = $c->req->params->{user_email} || "";
        my $password = $c->req->params->{user_password} || "";
        $loginname =~ tr/A-Z/a-z/;

        if ($loginname && $password) {
                # ログイン試行
                if ($c->login($loginname, $password)) {
                        # ログイン成功
                        $c->log->debug('*** Login Success: user[' 
                                                . $c->user .']');
                        if( $loginname eq 'admin' ){
                                $c->res->redirect($c->uri_for('/admin'));
                        }else{
                                $c->res->redirect($c->uri_for('/')); 
                        }
                        return;
                } else {
                        # ログイン失敗
                        $c->log->debug('*** Login Failed: [' . $loginname 
                                                        . '/' . $password . ']');
                        $c->log->info('*** Login Failed: [' 
                                                              . $loginname . ']');
                }
        }
        $c->log->debug('*** user/password [' . $loginname . '/' 
                                                               . $password . ']');

        $c->forward('loginform');
}
フォームから送られるパラメータは "user_email" と "user_password" です.これを使って login を呼び出します.ユーザが "admin" の場合だけ,/admin にリダイレクトし,それ以外は / にリダイレクトします.認証に失敗した場合には後述の loginform に処理を移します.

lib/SecureSBM/Controller/Login.pm(loginform)
sub loginform : Path('/loginform') {
        my ($self, $c) = @_;

        $c->stash->{template} = 'login.tt';
}
テンプレートに 'login.tt' を指定して終了します.Login.pm の中の関数は,何も指定しないと '/login/xxxxx' というパスで呼び出さなければなりません.Path('/loginform') は,'/login/loginform' の代わりに '/loginform' で呼び出すように設定するためのものです.

あわせて,login.tt を設定しますが,これは login_center.tt をインクルードします.login_center.tt は以下の通りです.

root/template/login_center.tt
<div id="login_center">

<h1>ログインページ</h1>
<p>ユーザ名とパスワードを入力してください</p>
<form action="/login" method="POST" name="login" id="login">
<table>
<tr><td>ユーザ名: </td>
<td><input type="text" name="user_email" width="30" 
                                                 id="user_email"></td></tr>
<tr><td>パスワード:</td>
<td> <input type="password" name="user_password" width="30"
                                             id="user_password"></td></tr>
<tr><td></td>
<td align="right"><input type="submit" name="submit" value="ログイン" 
                                                         id="login_submit">
<input type="reset" id="login_reset"></td></tr>
</table>
</form>

</div> <!-- end of login_center -->
[%#
This Program is distributed under version 1.0 of the Rinza Public
License Agreement, that is bundled with this package in the file
LICENSE, and is available through the website at the following URL:
http://www.tyzoh.jp/rinza/licenses/LICENSE-1.0.txt.

This is the Original Program.
The Initial Developer of the Original Program is Nihon Unisys, Ltd.
The Original Program is copyrighted (C) 2006-2007 by Nihon Unisys, Ltd. with
all rights reserved.
There is NO WARRANTY OF ANY KIND by the Initial Developer of the
Original Program.
%]
これで,ログインの準備は整いました.

次に,Logout.pm を設定します.

lib/SecureSBM/Controller/Logout.pm
sub index : Private {
        my ($self, $c) = @_;

        # ログアウト
        $c->logout;

        # Top ページに戻す
        $c->response->redirect($c->uri_for('/'));
}

ログイン,ログアウトの準備ができました.ただし,このままでは,認証の準備ができただけで,ログインしなくてもどのページにもアクセスできます.そこで,認証 (Authentication) 後,アクセスを認可 (Authorazation) するような仕組みを導入しなければなりません.Root.pm に以下のような設定を行います.

lib/SecureSBM/Controller/Root.pm
sub auto : Private {
    my ( $self, $c ) = @_; 

    $c->log->debug( '*** Controller [' . $c->controller . '].' );
    $c->log->debug( '*** action [' . $c->req->action . '].' );

    # Controller が Login か Root の場合には,認証を必要としない
    if ( $c->controller eq $c->controller('Login') 
        || $c->controller eq $c->controller('Root') ){
        return 1;
    }   

    # $c->user_exists が false の場合は,ログインをしていない.
    if (!$c->user_exists) {
        $c->log->
            debug('*** Root::auto User not found, forwarding to /login');
        $c->response->redirect($c->uri_for('/login'));
        return 0;
    }

    # ユーザが見つかった場合
    $c->log->debug('*** Root::auto User Found');
    return 1;
}
Admin 用のページなどに認証が入るようになります.

しかし,この状態ではまだまだアクセスコントロールをしたことにはなりません.admin 以外のユーザも認証さえすればどのページにもアクセスできてしまいます.そこで,次回以降で,admin 以外のユーザが /admin にアクセスできないように設定します.

2007/03/23 記

参考:

Keyword: Perl Catalyst Secure-SBM SSBM セキュア・ソーシャル・ブックマーク
ikarashiさんのブログを読む | コメント (0) | トラックバック数 (0) | 閲覧数 (4392)
Trackback is not accepted now.
印刷用ページ 友達に送る
 
投稿された内容の著作権はコメントの投稿者に帰属します。
サイト内検索
ブログ カレンダー
«  «  2008 3月  »  »
24 25 26 27 28 29 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5