Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject nonsense combination of MTLS and non-https #131

Merged
merged 1 commit into from
Jun 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public class HttpServiceConnection implements IServiceConnection, IServiceConnec
* @param sslStrategy the SSL strategy
* @param request the request
* @param service the service
* @param requiredAuthType
* @param authType the authorization type
* @param requiredAuthType the authorization type
* @param handler the result handler
* @throws ConnectorException when unable to connect
*/
Expand Down Expand Up @@ -130,6 +129,27 @@ private void connect() throws ConnectorException {
connection = (HttpURLConnection) url.openConnection();

boolean isSsl = connection instanceof HttpsURLConnection;

if(requiredAuthType == RequiredAuthType.MTLS && !isSsl) {
throw new ConnectorException("Mutually authenticating TLS requested, but insecure endpoint protocol was indicated."); //$NON-NLS-1$
}

if (requiredAuthType == RequiredAuthType.BASIC) {
BasicAuthOptions options = new BasicAuthOptions(service.getEndpointProperties());
if (options.getUsername() != null && options.getPassword() != null) {
if (options.isRequireSSL() && !isSsl) {
throw new ConnectorException("Endpoint security requested (BASIC auth) but endpoint is not secure (SSL)."); //$NON-NLS-1$
}

String up = options.getUsername() + ':' + options.getPassword();
StringBuilder builder = new StringBuilder();
builder.append("BASIC "); //$NON-NLS-1$
builder.append(Base64.encodeBase64String(up.getBytes()));
connection.setRequestProperty("Authorization", builder.toString()); //$NON-NLS-1$
}
}


if (isSsl) {
HttpsURLConnection https = (HttpsURLConnection) connection;
SSLSocketFactory socketFactory = sslStrategy.getSocketFactory();
Expand Down Expand Up @@ -157,23 +177,6 @@ private void connect() throws ConnectorException {
}
}

// If basic authentication is enabled (endpoint security) then add
// the appropriate authorization header
if (this.requiredAuthType == RequiredAuthType.BASIC) {
BasicAuthOptions options = new BasicAuthOptions(service.getEndpointProperties());
if (options.getUsername() != null && options.getPassword() != null) {
if (options.isRequireSSL() && !isSsl) {
throw new ConnectorException("Endpoint security requested (BASIC auth) but endpoint is not secure (SSL)."); //$NON-NLS-1$
}

String up = options.getUsername() + ':' + options.getPassword();
StringBuilder builder = new StringBuilder();
builder.append("BASIC "); //$NON-NLS-1$
builder.append(Base64.encodeBase64String(up.getBytes()));
connection.setRequestProperty("Authorization", builder.toString()); //$NON-NLS-1$
}
}

connection.connect();
connected = true;
} catch (IOException e) {
Expand Down