KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > callback > UsernamePasswordHandler


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security.auth.callback;
8
9 import javax.security.auth.callback.Callback JavaDoc;
10 import javax.security.auth.callback.CallbackHandler JavaDoc;
11 import javax.security.auth.callback.NameCallback JavaDoc;
12 import javax.security.auth.callback.PasswordCallback JavaDoc;
13 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
14
15 /** A simple implementation of CallbackHandler that sets a username and
16 password in the handle(Callback[]) method to that passed in to
17 the constructor. This is suitable for environments that need non-interactive
18 JAAS logins.
19
20  @see javax.security.auth.callback.CallbackHandler
21  @see #handle(Callback[])
22
23  @author Scott.Stark@jboss.org
24  @version $Revision: 1.5 $
25  */

26 public class UsernamePasswordHandler implements CallbackHandler JavaDoc
27 {
28    private transient String JavaDoc username;
29    private transient char[] password;
30    private transient Object JavaDoc credential;
31
32    /** Initialize the UsernamePasswordHandler with the usernmae
33     and password to use.
34     */

35    public UsernamePasswordHandler(String JavaDoc username, char[] password)
36    {
37       this.username = username;
38       this.password = password;
39       this.credential = password;
40    }
41
42    public UsernamePasswordHandler(String JavaDoc username, Object JavaDoc credential)
43    {
44       this.username = username;
45       this.credential = credential;
46    }
47
48    /** Sets any NameCallback name property to the instance username,
49     sets any PasswordCallback password property to the instance, and any
50     password.
51     @exception UnsupportedCallbackException, thrown if any callback of
52     type other than NameCallback or PasswordCallback are seen.
53     */

54    public void handle(Callback JavaDoc[] callbacks) throws
55          UnsupportedCallbackException JavaDoc
56    {
57       for (int i = 0; i < callbacks.length; i++)
58       {
59          Callback JavaDoc c = callbacks[i];
60          if (c instanceof NameCallback JavaDoc)
61          {
62             NameCallback JavaDoc nc = (NameCallback JavaDoc) c;
63             nc.setName(username);
64          }
65          else if (c instanceof PasswordCallback JavaDoc)
66          {
67             PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) c;
68             if( password == null )
69             {
70                // We were given an opaque Object credential but a char[] is requested?
71
if( credential != null )
72                {
73                   String JavaDoc tmp = credential.toString();
74                   password = tmp.toCharArray();
75                }
76             }
77             pc.setPassword(password);
78          }
79          else if (c instanceof ObjectCallback)
80          {
81             ObjectCallback oc = (ObjectCallback) c;
82             oc.setCredential(credential);
83          }
84          else
85          {
86             throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
87          }
88       }
89    }
90 }
91
Popular Tags