View Javadoc

    1   // ========================================================================
    2   // $Id: DefaultCallbackHandler.java 305 2006-03-07 10:32:14Z janb $
    3   // Copyright 2003-2004 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.jetty.plus.jaas.callback;
    17
    18  import java.io.IOException;
    19  import java.util.Arrays;
    20
    21  import javax.security.auth.callback.Callback;
    22  import javax.security.auth.callback.NameCallback;
    23  import javax.security.auth.callback.PasswordCallback;
    24  import javax.security.auth.callback.UnsupportedCallbackException;
    25
    26  import org.mortbay.jetty.Request;
    27  import org.mortbay.jetty.security.Password;
    28
    29
    30
    31  /* ---------------------------------------------------- */
    32  /** DefaultUsernameCredentialCallbackHandler
    33   * <p>
    34   *
    35   * <p><h4>Notes</h4>
    36   * <p>
    37   *
    38   * <p><h4>Usage</h4>
    39   * <pre>
    40   */
    41  /*
    42   * </pre>
    43   *
    44   * @see
    45   * @version 1.0 Tue Apr 15 2003
    46   * @author Jan Bartel (janb)
    47   */
    48  public class DefaultCallbackHandler extends AbstractCallbackHandler
    49  {
    50
    51      private Request request;
    52
    53      public void setRequest (Request request)
    54      {
    55          this.request = request;
    56      }
    57
    58      public void handle (Callback[] callbacks)
    59          throws IOException, UnsupportedCallbackException
    60      {
    61          for (int i=0; i < callbacks.length; i++)
    62          {
    63              if (callbacks[i] instanceof NameCallback)
    64              {
    65                  ((NameCallback)callbacks[i]).setName(getUserName());
    66              }
    67              else if (callbacks[i] instanceof ObjectCallback)
    68              {
    69                  ((ObjectCallback)callbacks[i]).setObject(getCredential());
    70              }
    71              else if (callbacks[i] instanceof PasswordCallback)
    72              {
    73                  if (getCredential() instanceof Password)
    74                      ((PasswordCallback)callbacks[i]).setPassword (((Password)getCredential()).toString().toCharArray());
    75                  else if (getCredential() instanceof String)
    76                  {
    77                      ((PasswordCallback)callbacks[i]).setPassword (((String)getCredential()).toCharArray());
    78                  }
    79                  else
    80                      throw new UnsupportedCallbackException (callbacks[i], "User supplied credentials cannot be converted to char[] for PasswordCallback: try using an ObjectCallback instead");
    81              }
    82              else if (callbacks[i] instanceof RequestParameterCallback)
    83              {
    84                  RequestParameterCallback callback = (RequestParameterCallback)callbacks[i];
    85                  callback.setParameterValues(Arrays.asList(request.getParameterValues(callback.getParameterName())));
    86              }
    87              else
    88                  throw new UnsupportedCallbackException(callbacks[i]);
    89          }
    90
    91      }
    92
    93  }
    94