KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
10 import java.security.Principal JavaDoc;
11 import javax.security.auth.callback.Callback JavaDoc;
12 import javax.security.auth.callback.CallbackHandler JavaDoc;
13 import javax.security.auth.callback.NameCallback JavaDoc;
14 import javax.security.auth.callback.PasswordCallback JavaDoc;
15 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
16
17 /** An implementation of CallbackHandler is assigned a Principal, and
18  opaque Object credential as values passed to the constructor. This is suitable
19  for environments that need non-interactive JAAS logins and is used by the
20  JaasSecurityManager as its default CallbackHandler.
21
22  @see javax.security.auth.callback.CallbackHandler
23  @see #handle(Callback[])
24
25  @author Scott.Stark@jboss.org
26  @version $Revision: 1.8 $
27  */

28 public class SecurityAssociationHandler implements CallbackHandler JavaDoc
29 {
30    private transient Principal JavaDoc principal;
31    private transient Object JavaDoc credential;
32
33    public SecurityAssociationHandler()
34    {
35    }
36
37    /** Initialize the UsernamePasswordHandler with the principal
38     and credentials to use.
39     */

40    public SecurityAssociationHandler(Principal JavaDoc principal, Object JavaDoc credential)
41    {
42       this.principal = principal;
43       this.credential = credential;
44    }
45
46    public void setSecurityInfo(Principal JavaDoc principal, Object JavaDoc credential)
47    {
48       this.principal = principal;
49       this.credential = credential;
50    }
51
52    /** Handles SecurityAssociationCallback, ObjectCallback, NameCallback and
53     PasswordCallback types. A NameCallback name property is set to
54     the Prinicpal.getName() value. A PasswordCallback password property is
55     set to the getPassword() value. The preferred SecurityAssociationCallback
56     has its principal and credential properties set to the instance principal
57     and credential. An ObjectCallback has its credential set to the credential
58     value.
59
60     @see #getPassword()
61     @exception UnsupportedCallbackException, thrown if any callback of
62     type other than SecurityAssociationCallback, ObjectCallback, NameCallback
63     or PasswordCallback are seen.
64     */

65    public void handle(Callback JavaDoc[] callbacks) throws
66       UnsupportedCallbackException JavaDoc
67    {
68       for (int i = 0; i < callbacks.length; i++)
69       {
70          Callback JavaDoc c = callbacks[i];
71          if (c instanceof SecurityAssociationCallback)
72          {
73             SecurityAssociationCallback sac = (SecurityAssociationCallback) c;
74             sac.setPrincipal(principal);
75             sac.setCredential(credential);
76          }
77          else if (c instanceof ObjectCallback)
78          {
79             ObjectCallback oc = (ObjectCallback) c;
80             oc.setCredential(credential);
81          }
82          else if (c instanceof NameCallback JavaDoc)
83          {
84             NameCallback JavaDoc nc = (NameCallback JavaDoc) c;
85             if (principal != null)
86                nc.setName(principal.getName());
87          }
88          else if (c instanceof PasswordCallback JavaDoc)
89          {
90             PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) c;
91             char[] password = getPassword();
92             if (password != null)
93                pc.setPassword(password);
94          }
95          else
96          {
97             throw new UnsupportedCallbackException JavaDoc(c, "Unrecognized Callback");
98          }
99       }
100    }
101
102    /** Try to convert the credential value into a char[] using the
103     first of the following attempts which succeeds:
104
105     1. Check for instanceof char[]
106     2. Check for instanceof String and then use toCharArray()
107     3. See if credential has a toCharArray() method and use it
108     4. Use toString() followed by toCharArray().
109     @return a char[] representation of the credential.
110     */

111    private char[] getPassword()
112    {
113       char[] password = null;
114       if (credential instanceof char[])
115       {
116          password = (char[]) credential;
117       }
118       else if (credential instanceof String JavaDoc)
119       {
120          String JavaDoc s = (String JavaDoc) credential;
121          password = s.toCharArray();
122       }
123       else
124       {
125          try
126          {
127             Class JavaDoc[] types = {};
128             Method JavaDoc m = credential.getClass().getMethod("toCharArray", types);
129             Object JavaDoc[] args = {};
130             password = (char[]) m.invoke(credential, args);
131          }
132          catch (Exception JavaDoc e)
133          {
134             if (credential != null)
135             {
136                String JavaDoc s = credential.toString();
137                password = s.toCharArray();
138             }
139          }
140       }
141       return password;
142    }
143 }
144
Popular Tags