Refactored digest auth to have a more flexible api

This commit is contained in:
Ziver Koc 2016-12-05 16:59:36 +01:00
parent 9f95258734
commit f9949ac9ef
2 changed files with 17 additions and 18 deletions

View file

@ -15,13 +15,12 @@ import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* A abstract page that requires HTTP Digest authentication * A class that encapsulates a HttpPage around a HTTP Digest authentication requirement.
* to access the subclass HttpPage.
* *
* @see <a href="https://tools.ietf.org/html/rfc2069">rfc2069</a> * @see <a href="https://tools.ietf.org/html/rfc2069">rfc2069</a>
* @author Ziver * @author Ziver
*/ */
public abstract class HttpDigestAuthPage implements HttpPage{ public class HttpDigestAuthPage implements HttpPage{
private static final Logger logger = LogUtil.getLogger(); private static final Logger logger = LogUtil.getLogger();
private static final String DEFAULT_REALM = "Login"; private static final String DEFAULT_REALM = "Login";
@ -41,8 +40,17 @@ public abstract class HttpDigestAuthPage implements HttpPage{
private String realm = DEFAULT_REALM; private String realm = DEFAULT_REALM;
private HashMap<String,String> userMap = new HashMap<>(); private HashMap<String,String> userMap = new HashMap<>();
private SecureRandom secRandom = new SecureRandom(); private SecureRandom secRandom = new SecureRandom();
private HttpPage targetPage;
/**
*
* @param page
*/
public HttpDigestAuthPage(HttpPage page){
targetPage = page;
}
public void setRealm(String realm){ public void setRealm(String realm){
this.realm = realm; this.realm = realm;
@ -84,7 +92,7 @@ public abstract class HttpDigestAuthPage implements HttpPage{
authMap.get(AUTH_RESPONSE))) { authMap.get(AUTH_RESPONSE))) {
// Safe area, user authenticated // Safe area, user authenticated
logger.fine("User '"+authMap.get(AUTH_USERNAME)+"' has been authenticated for realm '"+realm+"'"); logger.fine("User '"+authMap.get(AUTH_USERNAME)+"' has been authenticated for realm '"+realm+"'");
authRespond(out, headers, session, cookie, request); targetPage.respond(out, headers, session, cookie, request);
} }
else{ else{
out.setStatusCode(403); out.setStatusCode(403);
@ -154,10 +162,4 @@ public abstract class HttpDigestAuthPage implements HttpPage{
} }
public abstract void authRespond(HttpPrintStream out,
HttpHeader headers,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException;
} }

View file

@ -7,10 +7,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import zutil.Hasher; import zutil.Hasher;
import zutil.io.IOUtil; import zutil.io.IOUtil;
import zutil.net.http.HttpHeader; import zutil.net.http.*;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPrintStream;
import zutil.net.http.HttpTestUtil;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
@ -26,11 +23,11 @@ public class HttpDigestAuthPageTest {
private static final String PAGE_USERNAME = "username"; private static final String PAGE_USERNAME = "username";
private static final String PAGE_PASSWORD = "password"; private static final String PAGE_PASSWORD = "password";
private HttpDigestTestPage page; private HttpDigestAuthPage page;
@Before @Before
public void init(){ public void init(){
page = new HttpDigestTestPage(); page = new HttpDigestAuthPage(new TestPage());
page.addUser(PAGE_USERNAME, PAGE_PASSWORD.toCharArray()); page.addUser(PAGE_USERNAME, PAGE_PASSWORD.toCharArray());
} }
@ -113,9 +110,9 @@ public class HttpDigestAuthPageTest {
return authHeaders; return authHeaders;
} }
private static class HttpDigestTestPage extends HttpDigestAuthPage{ private static class TestPage implements HttpPage {
@Override @Override
public void authRespond(HttpPrintStream out, public void respond(HttpPrintStream out,
HttpHeader headers, HttpHeader headers,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,