Apache1.3系では、ベーシック認証は初期設定で使えるが、ダイジェスト認証はモジュールを追加する必要がある。

ダイジェスト認証(MD5を使用した認証機能)には、mod_auth_digest か mod_digest を使う。
Apache のマニュアルを見ると mod_digest は非推奨とのこと。でも、mod_auth_digest は実験的なモジュールという位置付けらしい。
ここでは、mod_auth_digest を使ってみる。

Digest認証のモジュールを使うように設定した Apache 1.3.34 をインストール


$ ./configure --prefix=/usr/local/apache \
  --enable-module=so --enable-module=auth_digest
$ make
$ make install

Basic認証用のエントリを追加: ユーザ名=secret パスワード=password


$ htpasswd -c /usr/local/apache/password/.htpasswd secret
New password: password
Re-type new password: password
Adding password for user secret

Digest認証用のエントリを追加: ユーザ名=secret パスワード=password


$ htdigest -c /usr/local/apache/password/.htdigest 'Secret Zone' secret
Adding password for secret in realm Secret Zone.
New password: password
Re-type new password: password

ファイルの中身を確認


$ ls -la | grep ht
-rw-r--r--   1 hogehoge hogehoge    52 2005-12-31 10:10 .htdigest
-rw-r--r--   1 hogehoge hogehoge    21 2005-12-31 10:10 .htpasswd
$ cat ./.htpasswd
secret:E.kPu71.ZoSH2
$ cat ./.htdigest
secret:Secret Zone:bae71d6e71194743724ee3cdb7d38a89

httpd.conf に追加する設定


<Directory "/usr/local/apache/htdocs/member_basic">
    AuthType Basic
    AuthName "Secret Zone"
    AuthUserFile /usr/local/apache/password/.htpasswd
    Require user secret
</Directory>
 
<Directory "/usr/local/apache/htdocs/member_digest">
    AuthType Digest
    AuthName "Secret Zone"
    AuthDigestFile /usr/local/apache/password/.htdigest
    Require user secret
</Directory>

Basic認証では、ウェブブラウザは毎回同じ Authorization ヘッダを送信している。
Authorization: Basic ("ユーザ名:パスワード" を BASE64 エンコードしたもの)

Authorization: Basic c2VjcmV0OnBhc3N3b3Jk
Authorization: Basic c2VjcmV0OnBhc3N3b3Jk

Digest認証では、ウェブブラウザは毎回異なる Authorization ヘッダを送信している。

Authorization: Digest username="secret", realm="Secret Zone", nonce="eB6yQw==520f503a975315f74f53bf17fe41f18fa9abcc8a", uri="/member_digest/", algorithm=MD5, response="068e0f1bb8f13220fbfb340869595e9b", qop=auth, nc=00000001, cnonce="15c07961ed8575c4"
Authorization: Digest username="secret", realm="Secret Zone", nonce="eB6yQw==520f503a975315f74f53bf17fe41f18fa9abcc8a", uri="/member_digest/", algorithm=MD5, response="5af212c7a9e83c467c009d2c61c16fdb", qop=auth, nc=00000002, cnonce="64e4b82bc57d0e80"

Apache のマニュアルにて、関連しそうな部分をピックアップ。

Authentication, Authorization, and Access Control

Apache Core Features - AuthType ディレクティブ によると、

このディレクティブは対象ディレクトリで利用するユーザー認証の種類を選びます。 ただ、現在のところは Basic 若しくは Digest しか実装されていません。 このディレクティブは AuthType ディレクティブや Require ディレクティブ及び、 AuthUserFile や AuthGroupFile などのディレクティブと一緒に利用する必要があります。

Apache module mod_auth

Apache module mod_auth_digest によると、

This is an updated version of mod_digest. However, it has not been extensively tested and is therefore marked experimental.

その他、参考資料。

tags: zlashdot Apache Apache

Posted by NI-Lab. (@nilab)