added PropertiesFile support for BasicAuthentication

master
Vishnu 2014-04-17 15:55:37 +05:30 committed by David BRASSELY
parent d5e2a7b00f
commit ec8db414af
5 changed files with 250 additions and 2 deletions

View File

@ -43,7 +43,7 @@ public class BasicAuthenticationDetail extends BasicAuthSecurityExtension
implements ExtensibilityElement, Serializable {
public enum CredentialValidationType {
AM, Realm, StringCompare
AM, Realm, StringCompare,PropertyFileAuthentication
}
// Local element name

View File

@ -0,0 +1,85 @@
/*
* BEGIN_HEADER - DO NOT EDIT
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
* If applicable add the following below this CDDL HEADER,
* with the fields enclosed by brackets "[]" replaced with
* your own identifying information: Portions Copyright
* [year] [name of copyright owner]
*/
/*
* @(#)StringCompareValidator.java
*
* Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
*
* END_HEADER - DO NOT EDIT
*/
package com.sun.jbi.httpsoapbc.extensions;
import java.io.Serializable;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.xml.namespace.QName;
/*
* Class holding information pertaining to authenticating credentials against username and password
* Strings.
*/
public class PropertiesFileValidation extends ValidationBaseType
implements ExtensibilityElement, Serializable {
// Local element name
public static final String ELEM_PROPERTIES_Compare = "PropertesFileAuthentication";
public static final String PROPERTIES_FILE_PATH="path";
public static final String PATH_NOT_DEFINED="NO "+PROPERTIES_FILE_PATH +" DEFINED";
// QName representing this Extensibility Element
private QName QNAME_StringCompare =
new QName(NS_URI_HTTPBC_SEC_EXTENSION, ELEM_PROPERTIES_Compare);
private Boolean mFieldRequired = false;
private String propertiesFileLocation="";
public void setElementType(QName arg0) {
QNAME_StringCompare = arg0;
}
public QName getElementType() {
return QNAME_StringCompare;
}
public void setRequired(Boolean arg0) {
mFieldRequired = arg0;
}
public Boolean getRequired() {
return mFieldRequired;
}
public String getPropertiesFileLocation() {
return propertiesFileLocation;
}
public void setPropertiesFileLocation(String propertiesFileLOcation) {
this.propertiesFileLocation = propertiesFileLOcation;
}
}

View File

@ -458,6 +458,15 @@ public class SOAPExtSerializer
detail.setCredentialValidation(rv);
break;
}
else if (dcelemLN.equals(PropertiesFileValidation.ELEM_PROPERTIES_Compare)) {
detail.setCredentialValidationType(BasicAuthenticationDetail.CredentialValidationType.PropertyFileAuthentication);
String path = dcelem.getAttribute(PropertiesFileValidation.PROPERTIES_FILE_PATH);
PropertiesFileValidation pfv = new PropertiesFileValidation();
pfv.setPropertiesFileLocation(path);
detail.setCredentialValidation(pfv);
break;
}
}
}
}

View File

