Esempio n. 1
0
  @Override
  public CommandResponse execute(JSONObject json, ClientRequestHandlerInterface handler)
      throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException,
          SignatureException, ParseException {
    String guid = json.getString(GNSProtocol.GUID.toString());
    String field = json.getString(GNSProtocol.FIELD.toString());
    String accessType = json.getString(GNSProtocol.ACL_TYPE.toString());
    // allows someone other than guid to read acl, defaults to guid
    String reader = json.optString(GNSProtocol.READER.toString(), guid);
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    Date timestamp =
        json.has(GNSProtocol.TIMESTAMP.toString())
            ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString()))
            : null; // can be null on older client

    MetaDataTypeName access;
    if ((access = MetaDataTypeName.valueOf(accessType)) == null) {
      return new CommandResponse(
          ResponseCode.BAD_ACL_TYPE_ERROR,
          GNSProtocol.BAD_RESPONSE.toString()
              + " "
              + GNSProtocol.BAD_ACL_TYPE.toString()
              + "Should be one of "
              + Arrays.toString(MetaDataTypeName.values()));
    }
    return new CommandResponse(
        ResponseCode.NO_ERROR,
        Boolean.toString(
            FieldMetaData.fieldExists(
                access, guid, field, reader, signature, message, timestamp, handler)));
  }
  @Override
  public CommandResponse execute(JSONObject json, ClientRequestHandlerInterface handler)
      throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException,
          SignatureException, UnsupportedEncodingException {
    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.optString(GNSProtocol.SIGNATURE.toString(), null);
    String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);

    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 {
      // Add the account but don't enable email verification
      CommandResponse result =
          AccountAccess.addAccount(
              handler.getHttpServerHostPortString(),
              name,
              guid,
              publicKey,
              password,
              false,
              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());
    }
  }