<?xml version="1.0" encoding="Shift_JIS"?>

<!DOCTYPE document SYSTEM "./dtd/document-v10.dtd">

<document>
  <header>
    <title>認証のテスト Howto/Testing secure code Howto</title>
    <authors>
      <person name="Vincent Massol" email="vmassol@apache.org"/>
    </authors>
    <translators><person name="漆島賢二"/></translators>
  </header>

  <body>

    <s1 title="認証を含むコードのテストの導入/Introduction to testing secure code">

      <p>
        Beginning with version 1.3, Cactus is able to unit test Servlet code
        that uses the Servlet Security API (<code>getRemoteUser()</code>,
        <code>isUserInRole()</code>, <code>getUserPrincipal()</code>).
      </p>
      <p>
	バージョン 1.3 より、
	Servlet Security API (<code>getRemoteUser()</code>,
        <code>isUserInRole()</code>, <code>getUserPrincipal()</code>)を使った
	Servlet コードの単体テストができるようになりました。
      </p>
      <p>
        The way to perform this is by securing a Servlet Redirector defined
        in your <code>web.xml</code> and then to specify user credentials
        in your <code>beginXXX()</code>, as defined below.
      </p>
      <p>
	これを行う方法は、下で示すように、
	<code>web.xml</code> で定義された Servlet Redirector を保障されたものとし、
	<code>beginXXX()</code> において
	セキュリティ属性(credential)を指定するというものです。
      </p>

      <note>
        The only currently supported authentication mechanism in Cactus 1.3 is
        the BASIC authentication.
      </note>
      <note>
	現在、Cactus 1.3 でサポートされている認証メカニズムは BASIC 認証のみです。
      </note>

      <note>
        The Cactus sample application demonstrates how to unit test everything
        that is explained here.
      </note>
      <note>
	Cactus の例題では、
	ここで説明した全ての事を単体テストする方法について示します。
      </note>

    </s1>

    <s1 title="ステップ 1 : beginXXX() においてセキュリティ属性を渡す/Step 1 : Passing credentials in beginXXX()">

      <p>
        Let's start with an example :
      </p>
      <p>
        例から始めましょう :
      </p>

