Skip to content

Commit

Permalink
ipAuth (this does not work yet)
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@937 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
allo committed Oct 14, 2005
1 parent 95abdeb commit 4320425
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
61 changes: 60 additions & 1 deletion source/de/anomic/data/userDB.java
Expand Up @@ -58,6 +58,8 @@
import de.anomic.kelondro.kelondroException;
import de.anomic.kelondro.kelondroMap;
import de.anomic.server.logging.serverLog;
import de.anomic.server.serverCodings;
import de.anomic.plasma.plasmaSwitchboard;

public final class userDB {

Expand All @@ -67,6 +69,8 @@ public final class userDB {
kelondroMap userTable;
private final File userTableFile;
private final int bufferkb;
private final serverCodings codings = new serverCodings(true);
private HashMap ipUsers = new HashMap();

public userDB(File userTableFile, int bufferkb) throws IOException {
this.userTableFile = userTableFile;
Expand Down Expand Up @@ -146,6 +150,61 @@ public String addEntry(Entry entry) {
return null;
}
}
/*
* use a ProxyAuth String to authenticate a user
* @param auth a base64 Encoded String, which contains "username:pw".
*/
public Entry proxyAuth(String auth) {
Entry entry=null;
auth=auth.trim().substring(6);
try{
auth=codings.decodeBase64String(auth);
}catch(StringIndexOutOfBoundsException e){} //no valid Base64
String[] tmp=auth.split(":");
if(tmp.length == 2){
entry=this.getEntry(tmp[0]);
if( entry != null && entry.getMD5EncodedUserPwd().equals(serverCodings.encodeMD5Hex(auth)) ){
return entry;
}
}
return null;
}
/*
* use a ProxyAuth String to authenticate a user and save the ip/username for ipAuth
* @param auth a base64 Encoded String, which contains "username:pw".
* @param ip an ip.
*/
public Entry proxyAuth(String auth, String ip){
Entry entry=proxyAuth(auth);
if(entry == null){
return null;
}else{
this.ipUsers.put(ip, entry.getUserName());
System.out.println(ip+", "+entry.getUserName());
return entry;
}
}
/*
* authenticate a user by ip, if he had used proxyAuth in the last 10 Minutes
* @param ip the IP of the User
*/
public Entry ipAuth(String ip) {
System.out.println(ip);
if(this.ipUsers.containsKey(ip)){
String user=(String)this.ipUsers.get(ip);
System.out.println(user);
Entry entry=this.getEntry(user);
Long entryTimestamp=entry.getLastAccess();
if(entryTimestamp == null || (System.currentTimeMillis()-entryTimestamp.longValue()) > (1000*60*10) ){ //no timestamp or older than 10 Minutes
System.out.println("too old");
System.out.println(System.currentTimeMillis()-entryTimestamp.longValue());
return null;
}
return entry; //All OK
}else{ //not known
return null;
}
}

public class Entry {
public static final String MD5ENCODED_USERPWD_STRING = "MD5_user:pwd";
Expand Down Expand Up @@ -262,7 +321,7 @@ public long updateLastAccess(long timeStamp, boolean incrementTimeUsed) {
long newTimeUsed = oldTimeUsed;

if (incrementTimeUsed) {
if ((lastAccess == null)||((lastAccess != null)&&(timeStamp-lastAccess.longValue()>=1000*60))) {
if ((lastAccess == null)||((lastAccess != null)&&(timeStamp-lastAccess.longValue()>=1000*60))) { //1 minute
//this.mem.put(TIME_USED,Long.toString(newTimeUsed = ++oldTimeUsed));
newTimeUsed = ++oldTimeUsed;
if(lastAccess != null){
Expand Down
32 changes: 14 additions & 18 deletions source/de/anomic/http/httpd.java
Expand Up @@ -318,24 +318,20 @@ private boolean handleProxyAuthentication(httpHeader header) throws IOException

if (this.use_proxyAccounts) {
String auth = (String) header.get(httpHeader.PROXY_AUTHORIZATION,"xxxxxx");
auth=auth.trim().substring(6);
try{
auth=codings.decodeBase64String(auth);
}catch(StringIndexOutOfBoundsException e){} //no valid Base64
String[] tmp=auth.split(":");
if(tmp.length == 2){
userDB.Entry entry=switchboard.userDB.getEntry(tmp[0]);
if( entry != null && entry.getMD5EncodedUserPwd().equals(serverCodings.encodeMD5Hex(auth)) ){
if(entry.canSurf()){
return true;
} else {
HashMap tp=new HashMap();
tp.put("limit", "0");//time per day
tp.put("limit_timelimit", entry.getTimeLimit());
sendRespondError(this.prop, this.session.out, 403, "Internet-Timelimit reached", new File("proxymsg/proxylimits.inc"), tp, null);
return false;
}
}
userDB.Entry entry=switchboard.userDB.ipAuth(this.clientIP);
if(entry == null){
entry=switchboard.userDB.proxyAuth(auth, this.clientIP);
}
if(entry != null){
if(entry.canSurf()){
return true;
} else {
HashMap tp=new HashMap();
tp.put("limit", "0");//time per day
tp.put("limit_timelimit", entry.getTimeLimit());
sendRespondError(this.prop, this.session.out, 403, "Internet-Timelimit reached", new File("proxymsg/proxylimits.inc"), tp, null);
return false;
}
}
// ask for authenticate
this.session.out.write((httpVersion + " 407 Proxy Authentication Required" + serverCore.crlfString +
Expand Down

0 comments on commit 4320425

Please sign in to comment.