1 23 24 29 30 package com.sun.enterprise.security.wss; 31 32 import java.util.HashMap ; 33 import java.util.Set ; 34 35 import com.sun.enterprise.security.jauth.*; 36 import com.sun.enterprise.security.ClientSecurityContext; 37 import com.sun.enterprise.security.SecurityContext; 38 import com.sun.enterprise.security.audit.AuditManager; 39 import com.sun.enterprise.security.audit.AuditManagerFactory; 40 41 import java.security.Principal ; 42 import javax.security.auth.Subject ; 43 import javax.xml.soap.SOAPMessage ; 44 import javax.servlet.http.HttpServletRequest ; 45 46 import com.sun.logging.*; 47 import java.util.logging.*; 48 49 import com.sun.enterprise.webservice.WsUtil; 50 import com.sun.enterprise.webservice.monitoring.Endpoint; 51 import com.sun.enterprise.webservice.monitoring.EndpointType; 52 import com.sun.enterprise.webservice.monitoring.WebServiceEngineImpl; 53 54 59 60 public class WebServiceSecurity { 61 62 private static Logger _logger=null; 63 static { 64 _logger = LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 65 } 66 67 private static AuditManager auditManager = 68 AuditManagerFactory.getAuditManagerInstance(); 69 70 private static final String SHARED_CLIENT_STATE = 72 "com.sun.enterprise.security.jauth.ClientHashMap"; 73 74 private static final String SHARED_SERVER_STATE = 75 "com.sun.enterprise.security.jauth.ServerHashMap"; 76 77 private WebServiceSecurity () { 78 } 79 80 public static boolean 82 validateRequest(javax.xml.rpc.handler.soap.SOAPMessageContext context, 83 ServerAuthContext sAC) 84 throws AuthException 85 { 86 boolean rvalue = true; 87 SOAPAuthParam param = 88 new SOAPAuthParam(WsUtil.getMessage(context), null); 89 90 HashMap sharedState = new HashMap (); 92 context.setProperty(SHARED_SERVER_STATE, sharedState); 93 94 try { 95 rvalue = validateRequest(param, sharedState, sAC); 96 } catch(PendingException pe){ 97 _logger.log(Level.FINE, 98 "Container-auth: wss: Error validating request ",pe); 99 context.setMessage(param.getResponse()); 100 rvalue = false; 101 } catch(FailureException fe){ 102 _logger.log(Level.FINE, 103 "Container-auth: wss: Error validating request ",fe); 104 context.setMessage(param.getResponse()); 105 throw fe; 106 } 107 return rvalue; 108 } 109 110 public static boolean 112 validateRequest(javax.xml.ws.handler.soap.SOAPMessageContext context, 113 ServerAuthContext sAC) 114 throws AuthException 115 { 116 boolean rvalue = true; 117 SOAPAuthParam param = 118 new SOAPAuthParam(WsUtil.getMessage(context), null); 119 120 HashMap sharedState = new HashMap (); 122 context.put(SHARED_SERVER_STATE, sharedState); 123 124 try { 125 rvalue = validateRequest(param, sharedState, sAC); 126 } catch(PendingException pe){ 127 _logger.log(Level.FINE, 128 "Container-auth: wss: Error validating request ",pe); 129 context.setMessage(param.getResponse()); 130 rvalue = false; 131 } catch(FailureException fe){ 132 _logger.log(Level.FINE, 133 "Container-auth: wss: Error validating request ",fe); 134 context.setMessage(param.getResponse()); 135 throw fe; 136 } 137 return rvalue; 138 } 139 140 private static boolean 141 validateRequest(AuthParam param, HashMap sharedState, 142 ServerAuthContext sAC) 143 throws AuthException 144 { 145 boolean rvalue = true; 146 147 if(_logger.isLoggable(Level.FINE)) { 148 _logger.log(Level.FINE, 149 "Container Auth: ServerAuthContext.validateRequest"); 150 } 151 152 Subject subject = null; 153 boolean firstAuthentication = true; 154 SecurityContext sc = SecurityContext.getCurrent(); 155 if (sc == null || sc.didServerGenerateCredentials()) { 156 subject = new Subject (); 157 } else { 158 subject = sc.getSubject(); 159 firstAuthentication = false; 160 } 161 162 sAC.validateRequest((AuthParam)param, subject, sharedState); 163 164 if (rvalue && firstAuthentication) { 165 Set principalSet = subject.getPrincipals(); 166 if (principalSet != null && !principalSet.isEmpty()) { 169 String initiator = ((Principal )principalSet.iterator().next()). 172 getName(); 173 SecurityContext newSC = new SecurityContext(initiator,subject); 174 SecurityContext.setCurrent(newSC); 175 } 176 } 177 178 return rvalue; 179 } 180 181 public static void 183 secureResponse(javax.xml.rpc.handler.soap.SOAPMessageContext context, 184 ServerAuthContext sAC) 185 throws AuthException 186 { 187 secureResponse(WsUtil.getMessage(context), 188 (HashMap ) context.getProperty(SHARED_SERVER_STATE), 189 sAC); 190 } 191 192 public static void 194 secureResponse(javax.xml.ws.handler.soap.SOAPMessageContext context, ServerAuthContext sAC) 195 throws AuthException 196 { 197 secureResponse(WsUtil.getMessage(context), 198 (HashMap ) context.get(SHARED_SERVER_STATE), 199 sAC); 200 } 201 202 private static void 203 secureResponse(SOAPMessage response, HashMap sharedState, 204 ServerAuthContext sAC) 205 throws AuthException 206 { 207 if(_logger.isLoggable(Level.FINE)) { 208 _logger.log(Level.FINE, 209 "Container Auth: ServerAuthContext.secureResponse"); 210 } 211 212 SecurityContext sc = SecurityContext.getCurrent(); 215 Subject subject = sc.getSubject(); 216 217 SOAPAuthParam param = new SOAPAuthParam(null, response); 218 219 try{ 220 sAC.secureResponse((AuthParam)param, subject, sharedState); 221 } finally { 222 sAC.disposeSubject(subject,sharedState); 223 } 224 225 return; 226 } 227 228 public static void 230 secureRequest(javax.xml.rpc.handler.soap.SOAPMessageContext context, 231 ClientAuthContext cAC, boolean isAppClient) 232 throws AuthException 233 { 234 HashMap sharedState = new HashMap (); 236 context.setProperty(SHARED_CLIENT_STATE, sharedState); 237 sharedState.put(javax.xml.ws.handler.MessageContext.WSDL_SERVICE, 238 context.getProperty(javax.xml.ws.handler.MessageContext.WSDL_SERVICE)); 239 240 secureRequest 241 (WsUtil.getMessage(context), sharedState, cAC, isAppClient); 242 } 243 244 public static void 246 secureRequest(javax.xml.ws.handler.soap.SOAPMessageContext context, 247 ClientAuthContext cAC, boolean isAppClient) 248 throws AuthException 249 { 250 HashMap sharedState = new HashMap (); 252 context.put(SHARED_CLIENT_STATE, sharedState); 253 sharedState.put(javax.xml.ws.handler.MessageContext.WSDL_SERVICE, 254 context.get(javax.xml.ws.handler.MessageContext.WSDL_SERVICE)); 255 256 secureRequest 257 (WsUtil.getMessage(context), sharedState, cAC, isAppClient); 258 } 259 260 private static void 261 secureRequest(SOAPMessage request, HashMap sharedState, 262 ClientAuthContext cAC, boolean isAppClient) 263 throws AuthException 264 { 265 266 if(_logger.isLoggable(Level.FINE)) { 267 _logger.log(Level.FINE, 268 "Container Auth: ClientAuthContext.secureRequest"); 269 } 270 271 SOAPAuthParam param = new SOAPAuthParam(request, null); 272 273 Subject subject = null; 274 if (isAppClient) { 275 ClientSecurityContext sc = ClientSecurityContext.getCurrent(); 276 if (sc != null) { 277 subject = sc.getSubject(); 278 } 279 } else { 280 SecurityContext sc = SecurityContext.getCurrent(); 281 if (sc != null && !sc.didServerGenerateCredentials()) { 282 subject = sc.getSubject(); 286 } 287 } 288 if (subject == null) subject = new Subject (); 289 290 cAC.secureRequest ( param, subject, sharedState); 291 } 292 293 public static boolean 295 validateResponse(javax.xml.rpc.handler.soap.SOAPMessageContext context, 296 ClientAuthContext cAC) 297 throws AuthException 298 { 299 return validateResponse 300 (WsUtil.getMessage(context), 301 (HashMap ) context.getProperty(SHARED_CLIENT_STATE), cAC); 302 } 303 304 public static boolean 306 validateResponse(javax.xml.ws.handler.soap.SOAPMessageContext context, 307 ClientAuthContext cAC) 308 throws AuthException 309 { 310 return validateResponse 311 (WsUtil.getMessage(context), 312 (HashMap ) context.get(SHARED_CLIENT_STATE), cAC); 313 } 314 315 private static boolean 316 validateResponse(SOAPMessage response, HashMap sharedState, 317 ClientAuthContext cAC) 318 throws AuthException 319 { 320 boolean rvalue = true; 321 322 Subject responderSubject = new Subject (); 324 325 SOAPAuthParam param = new SOAPAuthParam(null, response); 326 327 try{ 328 cAC.validateResponse( param, responderSubject, sharedState); 329 } catch(AuthException ae){ 330 _logger.log(Level.SEVERE, 331 "Container-auth: wss: Error validating response ", ae); 332 rvalue = false; 333 throw ae; 334 } finally { 335 cAC.disposeSubject(responderSubject,sharedState); 336 } 337 338 return rvalue; 339 } 340 341 public static void auditInvocation 343 (javax.xml.rpc.handler.soap.SOAPMessageContext context, boolean status) { 344 345 if (auditManager.isAuditOn()) { 346 347 350 HttpServletRequest req = (HttpServletRequest )context.getProperty 351 ("com.sun.xml.rpc.server.http.HttpServletRequest"); 352 353 String uri = null; 354 355 if( req != null ) { 356 uri = req.getRequestURI(); 357 } 358 359 Endpoint endpoint = WebServiceEngineImpl.getInstance().getEndpoint 360 (req.getRequestURL().toString()); 361 362 String epName = null; 363 364 if( endpoint != null ) { 365 epName = endpoint.getDescriptor().getEndpointName(); 366 } 367 368 auditManager.webServiceInvocation 369 ( ((uri==null) ? "(no uri)" : uri), 370 ((epName==null) ? "(no endpoint)" : epName), 371 status); 372 } 373 } 374 375 public static void auditInvocation 377 (javax.xml.ws.handler.soap.SOAPMessageContext context, boolean status) { 378 379 if (auditManager.isAuditOn()) { 380 381 HttpServletRequest req = (HttpServletRequest )context.get 382 (javax.xml.ws.handler.MessageContext.SERVLET_REQUEST); 383 384 String uri = null; 385 386 if( req != null ) { 387 uri = req.getRequestURI(); 388 } 389 390 String epName = null; 391 392 Endpoint endpoint = WebServiceEngineImpl.getInstance().getEndpoint 393 (req.getRequestURL().toString()); 394 395 if( endpoint != null ) { 396 epName = endpoint.getDescriptor().getEndpointName(); 397 } 398 399 auditManager.webServiceInvocation 400 ( ((uri==null) ? "(no uri)" : uri), 401 ((epName==null) ? "(no endpoint)" : epName), 402 status); 403 } 404 } 405 406 } 407 | Popular Tags |