Skip to content

Instantly share code, notes, and snippets.

@fernandomitre7
Created April 26, 2016 17:44
Show Gist options
  • Save fernandomitre7/973510ebd214d45b887b6799c4b59e57 to your computer and use it in GitHub Desktop.
Save fernandomitre7/973510ebd214d45b887b6799c4b59e57 to your computer and use it in GitHub Desktop.
/**
* Este metodo es el que genera la firma
* url es el url que estamos llamando String url = containerReqCtxt.getUriInfo().getAbsolutePath().toString();
* params es el mapa con los query params y body params (ver metodo extractInputParams)
* authkey es la llave secreta
*/
public static String generateSignature(String url,
Map<String, Object> params, String authKey)
throws NoSuchAlgorithmException, InvalidKeyException,
IllegalStateException, UnsupportedEncodingException {
// sort the params alphabetically, and append the key and value of each
// to the url
logger.info("url: {}", url);
logger.info("params: {}", params);
logger.info("authKey: {}", authKey);
StringBuffer data = new StringBuffer(url);
if (params != null) {
List<String> sortedKeys = new ArrayList<String>(params.keySet());
Collections.sort(sortedKeys);
for (String key : sortedKeys) {
data.append(key);
String value = "";
if (params.get(key) != null) {
Object objectValue = params.get(key);
if (objectValue instanceof List || objectValue instanceof Map) {
value = "[]";
} else {
value = String.valueOf(objectValue);
}
}
data.append(value);
}
}
logger.info("data: {}", data);
SecretKeySpec signkey = new SecretKeySpec(authKey.getBytes(), HMACSHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMACSHA1_ALGORITHM);
mac.init(signkey);
byte[] macbytes = mac.doFinal(data.toString().getBytes("UTF-8"));
String result = new String(Base64.encodeBase64(macbytes));
logger.debug("Generated Signature: {}", result);
return result;
}
/**
* Extracts all the call parameters.
*
* @param containerReqCtxt
* REST Container Request Context.
* @return Map with all the call parameters.
* @throws ProcessingException
* @throws InternalServerErrorException
*/
private Map<String, Object> extractInputParams(ContainerRequestContext containerReqCtxt)
throws ProcessingException, InternalServerErrorException {
Map<String, Object> params = new HashMap<String, Object>();
MultivaluedMap<String, String> queryParams = containerReqCtxt.getUriInfo().getQueryParameters();
for (Entry<String, List<String>> entry : queryParams.entrySet()) {
params.put(entry.getKey(), entry.getValue().get(0));
}
// Get Body Parameters
if (containerReqCtxt.hasEntity()) {
if ((containerReqCtxt instanceof ContainerRequest)) {
ContainerRequest containerReq = (ContainerRequest) containerReqCtxt;
containerReq.bufferEntity();
@SuppressWarnings("unchecked")
Map<String, String> bodyParams = containerReq.readEntity(HashMap.class);
params.putAll(bodyParams);
} else {
logger.error("Invalid instance Container request context [{}]", containerReqCtxt.getClass().getName());
throw new InternalServerErrorException();
}
}
return params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment