From e7244028173aa2d4a6a7fb7e7534d3504c196dd2 Mon Sep 17 00:00:00 2001 From: JP Chen Date: Fri, 28 May 2021 09:47:12 -0700 Subject: [PATCH] Remove state variable in HmacAuthenticationPolicy --- .../HmacAuthenticationPolicy.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/implementation/HmacAuthenticationPolicy.java b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/implementation/HmacAuthenticationPolicy.java index 781364e6f2370..ea43fabf58252 100644 --- a/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/implementation/HmacAuthenticationPolicy.java +++ b/sdk/communication/azure-communication-common/src/main/java/com/azure/communication/common/implementation/HmacAuthenticationPolicy.java @@ -58,8 +58,6 @@ public final class HmacAuthenticationPolicy implements HttpPipelinePolicy { DateTimeFormatter.ofPattern("E, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); private final AzureKeyCredential credential; - - private Mac sha256HMAC; private final ClientLogger logger = new ClientLogger(HmacAuthenticationPolicy.class); /** @@ -72,18 +70,7 @@ public HmacAuthenticationPolicy(AzureKeyCredential clientCredential) { } @Override - public synchronized Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { - String accessKey = credential.getKey(); - byte[] key = Base64.getDecoder().decode(accessKey); - Mac sha256HMAC; - try { - sha256HMAC = Mac.getInstance("HmacSHA256"); - sha256HMAC.init(new SecretKeySpec(key, "HmacSHA256")); - } catch (NoSuchAlgorithmException | InvalidKeyException e) { - throw logger.logExceptionAsError(new RuntimeException(e)); - } - this.sha256HMAC = sha256HMAC; - + public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { final Flux contents = context.getHttpRequest().getBody() == null ? Flux.just(ByteBuffer.allocate(0)) : context.getHttpRequest().getBody(); @@ -136,7 +123,7 @@ private Map addAuthenticationHeaders(final URL url, return headers; } - private synchronized void addSignatureHeader(final URL url, final String httpMethod, final Map httpHeaders) { + private void addSignatureHeader(final URL url, final String httpMethod, final Map httpHeaders) { final String signedHeaderNames = String.join(";", SIGNED_HEADERS); final String signedHeaderValues = Arrays.stream(SIGNED_HEADERS) .map(httpHeaders::get) @@ -150,6 +137,17 @@ private synchronized void addSignatureHeader(final URL url, final String httpMet // String-To-Sign=HTTP_METHOD + '\n' + path_and_query + '\n' + signed_headers_values // The line separator has to be \n. Using %n with String.format will result in a 401 from the service. String stringToSign = httpMethod.toUpperCase(Locale.US) + "\n" + pathAndQuery + "\n" + signedHeaderValues; + + String accessKey = credential.getKey(); + byte[] key = Base64.getDecoder().decode(accessKey); + Mac sha256HMAC; + try { + sha256HMAC = Mac.getInstance("HmacSHA256"); + sha256HMAC.init(new SecretKeySpec(key, "HmacSHA256")); + } catch (NoSuchAlgorithmException | InvalidKeyException e) { + throw logger.logExceptionAsError(new RuntimeException(e)); + } + final String signature = Base64.getEncoder().encodeToString(sha256HMAC.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8))); httpHeaders.put(AUTHORIZATIONHEADERNAME, String.format(HMACSHA256FORMAT, signedHeaderNames, signature));