Last active
June 20, 2017 02:17
-
-
Save capitalterefe/b248b31fb215f8d0576fa3d52e08f836 to your computer and use it in GitHub Desktop.
threatAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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