Need help with authentication and access to Pritunl API. I get 401 Unauthorized and I think it’s happening because I missed something when converted python to java. If somebody could share his/her example or give a hint how to fix an issue. Thanks in advance.
My example:
String path = "/organization";
String url = API_URL + path;
String timestamp = String.valueOf(Instant.now().getEpochSecond());
String authNonce = UUID.randomUUID().toString();
StringBuilder authStringBuilder = new StringBuilder();
authStringBuilder.append(API_TOKEN).append("&");
authStringBuilder.append(timestamp).append("&");
authStringBuilder.append(authNonce).append("&");
authStringBuilder.append("GET").append("&");
authStringBuilder.append(path);
String authString = authStringBuilder.toString();
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(API_SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
mac.init(secretKey);
byte[] hmacBytes = mac.doFinal(authString.getBytes(StandardCharsets.UTF_8));
String authSignature = Base64.getEncoder().encodeToString(hmacBytes);
HttpHeaders headers = new HttpHeaders();
headers.set("Auth-Token", API_TOKEN);
headers.set("Auth-Nonce", authNonce);
headers.set("Auth-Timestamp", timestamp);
headers.set("Auth-Signature", authSignature);
headers.set("Content-Type", "application/json");
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(new URI(url), HttpMethod.GET, request, String.class);
return response.getBody();