@Override public String digestParams(RestInvocation restInvocation) { Params params = restInvocation.getParamsMap().get(FormParam.class); StringBuilder queryString = new StringBuilder(params.asQueryString()); Mac mac = getMac(); byte[] source = mac.doFinal(queryString.toString().getBytes()); return String.format("%0128x", new BigInteger(1, source)); }
@Override public String digestParams(RestInvocation restInvocation) { Params params; if (restInvocation.getHttpMethod().equals("GET")) params = restInvocation.getParamsMap().get(QueryParam.class); else params = restInvocation.getParamsMap().get(FormParam.class); Map<String, String> sorted = new TreeMap<>(params.asHttpHeaders()); String queryString = buildQueryString(sorted); Mac mac = getMac(); mac.update(queryString.getBytes(StandardCharsets.UTF_8)); return String.format("%064x", new BigInteger(1, mac.doFinal())).toUpperCase(); } }
@Override public String digestParams(RestInvocation restInvocation) { // Get Parameters Map<String, String> params = restInvocation.getParamsMap().get(FormParam.class).asHttpHeaders(); // TODO: Find More Elegant Solution To Remove Sign // Order By Key Alphabetically, Concancecate Values byte[] unsigned = params .entrySet() .stream() .sorted(Map.Entry.<String, String>comparingByKey()) .filter(e -> !e.getKey().equalsIgnoreCase("sign")) .map(e -> e.getValue()) .collect(Collectors.joining()) .getBytes(); // TODO: Determine Charceter Encoding return String.valueOf(md5.digest(unsigned)); } }
/** @return the query string except of the "signature" parameter */ private static String getQuery(RestInvocation restInvocation) { final Params p = Params.of(); restInvocation .getParamsMap() .get(QueryParam.class) .asHttpHeaders() .entrySet() .stream() .filter(e -> !BinanceAuthenticated.SIGNATURE.equals(e.getKey())) .forEach(e -> p.add(e.getKey(), e.getValue())); return p.asQueryString(); }
/** {@inheritDoc} */ @Override public String digestParams(RestInvocation restInvocation) { Params params = restInvocation.getParamsMap().get(FormParam.class); Map<String, String> nameValues = params.asHttpHeaders(); nameValues.remove("signature"); Params newParams = Params.of(); for (Map.Entry<String, String> nameValue : nameValues.entrySet()) { newParams.add(nameValue.getKey(), nameValue.getValue()); } String message = newParams.asQueryString(); Mac mac = getMac(); mac.update(message.getBytes()); return String.format("%064x", new BigInteger(1, mac.doFinal())); } }
@Override public String digestParams(RestInvocation restInvocation) { String method = restInvocation.getHttpMethod(); String path = stripParams(restInvocation.getPath()); String query = Stream.of( restInvocation.getParamsMap().get(PathParam.class), restInvocation.getParamsMap().get(FormParam.class)) .map(Params::asHttpHeaders) .map(Map::entrySet) .flatMap(Collection::stream) .filter(e -> !"signature".equals(e.getKey())) .sorted(Entry.comparingByKey()) .map(e -> e.getKey() + "=" + e.getValue()) .collect(Collectors.joining("&")); String toSign = String.format("%s|/api/v2/%s|%s", method, path, query); Mac sha256hmac = getMac(); byte[] signed = sha256hmac.doFinal(toSign.getBytes()); String signature = new String(encodeHex(signed)); replaceInvocationUrl(restInvocation, signature); return signature; }
@Override public String digestParams(RestInvocation restInvocation) { // just write down their example code without thinking too much // https://kucoinapidocs.docs.apiary.io/#introduction/authentication/signature-calculation String endpoint = "/" + restInvocation.getPath(); // needs leading slash String queryString = restInvocation .getParamsMap() .get(QueryParam.class) .asHttpHeaders() .entrySet() .stream() .sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())) .map(e -> e.getKey() + "=" + e.getValue()) .collect(Collectors.joining("&")); Long nonce = (Long) restInvocation.getParamValue(HeaderParam.class, KucoinAuthenticated.HEADER_NONCE); String strForSign = endpoint + "/" + nonce + "/" + queryString; Mac mac = getMac(); try { mac.update(Base64.getEncoder().encode(strForSign.getBytes("UTF-8"))); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } return DigestUtils.bytesToHex(mac.doFinal()).toLowerCase(); } }
@Override public String digestParams(RestInvocation restInvocation) { // Create Query String From Form Parameters Params params = Params.of(); restInvocation .getParamsMap() .get(FormParam.class) .asHttpHeaders() .entrySet() .stream() .filter(e -> !e.getKey().equalsIgnoreCase("signature")) .forEach(e -> params.add(e.getKey(), e.getValue())); // Parse Query String byte[] queryString = params.asQueryString().trim().getBytes(UTF8); // Create And Return Signature return hex(getMac().doFinal(queryString)); } }
@Override public String digestParams(RestInvocation restInvocation) { final Params params = restInvocation.getParamsMap().get(FormParam.class); final Map<String, String> nameValueMap = params.asHttpHeaders();
@Override public String digestParams(RestInvocation restInvocation) { String httpMethod = restInvocation.getHttpMethod(); String host = getHost(restInvocation.getBaseUrl()); String method = "/" + restInvocation.getMethodPath(); String query = Stream.of( restInvocation.getParamsMap().get(FormParam.class), restInvocation.getParamsMap().get(QueryParam.class)) .map(Params::asHttpHeaders) .map(Map::entrySet) .flatMap(Collection::stream) .filter(e -> !"Signature".equals(e.getKey())) .sorted(Map.Entry.comparingByKey()) .map(e -> e.getKey() + "=" + encodeValue(e.getValue())) .collect(Collectors.joining("&")); String toSign = String.format("%s\n%s\n%s\n%s", httpMethod, host, method, query); Mac mac = getMac(); String signature = encodeValue(Base64.getEncoder().encodeToString(mac.doFinal(toSign.getBytes())).trim()); replaceSignatureUrl(restInvocation, signature); return signature; }
String method = restInvocation.getHttpMethod(); String uri = restInvocation.getInvocationUrl(); Map<Class<? extends Annotation>, Params> params = restInvocation.getParamsMap(); if (params.containsKey(QueryParam.class)) { uri = params.get(QueryParam.class).applyToPath(uri);
@Override public String digestParams(RestInvocation restInvocation) { String queryString = ""; if (restInvocation.getParamsMap().get(QueryParam.class) != null && !restInvocation.getParamsMap().get(QueryParam.class).isEmpty()) { queryString = String.valueOf(restInvocation.getParamsMap().get(QueryParam.class)); } else if (restInvocation.getRequestBody() != null && !restInvocation.getRequestBody().isEmpty()) {
/** * @param paramAnnotation One of {@link javax.ws.rs.QueryParam}.class, {@link javax.ws.rs.PathParam}.class, * {@link javax.ws.rs.FormParam}.class, {@link javax.ws.rs.HeaderParam}.class * @param paramName The name of the parameter, ie. the value of the value() element in the annotation. * @return The actual value that was passed as the argument in the method call; null if either no * parameter with the given name exists or null was passed as argument. */ public Object getParamValue(Class<? extends Annotation> paramAnnotation, String paramName) { if (!PARAM_ANNOTATION_CLASSES.contains(paramAnnotation)) { throw new IllegalArgumentException("Unsupported annotation type: " + paramAnnotation + ". Should be one of " + PARAM_ANNOTATION_CLASSES); } return getParamsMap().get(paramAnnotation).getParamValue(paramName); }
/** * @param paramAnnotation One of {@link javax.ws.rs.QueryParam}.class, {@link javax.ws.rs.PathParam}.class, * {@link javax.ws.rs.FormParam}.class, {@link javax.ws.rs.HeaderParam}.class * @param paramName The name of the parameter, ie. the value of the value() element in the annotation. * @return The actual value that was passed as the argument in the method call; null if either no * parameter with the given name exists or null was passed as argument. */ public Object getParamValue(Class<? extends Annotation> paramAnnotation, String paramName) { if (!PARAM_ANNOTATION_CLASSES.contains(paramAnnotation)) { throw new IllegalArgumentException("Unsupported annotation type: " + paramAnnotation + ". Should be one of " + PARAM_ANNOTATION_CLASSES); } return getParamsMap().get(paramAnnotation).getParamValue(paramName); }
public Map<String, String> getHttpHeadersFromParams() { return getParamsMap().get(HeaderParam.class).asHttpHeaders(); }
public Map<String, String> getHttpHeadersFromParams() { return getParamsMap().get(HeaderParam.class).asHttpHeaders(); }
public String writeBody(RestInvocation invocation) { //just safety checks if (!invocation.getParamsMap().get(FormParam.class).isEmpty() || invocation.getUnannanotatedParams().isEmpty()) { throw new IllegalArgumentException("No media type specified; don't know how to create request body. Please specify the body media type using @javax.ws.rs.Consumes."); } return null; } }
public String writeBody(RestInvocation invocation) { //just safety checks if (!invocation.getParamsMap().get(FormParam.class).isEmpty() || invocation.getUnannanotatedParams().isEmpty()) { throw new IllegalArgumentException("No media type specified; don't know how to create request body. Please specify the body media type using @javax.ws.rs.Consumes."); } return null; } }
/** @return the query string except of the "signature" parameter */ private static String getQuery(RestInvocation restInvocation) { final Params p = Params.of(); restInvocation.getParamsMap().get(QueryParam.class).asHttpHeaders().entrySet().stream() .filter(e -> !BinanceAuthenticated.SIGNATURE.equals(e.getKey())) .forEach(e -> p.add(e.getKey(), e.getValue())); return p.asQueryString(); }
/** * {@inheritDoc} */ @Override public String digestParams(RestInvocation restInvocation) { return digestParams(restInvocation.getParamsMap().get(FormParam.class).asHttpHeaders()); }