Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
added support for URIs with user info but no password for the ProxySe…
Browse files Browse the repository at this point in the history
…rvlet (#4051)

Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored and maggu2810 committed Aug 14, 2017
1 parent 705a18c commit c813f25
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
Expand Up @@ -10,6 +10,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ClassPath: .
Import-Package: org.eclipse.jdt.annotation;resolution:=optional,
org.hamcrest;core=split,
org.mockito
org.mockito,
org.objenesis
Require-Bundle: org.junit,
org.mockito,org.hamcrest
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.ui.internal.proxy;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import java.net.URI;
import java.net.URISyntaxException;

import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.StringUtil;
import org.junit.Before;
import org.junit.Test;

/**
* Unit tests for the {@link ProxyServletService} class.
*
* @author Kai Kreuzer - Initial contribution
*
*/
public class ProxyServletServiceTest {

static private ProxyServletService service;

@Before
public void setUp() {
service = new ProxyServletService();
}

@Test
public void testMaybeAppendAuthHeaderWithFullCredentials() throws URISyntaxException {
Request request = mock(Request.class);
URI uri = new URI("http://testuser:testpassword@127.0.0.1:8080/content");
service.maybeAppendAuthHeader(uri, request);
verify(request).header(HttpHeader.AUTHORIZATION,
"Basic " + B64Code.encode("testuser:testpassword", StringUtil.__ISO_8859_1));
}

@Test
public void testMaybeAppendAuthHeaderWithoutPassword() throws URISyntaxException {
Request request = mock(Request.class);
URI uri = new URI("http://testuser@127.0.0.1:8080/content");
service.maybeAppendAuthHeader(uri, request);
verify(request).header(HttpHeader.AUTHORIZATION,
"Basic " + B64Code.encode("testuser:", StringUtil.__ISO_8859_1));
}

@Test
public void testMaybeAppendAuthHeaderWithoutCredentials() throws URISyntaxException {
Request request = mock(Request.class);
URI uri = new URI("http://127.0.0.1:8080/content");
service.maybeAppendAuthHeader(uri, request);
verify(request, never()).header(any(HttpHeader.class), anyString());
}
}
Expand Up @@ -278,7 +278,7 @@ URI uriFromRequest(HttpServletRequest request) {
}

/**
* If the URI contains user info in the form <code>user:pass</code>, attempt to preempt the server
* If the URI contains user info in the form <code>user[:pass]@</code>, attempt to preempt the server
* returning a 401 by providing Basic Authentication support in the initial request to the server.
*
* @param uri the URI which may contain user info
Expand All @@ -288,11 +288,12 @@ void maybeAppendAuthHeader(URI uri, Request request) {
if (uri != null && uri.getUserInfo() != null) {
String[] userInfo = uri.getUserInfo().split(":");

if (userInfo.length >= 2) {
if (userInfo.length >= 1) {
String user = userInfo[0];
String password = userInfo[1];
String password = userInfo.length >= 2 ? userInfo[1] : null;
String authString = password != null ? user + ":" + password : user + ":";

String basicAuthentication = "Basic " + B64Code.encode(user + ":" + password, StringUtil.__ISO_8859_1);
String basicAuthentication = "Basic " + B64Code.encode(authString, StringUtil.__ISO_8859_1);
request.header(HttpHeader.AUTHORIZATION, basicAuthentication);
}
}
Expand Down

0 comments on commit c813f25

Please sign in to comment.