Introduce proper bureau authenticator class

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5192 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Jamie Doornbos
2008-06-23 20:09:30 +00:00
parent 9e06f34557
commit 52637e3fff
2 changed files with 60 additions and 9 deletions
@@ -0,0 +1,52 @@
package com.threerings.bureau.server;
import com.threerings.bureau.data.BureauCredentials;
import com.threerings.presents.data.AuthCodes;
import com.threerings.presents.net.AuthRequest;
import com.threerings.presents.net.AuthResponse;
import com.threerings.presents.net.AuthResponseData;
import com.threerings.presents.server.ChainedAuthenticator;
import com.threerings.presents.server.net.AuthingConnection;
import static com.threerings.bureau.Log.log;
/**
* Authenticates bureaus only.
*/
public class BureauAuthenticator extends ChainedAuthenticator
{
/**
* Creates a new bureau authenticator.
*/
public BureauAuthenticator (BureauRegistry registry)
{
_registry = registry;
}
@Override // from abstract ChainedAuthenticator
protected boolean shouldHandleConnection (AuthingConnection conn)
{
return (conn.getAuthRequest().getCredentials() instanceof BureauCredentials);
}
@Override // from Authenticator
protected void processAuthentication (
AuthingConnection conn,
AuthResponse rsp)
{
AuthRequest req = conn.getAuthRequest();
BureauCredentials creds = (BureauCredentials)req.getCredentials();
String problem = _registry.checkToken(creds);
if (problem == null) {
rsp.getData().code = AuthResponseData.SUCCESS;
} else {
log.warning("Received invalid bureau auth request [creds=" +
creds + "], problem: " + problem);
rsp.getData().code = AuthCodes.SERVER_ERROR;
}
}
protected BureauRegistry _registry;
}
@@ -125,26 +125,25 @@ public class BureauRegistry
/** /**
* Check the credentials to make sure this is one of our bureaus. * Check the credentials to make sure this is one of our bureaus.
* @return null if all's well, otherwise a string describing the authentication failure
*/ */
public void authenticate (BureauCredentials creds) public String checkToken (BureauCredentials creds)
throws AuthenticationException
{ {
Bureau bureau = _bureaus.get(creds.bureauId); Bureau bureau = _bureaus.get(creds.bureauId);
if (bureau == null) { if (bureau == null) {
throw new AuthenticationException( return "Bureau " + creds.bureauId + " not found";
"Bureau " + creds.bureauId + " not found");
} }
if (bureau.clientObj != null) { if (bureau.clientObj != null) {
throw new AuthenticationException( return "Bureau " + creds.bureauId + " already logged in";
"Bureau " + creds.bureauId + " already logged in");
} }
if (!bureau.token.equals(creds.sessionToken)) { if (!bureau.token.equals(creds.sessionToken)) {
throw new AuthenticationException( return "Bureau " + creds.bureauId +
"Bureau " + creds.bureauId + " does not match credentials token";
" does not match credentials token");
} }
return null;
} }
/** /**