Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDetectify.java
More file actions
Latest commit
208 lines (173 loc) · 6.95 KB
/
Copy pathDetectify.java
File metadata and controls
208 lines (173 loc) · 6.95 KB
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
packagecom.detectify;
importjavax.crypto.Mac;
importjavax.crypto.spec.SecretKeySpec;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.URL;
importjava.security.InvalidKeyException;
importjava.security.NoSuchAlgorithmException;
importjava.util.Base64;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;
publicclassDetectify {
/**
* Detectify API endpoint, no trailing slash
*/
privatestaticfinalStringEndpoint = "https://api.detectify.com/rest";
privateStringapiKey;
privateStringsecretKey;
Detectify(StringapiKey, StringsecretKey) {
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
*/
privateMap<String, String> MakeHeaders(Stringmethod, Stringpath, Datetimestamp, Stringbody) {
// Signature timestamp uses Unix epoch time
Longepoch = timestamp.getTime() / 1000;
// Format hash payload
Stringmessage = 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
Stringsignature = "";
try {
Machasher = Mac.getInstance("HmacSHA256");
hasher.init(newSecretKeySpec(key, "HmacSHA256"));
byte[] hash = hasher.doFinal(message.getBytes());
// Encode signature back to base64
signature = Base64.getEncoder().encodeToString(hash);
} catch (NoSuchAlgorithmExceptione) {
} catch (InvalidKeyExceptione) {
}
Map<String, String> headers = newHashMap<>();
headers.put("X-Detectify-Key", this.apiKey);
headers.put("X-Detectify-Signature", signature);
headers.put("X-Detectify-Timestamp", epoch.toString());
returnheaders;
}
publicbooleanStartScan(StringscanProfile) {
Datetimestamp = newDate();
// Format API request URL
Stringpath = String.format("/v2/scans/%s/", scanProfile);
Stringmethod = "POST";
URLurl;
try {
url = newURL(String.format("%s%s", Detectify.Endpoint, path));
} catch (IOExceptione) {
}
// Create Detectify signature HTTP headers
Map<String, String> headers = MakeHeaders(method, path, timestamp, "");
intstatusCode;
// Call the Detectify API
try {
HttpURLConnectionconn = (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 (IOExceptione) {
}
switch (statusCode) {
case202:
System.out.println("Scan start request accepted");
returntrue;
case400:
System.out.println("Invalid scan profile token");
returnfalse;
case401:
System.out.println("Missing/invalid API key or message signature, or invalid timestamp");
returnfalse;
case403:
System.out.println("The API key cannot access this functionality");
returnfalse;
case409:
System.out.println("A scan is already running on the specified profile");
returnfalse;
case423:
System.out.println("The domain is not verified");
returnfalse;
case500:
System.out.println("An error occurred while processing the request");
returnfalse;
case503:
System.out.println("An error occurred while processing the request");
returnfalse;
default:
System.out.println(String.format("Unhandled API response code: %d", statusCode));
returnfalse;
}
}
publicvoidScanStatus(StringscanProfile) {
Datetimestamp = newDate();
// Format API request URL
Stringpath = String.format("/v2/scans/%s/", scanProfile);
Stringmethod = "GET";
URLurl;
try {
url = newURL(String.format("%s%s", Detectify.Endpoint, path));
} catch (IOExceptione) {
}
// Create Detectify signature HTTP headers
Map<String, String> headers = MakeHeaders(method, path, timestamp, "");
intstatusCode;
// Buffer for JSON response
StringBufferresponse = newStringBuffer();
// Call the Detectify API
try {
HttpURLConnectionconn = (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
BufferedReaderin = newBufferedReader(
newInputStreamReader(conn.getInputStream()));
StringinputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
conn.disconnect();
} catch (IOExceptione) {
}
switch (statusCode) {
case200:
System.out.println(response);
break;
case400:
System.out.println("Invalid scan profile token");
break;
case401:
System.out.println("Missing/invalid API key or message signature, or invalid timestamp");
break;
case403:
System.out.println("The API key cannot access this functionality");
break;
case404:
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");
case500:
System.out.println("An error occurred while processing the request");
break;
case503:
System.out.println("An error occurred while processing the request");
break;
default:
System.out.println(String.format("Unhandled API response code: %d", statusCode));
}
}
}