<source><![CDATA[
public void beginBasicAuthentication(WebRequest theRequest)
{
    theRequest.setRedirectorName("ServletRedirectorSecure");
    theRequest.setAuthentication(
        new BasicAuthentication("testuser", "testpwd"));
}

public void testBasicAuthentication()
{
    assertEquals("testuser", request.getUserPrincipal().getName());
    assertEquals("testuser", request.getRemoteUser());
    assertTrue("User not in 'test' role", request.isUserInRole("test"));
}
]]></source>

      <p>
        There are several things to understand here :
      </p>
      <p>
	ここで幾つか理解しておくべきことがあります : 
      </p>
      <ul>
        <li>
          The Servlet that is called on the server side is the Cactus
          redirector servlet and thus we'll need to secure it in
          our <code>web.xml</code> (see step 2 below).
        </li>
        <li>
	  サーバ側で呼ばれるサーブレットは Cactus リダイレクタサーブレットなので、
	  <code>web.xml</code> において、ユーザー認証に対応したものにしておく必要があります。
	  (ステップ 2 参照)
        </li>
        <li>
          <code>WebRequest.setRedirectorName()</code> is an API that lets you
          override the redirector defined in <code>cactus.properties</code>.
          This is needed here because we want to test both code that is not
          secured (i.e. for which we don't want to have to pass credentials)
          and code that is secured and thus we need 2 redirectors. The
          <code>ServletRedirectorSecure</code> redirector will be secured
          in step 2 below.
        </li>
        <li>
	  <code>WebRequest.setRedirectorName()</code> は
	  <code>cactus.properties</code>
	  で定義されたリダイレクタを上書きさせる API です。
	  これは、ここで必要です。
	  なぜなら、ユーザ認証に対応していないコード
	  (即ち、セキュリティ属性を渡す必要の無いもの)も、
	  ユーザ認証に対応したコードも両方テストしたいからです。
	  ですから、2 つのリダイレクタが必要です。
	  <code>ServletRedirectorSecure</code> リダイレクタは
	  下のステップ 2 において、ユーザ認証に対応できます。
        </li>
        <li>
          <code>WebRequest.setAuthentication()</code> is used to pass
          credentials to the server. It takes a parameter which is a
          <code>BasicAuthentication</code> object (the only type of
          authentication currently supported by Cactus).
        </li>
        <li>
          <code>WebRequest.setAuthentication()</code> 
	  は、サーバーへのセキュリティ属性を渡すために使われます。
	  これは、
	  <code>BasicAuthentication</code>
	  オブジェクト
	  (現在 Cactus がサポートしている唯一の認証のタイプ)
	  のパラメーターをとります。
        </li>
      </ul>

    </s1>

    <s1 title="ステップ 2 : Cactus リダイレクタにユーザ認証をかける/Step 2 : Securing the Cactus Redirector">

      <p>
       All calls to the server side go through the Cactus Servlet Redirector
       and thus it is that servlet that needs to be secured in
       <code>web.xml</code>. It is performed as follows (example) :
      </p>
      <p>
       サーバ側への全ての呼び出しは、
       Cactus サーブレットリダイレクタを通じて行われるので、
       <suspect>
	<code>web.xml</code>でユーザ認証に対応する必要があるのは、そのサーブレットなのです。</suspect>
      </p>

<source><![CDATA[
[...]

<web-app>

    <servlet>
        <servlet-name>ServletRedirector</servlet-name>
        <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
    </servlet>

    <servlet>
        <servlet-name>ServletRedirectorSecure</servlet-name>
        <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
    </servlet>

    [...]

    <servlet-mapping>
        <servlet-name>ServletRedirector</servlet-name>
        <url-pattern>/ServletRedirector</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>ServletRedirectorSecure</servlet-name>
        <url-pattern>/ServletRedirectorSecure</url-pattern>
    </servlet-mapping>

    [...]

  	<!-- Start Authentication -->
  	<!-- 認証の開始 -->

  	<security-constraint>
     	<web-resource-collection>
        	<web-resource-name>SecurityRestriction</web-resource-name>
         	<description>Protect the Cactus redirector servlet.</description>
         	<url-pattern>/ServletRedirectorSecure</url-pattern>
         	<http-method>GET</http-method>
         	<http-method>POST</http-method>
     	</web-resource-collection>
     	<auth-constraint>
         	<description>Authorized Users Group</description>
         	<role-name>test</role-name>
     	</auth-constraint>
     	<user-data-constraint>
        	<transport-guarantee>NONE</transport-guarantee>
    	</user-data-constraint>
   	</security-constraint>

    <login-config>
    	<auth-method>BASIC</auth-method>
   	</login-config>

	<security-role>
		<description>Test role</description>
		<role-name>test</role-name>
	</security-role>

  	<!-- End Authentication -->
  	<!-- 認証の終わり -->

</web-app>
]]></source>

    </s1>

    <s1 title="ステップ 3 : ユーザ/ロールのマッピング/Step 3 : Map Users/Roles">

      <p>
        This step consists in defining authorized users and mapping them to the
        role defined in <code>web.xml</code> (e.g. in the example above, we have
        defined a <code>test</code> role).
      </p>
      <p>
	この段階では、権限をもつユーザの定義と<code>web.xml</code>
	で定義されたロール(役割)との関連付けが含まれています。
        (例えば、上の例では <code>test</code> ロールを定義しました)
      </p>

      <p>
        This step is completely container-dependent and there is no standard
        way of doing it. You'll have to learn how to do it for your container.
        For example, here is how you would do it for Tomcat 4.0 :
      </p>
      <p>
	このステップは、完全にコンテナに依存し、
	これを行う標準的な方法はありません。
	使っているコンテナで行うやり方を勉強しなければなりません。
	例として、Tomcat 4.0 での設定法を示します : 
      </p>
      <ul>
        <li>
          Create a <code>tomcat-users.xml</code> file that you put in your
          Tomcat configuration directory (where you have
          <code>server.xml</code> defined).
        </li>
        <li>
	  Tomcat 設定ディレクトリに
	  <code>tomcat-users.xml</code> ファイルを作成します。
	  (ここでは <code>server.xml</code> が定義されています。)
        </li>
      </ul>
      <p>
        Here is an example of <code>tomcat-users.xml</code> that matches the
        test we have defined in step 1 :
      </p>
      <p>
	ここに、ステップ 1 で定義した test とマッチする
	<code>tomcat-users.xml</code> の例を示します : 
      </p>

<source><![CDATA[
<tomcat-users>
  <user name="testuser" password="testpwd" roles="test" />
</tomcat-users>
]]></source>

    </s1>

  </body>
</document>
