KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > security > authenticators > SecurityAssociationActions


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.security.authenticators;
23
24 import java.security.PrivilegedAction JavaDoc;
25 import java.security.Principal JavaDoc;
26 import java.security.AccessController JavaDoc;
27
28 import javax.security.auth.Subject JavaDoc;
29
30 import org.jboss.security.SecurityAssociation;
31 import org.jboss.security.RunAsIdentity;
32
33 /** A PrivilegedAction implementation for setting the SecurityAssociation
34  * principal and credential
35  *
36  * @author Scott.Stark@jboss.org
37  * @version $Revison:$
38  */

39 class SecurityAssociationActions
40 {
41    public static final String JavaDoc AUTH_EXCEPTION_KEY = "org.jboss.security.exception";
42
43    private static class SetPrincipalInfoAction implements PrivilegedAction JavaDoc
44    {
45       Principal JavaDoc principal;
46       Object JavaDoc credential;
47       Subject JavaDoc subject;
48       SetPrincipalInfoAction(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject)
49       {
50          this.principal = principal;
51          this.credential = credential;
52          this.subject = subject;
53       }
54
55       public Object JavaDoc run()
56       {
57          SecurityAssociation.pushSubjectContext(subject, principal, credential);
58          credential = null;
59          principal = null;
60          subject = null;
61          return null;
62       }
63    }
64    private static class SetServerAction implements PrivilegedAction JavaDoc
65    {
66       static PrivilegedAction JavaDoc ACTION = new SetServerAction();
67       public Object JavaDoc run()
68       {
69          SecurityAssociation.setServer();
70          return null;
71       }
72    }
73    private static class ClearAction implements PrivilegedAction JavaDoc
74    {
75       static PrivilegedAction JavaDoc ACTION = new ClearAction();
76       public Object JavaDoc run()
77       {
78          SecurityAssociation.clear();
79          return null;
80       }
81    }
82    private static class GetSubjectAction implements PrivilegedAction JavaDoc
83    {
84       static PrivilegedAction JavaDoc ACTION = new GetSubjectAction();
85       public Object JavaDoc run()
86       {
87          Subject JavaDoc subject = SecurityAssociation.getSubject();
88          return subject;
89       }
90    }
91    private static class GetPrincipalAction implements PrivilegedAction JavaDoc
92    {
93       static PrivilegedAction JavaDoc ACTION = new GetPrincipalAction();
94       public Object JavaDoc run()
95       {
96          Principal JavaDoc principal = SecurityAssociation.getPrincipal();
97          return principal;
98       }
99    }
100    private static class GetCredentialAction implements PrivilegedAction JavaDoc
101    {
102       static PrivilegedAction JavaDoc ACTION = new GetCredentialAction();
103       public Object JavaDoc run()
104       {
105          Object JavaDoc credential = SecurityAssociation.getCredential();
106          return credential;
107       }
108    }
109    private static class PushRunAsRoleAction implements PrivilegedAction JavaDoc
110    {
111       RunAsIdentity principal;
112       PushRunAsRoleAction(RunAsIdentity principal)
113       {
114          this.principal = principal;
115       }
116       public Object JavaDoc run()
117       {
118          SecurityAssociation.pushRunAsIdentity(principal);
119          return null;
120       }
121    }
122
123    private static class PopRunAsRoleAction implements PrivilegedAction JavaDoc
124    {
125       static PrivilegedAction JavaDoc ACTION = new PopRunAsRoleAction();
126       public Object JavaDoc run()
127       {
128          RunAsIdentity principal = SecurityAssociation.popRunAsIdentity();
129          return principal;
130       }
131    }
132    private static class GetAuthExceptionAction implements PrivilegedAction JavaDoc
133    {
134       static PrivilegedAction JavaDoc ACTION = new GetAuthExceptionAction();
135       public Object JavaDoc run()
136       {
137          Object JavaDoc exception = SecurityAssociation.getContextInfo(AUTH_EXCEPTION_KEY);
138          return exception;
139       }
140    }
141    private static class ClearAuthExceptionAction implements PrivilegedAction JavaDoc
142    {
143       static PrivilegedAction JavaDoc ACTION = new ClearAuthExceptionAction();
144       public Object JavaDoc run()
145       {
146          Object JavaDoc exception = SecurityAssociation.setContextInfo(AUTH_EXCEPTION_KEY, null);
147          return exception;
148       }
149    }
150
151    static void setPrincipalInfo(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject)
152    {
153       SetPrincipalInfoAction action = new SetPrincipalInfoAction(principal, credential, subject);
154       AccessController.doPrivileged(action);
155    }
156    static void setServer()
157    {
158       AccessController.doPrivileged(SetServerAction.ACTION);
159    }
160    static void clear()
161    {
162       AccessController.doPrivileged(ClearAction.ACTION);
163    }
164    static Subject JavaDoc getSubject()
165    {
166       Subject JavaDoc subject = (Subject JavaDoc) AccessController.doPrivileged(GetSubjectAction.ACTION);
167       return subject;
168    }
169    static Principal JavaDoc getPrincipal()
170    {
171       Principal JavaDoc principal = (Principal JavaDoc) AccessController.doPrivileged(GetPrincipalAction.ACTION);
172       return principal;
173    }
174    static Object JavaDoc getCredential()
175    {
176       Object JavaDoc credential = AccessController.doPrivileged(GetCredentialAction.ACTION);
177       return credential;
178    }
179    static void pushRunAsIdentity(RunAsIdentity principal)
180    {
181       PushRunAsRoleAction action = new PushRunAsRoleAction(principal);
182       AccessController.doPrivileged(action);
183    }
184    static RunAsIdentity popRunAsIdentity()
185    {
186       RunAsIdentity principal = (RunAsIdentity) AccessController.doPrivileged(PopRunAsRoleAction.ACTION);
187       return principal;
188    }
189
190    static Throwable JavaDoc getAuthException()
191    {
192       Throwable JavaDoc ex = (Throwable JavaDoc) AccessController.doPrivileged(GetAuthExceptionAction.ACTION);
193       return ex;
194    }
195    static void clearAuthException()
196    {
197       AccessController.doPrivileged(ClearAuthExceptionAction.ACTION);
198    }
199 }
200
Popular Tags