@ -42,9 +42,11 @@ import com.sun.jbi.httpsoapbc.security.realm.impl.SunRealmCredentialValidator;
import com.sun.jbi.httpsoapbc.extensions.Policy;
import com.sun.jbi.httpsoapbc.extensions.BasicAuthenticationDetail.CredentialValidationType;
import com.sun.jbi.httpsoapbc.extensions.AccessManagerValidation;
import com.sun.jbi.httpsoapbc.extensions.PropertiesFileValidation;
import com.sun.jbi.httpsoapbc.extensions.RealmValidation;
import com.sun.jbi.httpsoapbc.extensions.StringCompareValidation;
import com.sun.jbi.httpsoapbc.extensions.ValidationBaseType;
import com.sun.jbi.httpsoapbc.security.sc.impl.PropertiesFileCredentialValidator;
import com.sun.jbi.internationalization.Messages;
@ -64,11 +66,13 @@ public class CredentialValidatorManager {
private SunAccessManagerCredentialValidator amValidator;
private Map<String /*realmName*/, RealmRefCount> realmValidators;
private RuntimeConfigurationMBean rtc;
private Map<String /*endpointName*/, PropertiesFileCredentialValidator> propertiesFileValidators;
public CredentialValidatorManager (RuntimeConfigurationMBean rtc) {
this.rtc = rtc;
scValidators = Collections.synchronizedMap(new HashMap());
realmValidators = Collections.synchronizedMap(new HashMap());
propertiesFileValidators=Collections.synchronizedMap(new HashMap());
}
private class RealmRefCount {
@ -106,6 +110,21 @@ public class CredentialValidatorManager {
cv = scValidators.get(uniqueEndpointName);
}
break;
case PropertyFileAuthentication:
synchronized (propertiesFileValidators) {
if (!propertiesFileValidators.containsKey(uniqueEndpointName)) {
PropertiesFileValidation pfv = (PropertiesFileValidation) vbt;
PropertiesFileCredentialValidator pfcv = new PropertiesFileCredentialValidator(
uniqueEndpointName,
pfv.getPropertiesFileLocation());
propertiesFileValidators.put(uniqueEndpointName, pfcv);
}
cv = propertiesFileValidators.get(uniqueEndpointName);
}
break;
case AM:
// lazy instantiation needed to prevent no class def error
// on bc startup if am sdk jars are not in the classpath
@ -159,7 +178,15 @@ public class CredentialValidatorManager {
synchronized (scValidators) {
scValidators.remove(scv.getEndpointName());
}
} else if (cv instanceof SunRealmCredentialValidator) {
}
else if (cv instanceof PropertiesFileCredentialValidator) {
PropertiesFileCredentialValidator pfcv = (PropertiesFileCredentialValidator)cv;
synchronized (propertiesFileValidators) {
propertiesFileValidators.remove(pfcv.getEndpointName());
}
}
else if (cv instanceof SunRealmCredentialValidator) {
SunRealmCredentialValidator rv = (SunRealmCredentialValidator)cv;
synchronized (realmValidators) {
RealmRefCount rrc = realmValidators.get(rv.getRealmName());

View File

@ -0,0 +1,127 @@
/*
* BEGIN_HEADER - DO NOT EDIT
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
* If applicable add the following below this CDDL HEADER,
* with the fields enclosed by brackets "[]" replaced with
* your own identifying information: Portions Copyright
* [year] [name of copyright owner]
*/
/*
* @(#)SunAccessManagerCredentialValidator.java
*
* Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
*
* END_HEADER - DO NOT EDIT
*/
package com.sun.jbi.httpsoapbc.security.sc.impl;
import javax.security.auth.Subject;
import com.sun.jbi.httpsoapbc.security.api.CredentialValidationException;
import com.sun.jbi.httpsoapbc.security.api.CredentialValidator;
import com.sun.jbi.httpsoapbc.security.api.HTTPBasicAuthCredential;
import com.sun.jbi.httpsoapbc.security.impl.UserPrincipal;
import com.sun.jbi.internationalization.Messages;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PropertiesFileCredentialValidator implements CredentialValidator {
private static final Messages mMessages =
Messages.getMessages(PropertiesFileCredentialValidator.class);
private String mUniqueEndpointName;
private String propertiesFileLocation = "";
Properties propertyFile = null;
public PropertiesFileCredentialValidator(String uniqueEndpointName, String propertiesFileLocation) throws CredentialValidationException {
mUniqueEndpointName = uniqueEndpointName;
this.propertiesFileLocation = propertiesFileLocation;
getPasswordFromFile(propertiesFileLocation);
}
public void getPasswordFromFile(String filePath) throws CredentialValidationException {
this.propertyFile = new Properties();
if (filePath != null && filePath.length() > 0) {
InputStream input = null;
try {
input = new FileInputStream(filePath);
try {
propertyFile.load(input);
} catch (IOException ex) {
Logger.getLogger(PropertiesFileCredentialValidator.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
Logger.getLogger(PropertiesFileCredentialValidator.class.getName()).log(Level.WARNING, e.getMessage(), e);
}
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(PropertiesFileCredentialValidator.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
throw new CredentialValidationException(
mMessages.getString("HTTPBC-E01026.Failed_string_compare_authentication", new Object[]{filePath}));
}
}
}
public Subject validateCredential(String username, char[] password) throws CredentialValidationException {
String passwordFromFile = null;
if (propertiesFileLocation == null || propertiesFileLocation.length() < 1) {
throw new CredentialValidationException(
mMessages.getString("HTTPBC-E01026.Failed_string_compare_authentication",
new Object[]{username}));
}
passwordFromFile = propertyFile.getProperty(username);
String passwordFromrequest = String.valueOf(password);
if (username != null && passwordFromFile != null && passwordFromrequest.equals(passwordFromFile)) {
Subject subj = new Subject();
subj.getPrincipals().add(new UserPrincipal(username));
subj.getPrivateCredentials().add(new HTTPBasicAuthCredential(username, passwordFromFile.toCharArray()));
return subj;
} else {
throw new CredentialValidationException(
mMessages.getString("HTTPBC-E01026.Failed_string_compare_authentication",
new Object[]{username}));
}
}
public String getEndpointName() {
return mUniqueEndpointName;
}
public String getPropertiesFileLocation() {
return propertiesFileLocation;
}
}