Improve design code.

This commit is contained in:
Christian P. MOMON 2013-08-26 16:50:44 +02:00
parent 573d8d53fa
commit 535288c26c
2 changed files with 72 additions and 17 deletions

View file

@ -10,7 +10,7 @@
<classpathentry kind="lib" path="lib/slf4j-api-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/slf4j-log4j12-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.0.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.8.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.8.jar" sourcepath="lib/commons-codec-1.8-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
<attributes>

View file

@ -7,6 +7,7 @@ import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.util.StringList;
/**
*
@ -59,6 +60,35 @@ public class SimpleSecurityAgent
return (result);
}
/**
* This method builds a key from keys and a secret key.
*/
public String computeAuth(final String... keys)
{
String result;
if (keys == null)
{
result = null;
}
else
{
// Add a secret key to the key list.
String[] targetKeys = new String[keys.length + 1];
for (int keyIndex = 0; keyIndex < keys.length; keyIndex++)
{
targetKeys[keyIndex] = keys[keyIndex];
}
targetKeys[keys.length] = this.secretKey;
//
result = md5sum(targetKeys);
}
//
return (result);
}
/**
* Check authentication and refresh it (reset countdown).
*/
@ -75,7 +105,7 @@ public class SimpleSecurityAgent
{
result = false;
}
else if (auth.equals(computeAuth(accountId, userId, request.getRemoteAddr(), this.secretKey)))
else if (auth.equals(computeAuth(accountId, userId, request.getRemoteAddr())))
{
result = true;
@ -91,6 +121,29 @@ public class SimpleSecurityAgent
return (result);
}
/**
*
* @param source
* @return
*/
public String md5sumWithSecret(final String source)
{
String result;
if (source == null)
{
result = null;
}
else
{
String key = source + this.secretKey;
result = md5sum(key);
}
//
return result;
}
/**
*
*/
@ -108,7 +161,7 @@ public class SimpleSecurityAgent
{
// Refresh cookie.
int duration = 60 * 60;
String auth = computeAuth(String.valueOf(accountId), userId, request.getRemoteAddr(), this.secretKey);
String auth = computeAuth(String.valueOf(accountId), userId, request.getRemoteAddr());
response.addCookie(CookieHelper.buildCookie(this.authLabel, auth, duration));
response.addCookie(CookieHelper.buildCookie(this.accountIdLabel, accountId, duration));
@ -133,24 +186,26 @@ public class SimpleSecurityAgent
/**
*
*/
static public String computeAuth(final String key1, final String key2, final String key3, final String key4)
static public String md5sum(final String... keys)
{
String result;
result = md5sum(key1 + key2 + key3 + key4);
if (keys == null)
{
result = null;
}
else
{
//
StringList targetKey = new StringList();
for (String key : keys)
{
targetKey.append(key);
}
//
return (result);
}
/**
*
*/
static String md5sum(final String source)
{
String result;
result = DigestUtils.md5Hex(source);
//
result = DigestUtils.md5Hex(targetKey.toString());
}
//
return (result);