Search in sources :

Example 1 with ApiRequestSignerException

use of com.bluenimble.platform.api.security.ApiRequestSignerException in project serverless by bluenimble.

the class DefaultApiRequestSigner method sign.

/**
 * [VERB] + '\n' + [ENDPOINT] + '\n' + [REQUEST URI: must starts with '/'] + '\n' + [ACCESS_KEY] '\n' + [ORDERED REQUEST PARAMETERS] + '\n' + [Timestamp Header]
 */
public String sign(ApiRequest request, String utcTimestamp, String accessKey, String secretKey, boolean writeTorequest) throws ApiRequestSignerException {
    SortedMap<String, String> sorted = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    Iterator<String> names = request.keys(Scope.Parameter);
    if (names != null) {
        while (names.hasNext()) {
            String name = names.next();
            sorted.put(name, String.valueOf(request.get(name)));
        }
    }
    String params = canonicalize(sorted);
    String rEndpoint = request.getEndpoint();
    StringBuilder sb = new StringBuilder();
    sb.append(request.getVerb().name()).append(Lang.ENDLN).append(request.getScheme()).append(Lang.COLON).append(Lang.SLASH).append(Lang.SLASH).append(rEndpoint).append(Lang.ENDLN).append(request.getPath()).append(Lang.ENDLN).append(params).append(Lang.ENDLN).append(accessKey).append(Lang.ENDLN).append(utcTimestamp);
    String s = sb.toString();
    sb.setLength(0);
    try {
        s = hmac(secretKey, s);
    } catch (Exception e) {
        throw new ApiRequestSignerException(e.getMessage(), e);
    }
    return s;
}
Also used : ApiRequestSignerException(com.bluenimble.platform.api.security.ApiRequestSignerException) TreeMap(java.util.TreeMap) ApiRequestSignerException(com.bluenimble.platform.api.security.ApiRequestSignerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ApiRequestSignerException (com.bluenimble.platform.api.security.ApiRequestSignerException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 TreeMap (java.util.TreeMap)1