use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.
the class GNSHttpServer method processQuery.
/*
* Process queries for the http service. Converts the URI of e the HTTP query into
* the JSON Object format that is used by the CommandModeule class, then finds
* executes the matching command.
*
* @throws InternalRequestException
*/
private CommandResponse processQuery(String host, String commandName, String queryString, boolean secureServer) throws InternalRequestException {
// the signature, and the message signed.
try {
// Note that the commandName is not part of the queryString string here so
// it doesn't end up in the jsonCommand. Also see below where we put the
// command integer into the jsonCommand.
JSONObject jsonCommand = Util.parseURIQueryStringIntoJSONObject(queryString);
// If the signature exists it is Base64 encoded so decode it now.
if (jsonCommand.has(GNSProtocol.SIGNATURE.toString())) {
jsonCommand.put(GNSProtocol.SIGNATURE.toString(), new String(Base64.decode(jsonCommand.getString(GNSProtocol.SIGNATURE.toString())), GNSProtocol.CHARSET.toString()));
}
// getCommandForHttp allows for "dump" as well as "Dump"
CommandType commandType = CommandType.getCommandForHttp(commandName);
if (commandType == null) {
return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Sorry, don't understand " + commandName + QUERYPREFIX + queryString);
}
//Only allow mutual auth commands if we're on a secure (HTTPS) server
if (commandType.isMutualAuth() && !secureServer) {
return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Not authorized to execute " + commandName + QUERYPREFIX + queryString);
}
// The client currently just uses the command name (which is not part of the
// query string above) so we need to stuff
// in the Command integer for the signature check and execution.
jsonCommand.put(GNSProtocol.COMMAND_INT.toString(), commandType.getInt());
// Optionally does some sanity checking on the message if that was enabled at the client.
// This makes necessary changes to the jsonCommand so don't remove this call
// unless you know what you're doing and also change the code in the HTTP client.
sanityCheckMessage(jsonCommand);
// is true (or there was a problem).
if (client == null || commandType.isLocallyHandled()) {
// EXECUTE IT LOCALLY
AbstractCommand command;
try {
command = commandModule.lookupCommand(commandType);
// Do some work to get the signature and message into the command for
// signature checking that happens later on.
// This only happens for local execution because remote handling (in the
// other side of the if) already does this.
processSignature(jsonCommand);
if (command != null) {
return CommandHandler.executeCommand(command, new CommandPacket((long) (Math.random() * Long.MAX_VALUE), jsonCommand, false), requestHandler);
}
LOGGER.log(Level.FINE, "lookupCommand returned null for {0}", commandName);
} catch (IllegalArgumentException e) {
LOGGER.log(Level.FINE, "lookupCommand failed for {0}", commandName);
}
return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Sorry, don't understand " + commandName + QUERYPREFIX + queryString);
} else {
// Send the command remotely using a client
try {
LOGGER.log(Level.FINE, "Sending command out to a remote server: {0}", jsonCommand);
CommandPacket commandResponsePacket = getResponseUsingGNSClient(client, jsonCommand);
return new CommandResponse(ResponseCode.NO_ERROR, // There is similar code to this other places.
specialCaseSingleFieldRead(commandResponsePacket.getResultString(), commandType, jsonCommand));
} catch (IOException | ClientException e) {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.toString());
// } catch (ClientException e) {
// return new CommandResponse(ResponseCode.GNSProtocol.UNSPECIFIED_ERROR.toString(),
// GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString()
// + " Sorry, don't understand " + commandName + QUERYPREFIX + queryString);
}
}
} catch (JSONException | UnsupportedEncodingException e) {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.toString());
}
}
use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.
the class AddGuid method execute.
@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException {
JSONObject json = commandPacket.getCommand();
String name = json.getString(GNSProtocol.NAME.toString());
String accountGuid = json.getString(GNSProtocol.GUID.toString());
String publicKey = json.optString(GNSProtocol.PUBLIC_KEY.toString(), null);
String signature = json.getString(GNSProtocol.SIGNATURE.toString());
String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
String newGuid;
if (publicKey != null) {
newGuid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
} else {
// add a fake public key
publicKey = GuidInfo.KEYLESS_PREFIX + name;
newGuid = SharedGuidUtils.createGuidStringFromPublicKey(publicKey.getBytes());
}
GuidInfo accountGuidInfo;
if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, accountGuid, handler)) == null) {
return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + accountGuid);
}
if (NSAccessSupport.verifySignature(accountGuidInfo.getPublicKey(), signature, message)) {
AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, accountGuid, handler);
if (accountInfo == null) {
return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + accountGuid);
}
if (!accountInfo.isVerified()) {
return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
} else if (accountInfo.getGuids().size() > Config.getGlobalInt(GNSConfig.GNSC.ACCOUNT_GUID_MAX_SUBGUIDS)) {
return new CommandResponse(ResponseCode.TOO_MANY_GUIDS_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.TOO_MANY_GUIDS.toString());
} else {
CommandResponse result = AccountAccess.addGuid(header, commandPacket, accountInfo, accountGuidInfo, name, newGuid, publicKey, handler);
if (result.getExceptionOrErrorCode().isOKResult()) {
// Everything is hunkey dorey so return the new guid
return new CommandResponse(ResponseCode.NO_ERROR, newGuid);
} else {
// Otherwise return the error response
return result;
}
}
} else {
// Signature verification failed
return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
}
//}
}
use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.
the class AddMultipleGuids method execute.
@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException {
JSONObject json = commandPacket.getCommand();
String guid = json.getString(GNSProtocol.GUID.toString());
String guidCntString = json.optString(GNSProtocol.GUIDCNT.toString());
JSONArray names = json.optJSONArray(GNSProtocol.NAMES.toString());
JSONArray publicKeys = json.optJSONArray(GNSProtocol.PUBLIC_KEYS.toString());
String signature = json.getString(GNSProtocol.SIGNATURE.toString());
String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
GuidInfo accountGuidInfo;
if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, guid, handler)) == null) {
return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guid);
}
if (NSAccessSupport.verifySignature(accountGuidInfo.getPublicKey(), signature, message)) {
AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, guid, handler);
if (accountInfo == null) {
return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + guid);
}
if (!accountInfo.isVerified()) {
return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
} else if (accountInfo.getGuids().size() > Config.getGlobalInt(GNSConfig.GNSC.ACCOUNT_GUID_MAX_SUBGUIDS)) {
return new CommandResponse(ResponseCode.TOO_MANY_GUIDS_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.TOO_MANY_GUIDS.toString());
} else if (names != null && publicKeys != null) {
GNSConfig.getLogger().log(Level.INFO, "ADD SLOW{0} / {1}", new Object[] { names, publicKeys });
return AccountAccess.addMultipleGuids(header, commandPacket, JSONUtils.JSONArrayToArrayListString(names), JSONUtils.JSONArrayToArrayListString(publicKeys), accountInfo, accountGuidInfo, handler);
} else if (names != null) {
//GNS.getLogger().info("ADD FASTER" + names + " / " + publicKeys);
return AccountAccess.addMultipleGuidsFaster(header, commandPacket, JSONUtils.JSONArrayToArrayListString(names), accountInfo, accountGuidInfo, handler);
} else if (guidCntString != null) {
//GNS.getLogger().info("ADD RANDOM" + names + " / " + publicKeys);
int guidCnt = Integer.parseInt(guidCntString);
return AccountAccess.addMultipleGuidsFasterAllRandom(header, commandPacket, guidCnt, accountInfo, accountGuidInfo, handler);
} else {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " bad arguments: need " + GNSProtocol.NAMES.toString() + " or " + GNSProtocol.NAMES.toString() + " and " + GNSProtocol.PUBLIC_KEYS.toString() + " or " + GNSProtocol.GUIDCNT.toString());
}
} else {
return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
}
//}
}
use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.
the class RemoveGuid method execute.
@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException, InternalRequestException {
JSONObject json = commandPacket.getCommand();
String guidToRemove = json.getString(GNSProtocol.GUID.toString());
String accountGuid = json.optString(GNSProtocol.ACCOUNT_GUID.toString(), null);
String signature = json.getString(GNSProtocol.SIGNATURE.toString());
String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
GuidInfo accountGuidInfo = null;
GuidInfo guidInfoToRemove;
if ((guidInfoToRemove = AccountAccess.lookupGuidInfoLocally(header, guidToRemove, handler)) == null) {
// Removing a non-existant guid is no longer an error.
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
//return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guidToRemove);
}
if (accountGuid != null) {
if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, accountGuid, handler)) == null) {
return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + accountGuid);
}
}
if (NSAccessSupport.verifySignature(accountGuidInfo != null ? accountGuidInfo.getPublicKey() : guidInfoToRemove.getPublicKey(), signature, message)) {
AccountInfo accountInfo = null;
if (accountGuid != null) {
accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, accountGuid, handler);
if (accountInfo == null) {
return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + accountGuid);
}
}
return AccountAccess.removeGuid(header, commandPacket, guidInfoToRemove, accountInfo, handler);
} else {
return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
}
}
use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.
the class RetrieveAliases method execute.
@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException {
JSONObject json = commandPacket.getCommand();
String guid = json.getString(GNSProtocol.GUID.toString());
String signature = json.getString(GNSProtocol.SIGNATURE.toString());
String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
GuidInfo guidInfo;
if ((guidInfo = AccountAccess.lookupGuidInfoLocally(header, guid, handler)) == null) {
return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guid);
}
if (NSAccessSupport.verifySignature(guidInfo.getPublicKey(), signature, message)) {
AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidLocally(header, guid, handler);
if (accountInfo == null) {
return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + guid);
} else if (!accountInfo.isVerified()) {
return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
}
List<String> aliases = accountInfo.getAliases();
return new CommandResponse(ResponseCode.NO_ERROR, new JSONArray(aliases).toString());
} else {
return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
}
}
Aggregations