001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.ksb.messaging.servicehandlers; 017 018 import java.io.IOException; 019 020 import javax.security.auth.callback.Callback; 021 import javax.security.auth.callback.CallbackHandler; 022 import javax.security.auth.callback.UnsupportedCallbackException; 023 import javax.xml.namespace.QName; 024 025 import org.apache.ws.security.WSPasswordCallback; 026 import org.kuali.rice.core.api.exception.RiceRuntimeException; 027 import org.kuali.rice.ksb.service.KSBServiceLocator; 028 029 /** 030 * CallbackHandler that verifies the password and username is correct for a service 031 * secured with basic authentication. 032 * 033 * @author Kuali Rice Team (rice.collab@kuali.org) 034 */ 035 public class BasicAuthenticationHandler implements CallbackHandler { 036 037 private String serviceNameSpaceURI; 038 private QName localServiceName; 039 040 /** 041 * Initialize the BasicAuthenticationHandler with the serviceNameSpaceURI and localServiceName 042 * 043 * @param serviceNameSpaceURI the serviceNameSpaceURI to use 044 * @param serviceName the serviceName to use 045 */ 046 public BasicAuthenticationHandler(String serviceNameSpaceURI, QName serviceName) { 047 this.serviceNameSpaceURI = serviceNameSpaceURI; 048 this.localServiceName = serviceName; 049 } 050 051 /** 052 * @param callbacks an array of Callback objects 053 * @throws RiceRuntimeException if the username or password is invalid 054 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[]) 055 */ 056 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { 057 if (callbacks[0] != null && callbacks[0] instanceof WSPasswordCallback) { 058 WSPasswordCallback pc = (WSPasswordCallback) callbacks[0]; 059 if (!KSBServiceLocator.getBasicAuthenticationService().checkServiceAuthentication(this.serviceNameSpaceURI, 060 this.localServiceName, pc.getIdentifier(), pc.getPassword())) { 061 throw new RiceRuntimeException("Invalid username or password"); 062 } 063 } 064 } 065 }