KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > socks > server > UserPasswordAuthenticator


1 package socks.server;
2
3 import socks.ProxyMessage;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.net.Socket JavaDoc;
8
9 /**
10   This class implements SOCKS5 User/Password authentication scheme as
11   defined in rfc1929,the server side of it.
12 */

13 public class UserPasswordAuthenticator extends ServerAuthenticatorNone{
14
15    static final int METHOD_ID = 2;
16
17    UserValidation validator;
18
19    /**
20     Construct a new UserPasswordAuthentication object, with given
21     UserVlaidation scheme.
22
23     @param v UserValidation to use for validating users.
24    */

25    public UserPasswordAuthenticator(UserValidation validator){
26       this.validator = validator;
27    }
28
29    public ServerAuthenticator startSession(Socket JavaDoc s) throws IOException JavaDoc{
30      InputStream JavaDoc in = s.getInputStream();
31      OutputStream JavaDoc out = s.getOutputStream();
32
33      if(in.read() != 5) return null; //Drop non version 5 messages.
34

35      if(!selectSocks5Authentication(in,out,METHOD_ID))
36        return null;
37      if(!doUserPasswordAuthentication(s,in,out))
38        return null;
39
40      return new ServerAuthenticatorNone(in,out);
41    }
42
43
44 //Private Methods
45
//////////////////
46

47    private boolean doUserPasswordAuthentication(Socket JavaDoc s,
48                                                 InputStream JavaDoc in,
49                                                 OutputStream JavaDoc out)
50                                                 throws IOException JavaDoc{
51      int version = in.read();
52      if(version != 1) return false;
53      int ulen = in.read();
54      if(ulen < 0) return false;
55      byte[] user = new byte[ulen];
56      in.read(user);
57      int plen = in.read();
58      if(plen < 0) return false;
59      byte[] password = new byte[plen];
60      in.read(password);
61
62      if(validator.isUserValid(new String JavaDoc(user), new String JavaDoc(password),s)){
63        //System.out.println("user valid");
64
out.write(new byte[]{1,0});
65      }else{
66        //System.out.println("user invalid");
67
out.write(new byte[]{1,1});
68        return false;
69      }
70
71      return true;
72    }
73 }
74
Popular Tags