public String getSignature(String baseString, OAuthParameters oauthParameters)
     throws OAuthException {
   try {
     if (oauthParameters == null) {
       throw new OAuthException("OAuth parameters cannot be null");
     }
     String keyString = getKey(oauthParameters);
     SecretKey key = new SecretKeySpec(keyString.getBytes("UTF-8"), "HmacSHA1");
     Mac mac = Mac.getInstance("HmacSHA1");
     mac.init(key);
     return Base64.encode(mac.doFinal(baseString.getBytes("UTF-8")));
   } catch (UnsupportedEncodingException e) {
     throw new OAuthException(e);
   } catch (NoSuchAlgorithmException e) {
     throw new OAuthException(e);
   } catch (InvalidKeyException e) {
     throw new OAuthException(e);
   }
 }
Example #2
0
  public static ArrayList<String> extractDataFromZip(ZipInputStream zis)
      throws IOException, SignedDataException {
    ArrayList<String> lines = new ArrayList<String>();
    String line = null;
    String surveyDataOnly = null;
    String dataSig = null;
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
      log.info("Unzipping: " + entry.getName());
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      byte[] buffer = new byte[2048];
      int size;
      while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
        out.write(buffer, 0, size);
      }
      line = out.toString("UTF-8");

      if (entry.getName().endsWith("txt")) {
        if (entry.getName().equals("regions.txt")) {
          lines.add("regionFlag=true");
        } else {
          surveyDataOnly = line;
        }
        String[] linesSplit = line.split("\n");
        for (String s : linesSplit) {
          if (s.contains("\u0000")) {
            s = s.replaceAll("\u0000", "");
          }
          lines.add(s);
        }
      } else if (entry.getName().endsWith(".sig")) {
        dataSig = line.trim();
      } else {
        S3Driver s3 = new S3Driver();
        String[] imageParts = entry.getName().split("/");
        // comment out while testing locally
        try {
          // GAEImageAdapter gaeIA = new GAEImageAdapter();
          // byte[] resizedImage =
          // gaeIA.resizeImage(out.toByteArray(), 500, 500);
          // s3.uploadFile("dru-test", imageParts[1], resizedImage);
          GAEImageAdapter gaeImg = new GAEImageAdapter();
          byte[] newImage = gaeImg.resizeImage(out.toByteArray(), 500, 500);
          s3.uploadFile("dru-test", imageParts[1], newImage);
          // add queue call to resize
          Queue queue = QueueFactory.getDefaultQueue();

          queue.add(TaskOptions.Builder.withUrl("imageprocessor").param("imageURL", imageParts[1]));
          log.info("submiting image resize for imageURL: " + imageParts[1]);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
        out.close();
      }
      zis.closeEntry();
    }
    // check the signature if we have it
    if (surveyDataOnly != null && dataSig != null) {
      try {
        MessageDigest sha1Digest = MessageDigest.getInstance("SHA1");
        byte[] digest = sha1Digest.digest(surveyDataOnly.getBytes("UTF-8"));
        SecretKeySpec signingKey =
            new SecretKeySpec(
                PropertyUtil.getProperty(SIGNING_KEY).getBytes("UTF-8"), SIGNING_ALGORITHM);
        Mac mac = Mac.getInstance(SIGNING_ALGORITHM);
        mac.init(signingKey);
        byte[] hmac = mac.doFinal(digest);

        String encodedHmac = com.google.gdata.util.common.util.Base64.encode(hmac);
        if (!encodedHmac.trim().equals(dataSig.trim())) {
          String allowUnsigned = PropertyUtil.getProperty(ALLOW_UNSIGNED);
          if (allowUnsigned != null && allowUnsigned.trim().equalsIgnoreCase("false")) {
            throw new SignedDataException(
                "Computed signature does not match the one submitted with the data");
          } else {
            log.warning("Signatures don't match. Processing anyway since allow unsigned is true");
          }
        }
      } catch (GeneralSecurityException e) {
        throw new SignedDataException("Could not calculate signature", e);
      }

    } else if (surveyDataOnly != null) {
      // if there is no signature, check the configuration to see if we
      // are allowed to proceed
      String allowUnsigned = PropertyUtil.getProperty(ALLOW_UNSIGNED);
      if (allowUnsigned != null && allowUnsigned.trim().equalsIgnoreCase("false")) {
        throw new SignedDataException("Datafile does not have a signature");
      }
    }

    return lines;
  }