Search in sources :

Example 16 with CommandResponse

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.

the class LookupRandomGuids method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws JSONException {
    JSONObject json = commandPacket.getCommand();
    String guid = json.getString(GNSProtocol.GUID.toString());
    int count = json.getInt(GNSProtocol.GUIDCNT.toString());
    AccountInfo acccountInfo;
    if ((acccountInfo = AccountAccess.lookupAccountInfoFromGuidLocally(header, guid, handler)) == null) {
        return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + guid);
    }
    if (acccountInfo != null) {
        List<String> guids = acccountInfo.getGuids();
        if (count >= guids.size()) {
            return new CommandResponse(ResponseCode.NO_ERROR, new JSONArray(guids).toString());
        } else {
            Random rand = new Random();
            List<String> result = new ArrayList<>();
            for (int i = 0; i < count; i++) {
                result.add(guids.get(rand.nextInt(guids.size())));
            }
            return new CommandResponse(ResponseCode.NO_ERROR, new JSONArray(result).toString());
        }
    } else {
        return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guid);
    }
// }
}
Also used : JSONObject(org.json.JSONObject) Random(java.util.Random) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Example 17 with CommandResponse

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.

the class RegisterAccount 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 name = json.getString(GNSProtocol.NAME.toString());
    String publicKey = json.getString(GNSProtocol.PUBLIC_KEY.toString());
    String password = json.getString(GNSProtocol.PASSWORD.toString());
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
    if (!NSAccessSupport.verifySignature(publicKey, signature, message)) {
        return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
    }
    try {
        CommandResponse result = AccountAccess.addAccount(header, commandPacket, handler.getHttpServerHostPortString(), name, guid, publicKey, password, Config.getGlobalBoolean(GNSConfig.GNSC.ENABLE_EMAIL_VERIFICATION), handler);
        if (result.getExceptionOrErrorCode().isOKResult()) {
            // Everything is hunkey dorey so return the new guid
            return new CommandResponse(ResponseCode.NO_ERROR, guid);
        } else {
            assert (result.getExceptionOrErrorCode() != null);
            // Otherwise return the error response.
            return result;
        }
    } catch (ClientException | IOException e) {
        return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException)

Example 18 with CommandResponse

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.

the class RemoveAccount method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException, InternalRequestException {
    JSONObject json = commandPacket.getCommand();
    // The name of the account we are removing.
    String name = json.getString(GNSProtocol.NAME.toString());
    // The guid of the account we are removing.
    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) {
        // Removing a non-existant guid is not 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() + " " + guid);
    }
    if (NSAccessSupport.verifySignature(guidInfo.getPublicKey(), signature, message)) {
        AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromNameAnywhere(header, name, handler);
        if (accountInfo != null) {
            return AccountAccess.removeAccount(header, commandPacket, accountInfo, handler);
        } else {
            return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString());
        }
    } else {
        return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
    }
}
Also used : JSONObject(org.json.JSONObject) GuidInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Example 19 with CommandResponse

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.

the class RemoveAlias method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, ParseException {
    JSONObject json = commandPacket.getCommand();
    String guid = json.getString(GNSProtocol.GUID.toString());
    String name = json.getString(GNSProtocol.NAME.toString());
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    // can be null on older client
    Date timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : null;
    if (AccountAccess.lookupGuidInfoAnywhere(header, guid, handler) == null) {
        return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guid);
    }
    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);
    } else if (!accountInfo.isVerified()) {
        return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
    }
    return AccountAccess.removeAlias(header, commandPacket, accountInfo, name, guid, signature, message, timestamp, handler);
}
Also used : JSONObject(org.json.JSONObject) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) Date(java.util.Date) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Example 20 with CommandResponse

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse in project GNS by MobilityFirst.

the class AddAlias method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, ParseException {
    JSONObject json = commandPacket.getCommand();
    // The guid of the account we are adding the alias to
    String guid = json.getString(GNSProtocol.GUID.toString());
    // The HRN (alias) we are adding to this account guid
    String name = json.getString(GNSProtocol.NAME.toString());
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    // can be null on older client
    Date timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : null;
    // Fixme: Does this really need remote access?
    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.getAliases().size() > Config.getGlobalInt(GNSConfig.GNSC.ACCOUNT_GUID_MAX_ALIASES)) {
        return new CommandResponse(ResponseCode.TOO_MANY_ALIASES_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.TOO_MANY_ALIASES.toString());
    } else {
        return AccountAccess.addAlias(header, commandPacket, accountInfo, name, guid, signature, message, timestamp, handler);
    }
}
Also used : JSONObject(org.json.JSONObject) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) Date(java.util.Date) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Aggregations

CommandResponse (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse)41 JSONObject (org.json.JSONObject)41 Date (java.util.Date)25 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)16 AccountInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)13 GuidInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo)11 MetaDataTypeName (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.MetaDataTypeName)9 JSONArray (org.json.JSONArray)8 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)7 IOException (java.io.IOException)7 ResultValue (edu.umass.cs.gnsserver.utils.ResultValue)5 JSONException (org.json.JSONException)5 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)4 CommandType (edu.umass.cs.gnscommon.CommandType)1 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)1 CommandPacket (edu.umass.cs.gnscommon.packets.CommandPacket)1 AbstractCommand (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commands.AbstractCommand)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1