ipAuth (this does not work yet)

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@937 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
allo 20 years ago
parent 95abdeb685
commit 4320425a17

@ -58,6 +58,8 @@ import de.anomic.kelondro.kelondroDyn;
import de.anomic.kelondro.kelondroException; import de.anomic.kelondro.kelondroException;
import de.anomic.kelondro.kelondroMap; import de.anomic.kelondro.kelondroMap;
import de.anomic.server.logging.serverLog; import de.anomic.server.logging.serverLog;
import de.anomic.server.serverCodings;
import de.anomic.plasma.plasmaSwitchboard;
public final class userDB { public final class userDB {
@ -67,6 +69,8 @@ public final class userDB {
kelondroMap userTable; kelondroMap userTable;
private final File userTableFile; private final File userTableFile;
private final int bufferkb; private final int bufferkb;
private final serverCodings codings = new serverCodings(true);
private HashMap ipUsers = new HashMap();
public userDB(File userTableFile, int bufferkb) throws IOException { public userDB(File userTableFile, int bufferkb) throws IOException {
this.userTableFile = userTableFile; this.userTableFile = userTableFile;
@ -146,6 +150,61 @@ public final class userDB {
return null; 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 class Entry {
public static final String MD5ENCODED_USERPWD_STRING = "MD5_user:pwd"; public static final String MD5ENCODED_USERPWD_STRING = "MD5_user:pwd";
@ -262,7 +321,7 @@ public final class userDB {
long newTimeUsed = oldTimeUsed; long newTimeUsed = oldTimeUsed;
if (incrementTimeUsed) { 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)); //this.mem.put(TIME_USED,Long.toString(newTimeUsed = ++oldTimeUsed));
newTimeUsed = ++oldTimeUsed; newTimeUsed = ++oldTimeUsed;
if(lastAccess != null){ if(lastAccess != null){

@ -318,14 +318,11 @@ public final class httpd implements serverHandler {
if (this.use_proxyAccounts) { if (this.use_proxyAccounts) {
String auth = (String) header.get(httpHeader.PROXY_AUTHORIZATION,"xxxxxx"); String auth = (String) header.get(httpHeader.PROXY_AUTHORIZATION,"xxxxxx");
auth=auth.trim().substring(6); userDB.Entry entry=switchboard.userDB.ipAuth(this.clientIP);
try{ if(entry == null){
auth=codings.decodeBase64String(auth); entry=switchboard.userDB.proxyAuth(auth, this.clientIP);
}catch(StringIndexOutOfBoundsException e){} //no valid Base64 }
String[] tmp=auth.split(":"); if(entry != null){
if(tmp.length == 2){
userDB.Entry entry=switchboard.userDB.getEntry(tmp[0]);
if( entry != null && entry.getMD5EncodedUserPwd().equals(serverCodings.encodeMD5Hex(auth)) ){
if(entry.canSurf()){ if(entry.canSurf()){
return true; return true;
} else { } else {
@ -336,7 +333,6 @@ public final class httpd implements serverHandler {
return false; return false;
} }
} }
}
// ask for authenticate // ask for authenticate
this.session.out.write((httpVersion + " 407 Proxy Authentication Required" + serverCore.crlfString + this.session.out.write((httpVersion + " 407 Proxy Authentication Required" + serverCore.crlfString +
httpHeader.PROXY_AUTHENTICATE + ": Basic realm=\"log-in\"" + serverCore.crlfString).getBytes()); httpHeader.PROXY_AUTHENTICATE + ": Basic realm=\"log-in\"" + serverCore.crlfString).getBytes());

Loading…
Cancel
Save