-
Notifications
You must be signed in to change notification settings - Fork 9
/
Detectify.java
208 lines (173 loc) · 6.95 KB
/
Detectify.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.detectify;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Detectify {
/**
* Detectify API endpoint, no trailing slash
*/
private static final String Endpoint = "https://api.detectify.com/rest";
private String apiKey;
private String secretKey;
Detectify(String apiKey, String secretKey) {
this.apiKey = apiKey;
this.secretKey = secretKey;
}
/**
* Create the HTTP headers for API requests.
*
* @param method The HTTP method to use for the request, in uppercase
* @param path The path of the request
* @param timestamp The timestamp of the request
* @param body The optional body of the request
*/
private Map<String, String> MakeHeaders(String method, String path, Date timestamp, String body) {
// Signature timestamp uses Unix epoch time
Long epoch = timestamp.getTime() / 1000;
// Format hash payload
String message = String.format("%s;%s;%s;%s;%s", method, path, this.apiKey, epoch, body);
// Decode base64 secret key to binary
byte[] key = Base64.getDecoder().decode(this.secretKey);
// Create the signature
String signature = "";
try {
Mac hasher = Mac.getInstance("HmacSHA256");
hasher.init(new SecretKeySpec(key, "HmacSHA256"));
byte[] hash = hasher.doFinal(message.getBytes());
// Encode signature back to base64
signature = Base64.getEncoder().encodeToString(hash);
} catch (NoSuchAlgorithmException e) {
} catch (InvalidKeyException e) {
}
Map<String, String> headers = new HashMap<>();
headers.put("X-Detectify-Key", this.apiKey);
headers.put("X-Detectify-Signature", signature);
headers.put("X-Detectify-Timestamp", epoch.toString());
return headers;
}
public boolean StartScan(String scanProfile) {
Date timestamp = new Date();
// Format API request URL
String path = String.format("/v2/scans/%s/", scanProfile);
String method = "POST";
URL url;
try {
url = new URL(String.format("%s%s", Detectify.Endpoint, path));
} catch (IOException e) {
}
// Create Detectify signature HTTP headers
Map<String, String> headers = MakeHeaders(method, path, timestamp, "");
int statusCode;
// Call the Detectify API
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
// Add HTTP headers to request
for (Map.Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
// Get result of request
statusCode = conn.getResponseCode();
} catch (IOException e) {
}
switch (statusCode) {
case 202:
System.out.println("Scan start request accepted");
return true;
case 400:
System.out.println("Invalid scan profile token");
return false;
case 401:
System.out.println("Missing/invalid API key or message signature, or invalid timestamp");
return false;
case 403:
System.out.println("The API key cannot access this functionality");
return false;
case 409:
System.out.println("A scan is already running on the specified profile");
return false;
case 423:
System.out.println("The domain is not verified");
return false;
case 500:
System.out.println("An error occurred while processing the request");
return false;
case 503:
System.out.println("An error occurred while processing the request");
return false;
default:
System.out.println(String.format("Unhandled API response code: %d", statusCode));
return false;
}
}
public void ScanStatus(String scanProfile) {
Date timestamp = new Date();
// Format API request URL
String path = String.format("/v2/scans/%s/", scanProfile);
String method = "GET";
URL url;
try {
url = new URL(String.format("%s%s", Detectify.Endpoint, path));
} catch (IOException e) {
}
// Create Detectify signature HTTP headers
Map<String, String> headers = MakeHeaders(method, path, timestamp, "");
int statusCode;
// Buffer for JSON response
StringBuffer response = new StringBuffer();
// Call the Detectify API
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
for (Map.Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
// Get result of request
statusCode = conn.getResponseCode();
// Read JSON response
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
conn.disconnect();
} catch (IOException e) {
}
switch (statusCode) {
case 200:
System.out.println(response);
break;
case 400:
System.out.println("Invalid scan profile token");
break;
case 401:
System.out.println("Missing/invalid API key or message signature, or invalid timestamp");
break;
case 403:
System.out.println("The API key cannot access this functionality");
break;
case 404:
System.out.println("No scan running for the specified profile, or the specified scan profile does not exist or the API key cannot access the scan profile");
case 500:
System.out.println("An error occurred while processing the request");
break;
case 503:
System.out.println("An error occurred while processing the request");
break;
default:
System.out.println(String.format("Unhandled API response code: %d", statusCode));
}
}
}