1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security.validate;
21
22 import java.security.Principal;
23 import java.util.Set;
24
25 import javax.security.auth.Subject;
26 import javax.security.auth.callback.CallbackHandler;
27 import javax.security.auth.login.LoginContext;
28 import javax.security.auth.login.LoginException;
29
30 import org.apache.ws.security.WSSecurityException;
31 import org.apache.ws.security.handler.RequestData;
32 import org.apache.ws.security.message.token.BinarySecurity;
33 import org.apache.ws.security.message.token.KerberosSecurity;
34 import org.apache.ws.security.message.token.KerberosServiceAction;
35
36
37
38 public class KerberosTokenValidator implements Validator {
39
40 private static org.apache.commons.logging.Log log =
41 org.apache.commons.logging.LogFactory.getLog(KerberosTokenValidator.class);
42
43 private String serviceName;
44 private CallbackHandler callbackHandler;
45 private String contextName;
46 private KerberosTokenDecoder kerberosTokenDecoder;
47
48
49
50
51
52 public String getContextName() {
53 return contextName;
54 }
55
56
57
58
59
60 public void setContextName(String contextName) {
61 this.contextName = contextName;
62 }
63
64
65
66
67
68
69 public String getJaasLoginModuleName() {
70 return contextName;
71 }
72
73
74
75
76
77
78 public void setJaasLoginModuleName(String jaasLoginModuleName) {
79 this.contextName = jaasLoginModuleName;
80 }
81
82
83
84
85
86 public CallbackHandler getCallbackHandler() {
87 return callbackHandler;
88 }
89
90
91
92
93
94 public void setCallbackHandler(CallbackHandler callbackHandler) {
95 this.callbackHandler = callbackHandler;
96 }
97
98
99
100
101
102
103 public void setServiceName(String serviceName) {
104 this.serviceName = serviceName;
105 }
106
107
108
109
110
111
112 public String getServiceName() {
113 return serviceName;
114 }
115
116
117
118
119
120
121 public KerberosTokenDecoder getKerberosTokenDecoder() {
122 return kerberosTokenDecoder;
123 }
124
125
126
127
128
129
130 public void setKerberosTokenDecoder(KerberosTokenDecoder kerberosTokenDecoder) {
131 this.kerberosTokenDecoder = kerberosTokenDecoder;
132 }
133
134
135
136
137
138
139
140
141 public Credential validate(Credential credential, RequestData data) throws WSSecurityException {
142 if (credential == null || credential.getBinarySecurityToken() == null) {
143 throw new WSSecurityException(WSSecurityException.FAILURE, "noCredential");
144 }
145
146 BinarySecurity binarySecurity = credential.getBinarySecurityToken();
147 if (!(binarySecurity instanceof KerberosSecurity)) {
148 return credential;
149 }
150
151 if (log.isDebugEnabled()) {
152 try {
153 String jaasAuth = System.getProperty("java.security.auth.login.config");
154 String krbConf = System.getProperty("java.security.krb5.conf");
155 log.debug("KerberosTokenValidator - Using JAAS auth login file: " + jaasAuth);
156 log.debug("KerberosTokenValidator - Using KRB conf file: " + krbConf);
157 } catch (SecurityException ex) {
158 log.debug(ex.getMessage(), ex);
159 }
160 }
161
162
163 LoginContext loginContext = null;
164 try {
165 if (callbackHandler == null) {
166 loginContext = new LoginContext(getContextName());
167 } else {
168 loginContext = new LoginContext(getContextName(), callbackHandler);
169 }
170 loginContext.login();
171 } catch (LoginException ex) {
172 if (log.isDebugEnabled()) {
173 log.debug(ex.getMessage(), ex);
174 }
175 throw new WSSecurityException(
176 WSSecurityException.FAILURE,
177 "kerberosLoginError",
178 new Object[] {ex.getMessage()},
179 ex
180 );
181 }
182 if (log.isDebugEnabled()) {
183 log.debug("Successfully authenticated to the TGT");
184 }
185
186 byte[] token = binarySecurity.getToken();
187
188
189 Subject subject = loginContext.getSubject();
190 String service = serviceName;
191 if (service == null) {
192 Set<Principal> principals = subject.getPrincipals();
193 if (principals.isEmpty()) {
194 throw new WSSecurityException(
195 WSSecurityException.FAILURE,
196 "kerberosLoginError",
197 new Object[] {"No Client principals found after login"}
198 );
199 }
200 service = principals.iterator().next().getName();
201 }
202
203
204 KerberosServiceAction action = new KerberosServiceAction(token, service);
205 Principal principal = (Principal)Subject.doAs(subject, action);
206 if (principal == null) {
207 throw new WSSecurityException(
208 WSSecurityException.FAILURE, "kerberosTicketValidationError"
209 );
210 }
211 credential.setPrincipal(principal);
212 credential.setSubject(subject);
213
214
215
216 if (kerberosTokenDecoder != null) {
217 kerberosTokenDecoder.clear();
218 kerberosTokenDecoder.setToken(token);
219 kerberosTokenDecoder.setSubject(subject);
220 byte[] sessionKey = kerberosTokenDecoder.getSessionKey();
221 credential.setSecretKey(sessionKey);
222 }
223
224 if (log.isDebugEnabled()) {
225 log.debug("Successfully validated a ticket");
226 }
227
228 return credential;
229 }
230
231 }