This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from joaopsilva/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 622
/
AuthenticationInterceptor.java
92 lines (78 loc) · 3.11 KB
/
AuthenticationInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.binance.api.client.security;
import com.binance.api.client.constant.BinanceApiConstants;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Objects;
/**
* A request interceptor that injects the API Key Header into requests, and signs messages, whenever required.
*/
public class AuthenticationInterceptor implements Interceptor {
private final String apiKey;
private final String secret;
public AuthenticationInterceptor(String apiKey, String secret) {
this.apiKey = apiKey;
this.secret = secret;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder newRequestBuilder = original.newBuilder();
boolean isApiKeyRequired = original.header(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_APIKEY) != null;
boolean isSignatureRequired = original.header(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_SIGNED) != null;
newRequestBuilder.removeHeader(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_SIGNED)
.removeHeader(BinanceApiConstants.ENDPOINT_SECURITY_TYPE_SIGNED);
// Endpoint requires sending a valid API-KEY
if (isApiKeyRequired || isSignatureRequired) {
newRequestBuilder.addHeader(BinanceApiConstants.API_KEY_HEADER, apiKey);
}
// Endpoint requires signing the payload
if (isSignatureRequired) {
String payload = original.url().query();
if (!StringUtils.isEmpty(payload)) {
String signature = HmacSHA256Signer.sign(payload, secret);
HttpUrl signedUrl = original.url().newBuilder().addQueryParameter("signature", signature).build();
newRequestBuilder.url(signedUrl);
}
}
// Build new request after adding the necessary authentication information
Request newRequest = newRequestBuilder.build();
return chain.proceed(newRequest);
}
/**
* Extracts the request body into a String.
*
* @return request body as a string
*/
@SuppressWarnings("unused")
private static String bodyToString(RequestBody request) {
try (final Buffer buffer = new Buffer()) {
final RequestBody copy = request;
if (copy != null) {
copy.writeTo(buffer);
} else {
return "";
}
return buffer.readUtf8();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final AuthenticationInterceptor that = (AuthenticationInterceptor) o;
return Objects.equals(apiKey, that.apiKey) &&
Objects.equals(secret, that.secret);
}
@Override
public int hashCode() {
return Objects.hash(apiKey, secret);
}
}