1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55:
56:
59: public class PlainServer
60: extends ServerMechanism
61: implements SaslServer
62: {
63: public PlainServer()
64: {
65: super(Registry.SASL_PLAIN_MECHANISM);
66: }
67:
68: protected void initMechanism() throws SaslException
69: {
70: }
71:
72: protected void resetMechanism() throws SaslException
73: {
74: }
75:
76: public byte[] evaluateResponse(final byte[] response) throws SaslException
77: {
78: if (response == null)
79: return null;
80: try
81: {
82: final String nullStr = new String("\0");
83: final StringTokenizer strtok = new StringTokenizer(new String(response),
84: nullStr, true);
85: authorizationID = strtok.nextToken();
86: if (! authorizationID.equals(nullStr))
87: strtok.nextToken();
88: else
89: authorizationID = null;
90: final String id = strtok.nextToken();
91: if (id.equals(nullStr))
92: throw new SaslException("No identity given");
93: if (authorizationID == null)
94: authorizationID = id;
95: if ((! authorizationID.equals(nullStr)) && (! authorizationID.equals(id)))
96: throw new SaslException("Delegation not supported");
97: strtok.nextToken();
98: final byte[] pwd;
99: try
100: {
101: pwd = strtok.nextToken().getBytes("UTF-8");
102: }
103: catch (UnsupportedEncodingException x)
104: {
105: throw new SaslException("evaluateResponse()", x);
106: }
107: if (pwd == null)
108: throw new SaslException("No password given");
109: final byte[] password;
110: try
111: {
112: password = new String(lookupPassword(id)).getBytes("UTF-8");
113: }
114: catch (UnsupportedEncodingException x)
115: {
116: throw new SaslException("evaluateResponse()", x);
117: }
118: if (! Arrays.equals(pwd, password))
119: throw new SaslException("Password incorrect");
120: this.complete = true;
121: return null;
122: }
123: catch (NoSuchElementException x)
124: {
125: throw new SaslException("evaluateResponse()", x);
126: }
127: }
128:
129: protected String getNegotiatedQOP()
130: {
131: return Registry.QOP_AUTH;
132: }
133:
134: private char[] lookupPassword(final String userName) throws SaslException
135: {
136: try
137: {
138: if (! authenticator.contains(userName))
139: throw new NoSuchUserException(userName);
140: final Map userID = new HashMap();
141: userID.put(Registry.SASL_USERNAME, userName);
142: final Map credentials = authenticator.lookup(userID);
143: final String password = (String) credentials.get(Registry.SASL_PASSWORD);
144: if (password == null)
145: throw new SaslException("lookupPassword()", new InternalError());
146: return password.toCharArray();
147: }
148: catch (IOException x)
149: {
150: if (x instanceof SaslException)
151: throw (SaslException) x;
152: throw new SaslException("lookupPassword()", x);
153: }
154: }
155: }