View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plus.jaas.callback;
20  
21  import java.io.IOException;
22  import java.util.Arrays;
23  
24  import javax.security.auth.callback.Callback;
25  import javax.security.auth.callback.NameCallback;
26  import javax.security.auth.callback.PasswordCallback;
27  import javax.security.auth.callback.UnsupportedCallbackException;
28  
29  import org.eclipse.jetty.util.security.Password;
30  import org.eclipse.jetty.server.Request;
31  
32  
33  
34  /* ---------------------------------------------------- */
35  /** DefaultUsernameCredentialCallbackHandler
36   * <p>
37   *
38   * <p><h4>Notes</h4>
39   * <p>
40   *
41   * <p><h4>Usage</h4>
42   * <pre>
43   */
44  /*
45   * </pre>
46   *
47   * @see
48   * @version 1.0 Tue Apr 15 2003
49   * 
50   */
51  public class DefaultCallbackHandler extends AbstractCallbackHandler
52  {
53       
54      private Request _request;
55      
56      public void setRequest (Request request)
57      {
58          this._request = request;
59      }
60      
61      public void handle (Callback[] callbacks)
62          throws IOException, UnsupportedCallbackException
63      {
64          for (int i=0; i < callbacks.length; i++)
65          {
66              if (callbacks[i] instanceof NameCallback)
67              {
68                  ((NameCallback)callbacks[i]).setName(getUserName());
69              }
70              else if (callbacks[i] instanceof ObjectCallback)
71              {
72                  ((ObjectCallback)callbacks[i]).setObject(getCredential());
73              }
74              else if (callbacks[i] instanceof PasswordCallback)
75              {
76                  if (getCredential() instanceof Password)
77                      ((PasswordCallback)callbacks[i]).setPassword (((Password)getCredential()).toString().toCharArray());
78                  else if (getCredential() instanceof String)
79                  {
80                      ((PasswordCallback)callbacks[i]).setPassword (((String)getCredential()).toCharArray());
81                  }
82                  else
83                      throw new UnsupportedCallbackException (callbacks[i], "User supplied credentials cannot be converted to char[] for PasswordCallback: try using an ObjectCallback instead");
84              }
85              else if (callbacks[i] instanceof RequestParameterCallback)
86              {
87                  RequestParameterCallback callback = (RequestParameterCallback)callbacks[i];
88                  callback.setParameterValues(Arrays.asList(_request.getParameterValues(callback.getParameterName())));
89              }
90              else
91                  throw new UnsupportedCallbackException(callbacks[i]);
92          }
93          
94      }
95      
96  }
97