1 16 17 package org.apache.axis.handlers ; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Handler; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.components.logger.LogFactory; 23 import org.apache.axis.security.AuthenticatedUser; 24 import org.apache.axis.security.SecurityProvider; 25 import org.apache.axis.utils.JavaUtils; 26 import org.apache.axis.utils.Messages; 27 import org.apache.commons.logging.Log; 28 29 import java.util.StringTokenizer ; 30 31 32 49 public class SimpleAuthorizationHandler extends BasicHandler { 50 protected static Log log = 51 LogFactory.getLog(SimpleAuthorizationHandler.class.getName()); 52 53 56 public void invoke(MessageContext msgContext) throws AxisFault { 57 if (log.isDebugEnabled()) { 58 log.debug("Enter: SimpleAuthorizationHandler::invoke"); 59 } 60 61 boolean allowByDefault = 62 JavaUtils.isTrueExplicitly(getOption("allowByDefault")); 63 64 AuthenticatedUser user = (AuthenticatedUser)msgContext. 65 getProperty(MessageContext.AUTHUSER); 66 67 if (user == null) 68 throw new AxisFault("Server.NoUser", 69 Messages.getMessage("needUser00"), null, null); 70 71 String userID = user.getName(); 72 Handler serviceHandler = msgContext.getService(); 73 74 if (serviceHandler == null) 75 throw new AxisFault(Messages.getMessage("needService00")); 76 77 String serviceName = serviceHandler.getName(); 78 79 String allowedRoles = (String )serviceHandler.getOption("allowedRoles"); 80 if (allowedRoles == null) { 81 if (allowByDefault) { 82 if (log.isDebugEnabled()) { 83 log.debug(Messages.getMessage( "noRoles00")); 84 } 85 } 86 else { 87 if (log.isDebugEnabled()) { 88 log.debug(Messages.getMessage( "noRoles01")); 89 } 90 91 throw new AxisFault( "Server.Unauthorized", 92 Messages.getMessage("notAuth00", userID, serviceName), 93 null, null ); 94 } 95 96 if (log.isDebugEnabled()) { 97 log.debug("Exit: SimpleAuthorizationHandler::invoke"); 98 } 99 return; 100 } 101 102 SecurityProvider provider = (SecurityProvider)msgContext.getProperty(MessageContext.SECURITY_PROVIDER); 103 if (provider == null) 104 throw new AxisFault(Messages.getMessage("noSecurity00")); 105 106 StringTokenizer st = new StringTokenizer (allowedRoles, ","); 107 while (st.hasMoreTokens()) { 108 String thisRole = st.nextToken(); 109 if (provider.userMatches(user, thisRole)) { 110 111 if (log.isDebugEnabled()) { 112 log.debug(Messages.getMessage("auth01", 113 userID, serviceName)); 114 } 115 116 if (log.isDebugEnabled()) { 117 log.debug("Exit: SimpleAuthorizationHandler::invoke"); 118 } 119 return; 120 } 121 } 122 123 throw new AxisFault( "Server.Unauthorized", 124 Messages.getMessage("cantAuth02", userID, serviceName), 125 null, null ); 126 } 127 128 131 public void onFault(MessageContext msgContext) { 132 if (log.isDebugEnabled()) { 133 log.debug("Enter: SimpleAuthorizationHandler::onFault"); 134 log.debug("Exit: SimpleAuthorizationHandler::onFault"); 135 } 136 } 137 }; 138 | Popular Tags |