Skip to content

Instantly share code, notes, and snippets.

@capitalterefe
Last active June 20, 2017 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capitalterefe/b248b31fb215f8d0576fa3d52e08f836 to your computer and use it in GitHub Desktop.
Save capitalterefe/b248b31fb215f8d0576fa3d52e08f836 to your computer and use it in GitHub Desktop.
threatAuth
package threatConnect;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MediaType;
import org.apache.commons.codec.binary.Base64;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class ThreatConnectAuth {
public static void main(String[] args) {
String authorization = null;
long millis = System.currentTimeMillis() / 1000L;
try {
String api_id = "my_api_id";
String secretkey = "my_sec_key";
String signingBase = String.format("%s:%s:%d","https://sandbox.threatconnect.com/api/v2/groups/incidents", HttpMethod.GET, millis);
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secretkey.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(signingBase.getBytes()));
authorization = String.format("TC %s:%s", api_id, hash);
System.out.println(authorization);
} catch (Exception e) {
System.out.println("Error");
}
if (callThreatConnect(authorization, millis).getStatus() != 200) {
System.out.println("Failed With Status: " + callThreatConnect(authorization, millis).getStatus());
}
}
public static ClientResponse callThreatConnect(String authorization, long date) {
WebResource resource = Client.create(new DefaultClientConfig())
.resource("https://sandbox.threatconnect.com/api/v2/groups/incidents");
WebResource.Builder builder = resource.accept(MediaType.APPLICATION_JSON);
builder.header("Authorization", authorization);
builder.header("Timestamp", date);
builder.header("Host", "api.threatconnect.com");
return builder.get(ClientResponse.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment