@Test public void testSignHmacAddHeaders() { SecurityEnvironment env = SecurityEnvironment.builder() .targetUri(URI.create("http: .build(); OutboundTargetDefinition outboundDef = OutboundTargetDefinition.builder("myServiceKeyId") .hmacSecret("MyPasswordForHmac") .signedHeaders(SignedHeadersConfig.builder() .defaultConfig(SignedHeadersConfig .HeadersConfig .create(List.of("date", "host"))) .build()) .build(); HttpSignature.sign(env, outboundDef, new HashMap<>()); }
static HttpSignature sign(SecurityEnvironment env, OutboundTargetDefinition outboundDefinition, Map<String, List<String>> newHeaders) { HttpSignature signature = new HttpSignature(outboundDefinition.keyId(), outboundDefinition.algorithm(), outboundDefinition.signedHeadersConfig() .headers(env.method(), env.headers())); switch (signature.getAlgorithm()) { case HttpSignProvider.ALGORITHM_RSA: signature.signatureBytes = signature.signRsaSha256(env, outboundDefinition.keyConfig() .orElseThrow(() -> new HttpSignatureException( "Private key configuration must be present to use " + HttpSignProvider.ALGORITHM_RSA + " " + "algorithm")), newHeaders); break; case HttpSignProvider.ALGORITHM_HMAC: signature.signatureBytes = signature.signHmacSha256(env, outboundDefinition.hmacSharedSecret() .orElseThrow(() -> new HttpSignatureException( "HMAC shared secret must be configured to use " + HttpSignProvider.ALGORITHM_HMAC + " algorithm")), newHeaders); break; default: throw new HttpSignatureException("Unsupported signature algorithm: " + signature.getAlgorithm()); } signature.base64Signature = Base64.getEncoder().encodeToString(signature.signatureBytes); return signature; }
HttpSignature { static HttpSignature sign(SecurityEnvironment env, OutboundTargetDefinition outboundDefinition, Map<String, List<String>> newHeaders) { HttpSignature signature = new HttpSignature(outboundDefinition.keyId(), outboundDefinition.algorithm(), outboundDefinition.signedHeadersConfig() .headers(env.method(), env.headers())); switch (signature.getAlgorithm()) { case HttpSignProvider.ALGORITHM_RSA: signature.signatureBytes = signature.signRsaSha256(env, outboundDefinition.keyConfig() .orElseThrow(() -> new HttpSignatureException( "Private key configuration must be present to use " + HttpSignProvider.ALGORITHM_RSA + " " + "algorithm")), newHeaders); break; case HttpSignProvider.ALGORITHM_HMAC: signature.signatureBytes = signature.signHmacSha256(env, outboundDefinition.hmacSharedSecret() .orElseThrow(() -> new HttpSignatureException( "HMAC shared secret must be configured to use " + HttpSignProvider.ALGORITHM_HMAC + " algorithm")), newHeaders); break; default: throw new HttpSignatureException("Unsupported signature algorithm: " + signature.getAlgorithm()); } signature.base64Signature = Base64.getEncoder().encodeToString(signature.signatureBytes); return signature; } }
HttpSignature { static HttpSignature sign(SecurityEnvironment env, OutboundTargetDefinition outboundDefinition, Map<String, List<String>> newHeaders) { HttpSignature signature = new HttpSignature(outboundDefinition.keyId(), outboundDefinition.algorithm(), outboundDefinition.signedHeadersConfig() .headers(env.method(), env.headers())); switch (signature.getAlgorithm()) { case HttpSignProvider.ALGORITHM_RSA: signature.signatureBytes = signature.signRsaSha256(env, outboundDefinition.keyConfig() .orElseThrow(() -> new HttpSignatureException( "Private key configuration must be present to use " + HttpSignProvider.ALGORITHM_RSA + " " + "algorithm")), newHeaders); break; case HttpSignProvider.ALGORITHM_HMAC: signature.signatureBytes = signature.signHmacSha256(env, outboundDefinition.hmacSharedSecret() .orElseThrow(() -> new HttpSignatureException( "HMAC shared secret must be configured to use " + HttpSignProvider.ALGORITHM_HMAC + " algorithm")), newHeaders); break; default: throw new HttpSignatureException("Unsupported signature algorithm: " + signature.getAlgorithm()); } signature.base64Signature = Base64.getEncoder().encodeToString(signature.signatureBytes); return signature; } HttpSignature(String keyId, String algorithm, List<String> headers); private  HttpSignature(String header, String keyId,
                          String algorithm,
                          List<String> headers,
                          String base64Signature); }
HttpSignature { static HttpSignature sign(SecurityEnvironment env, OutboundTargetDefinition outboundDefinition, Map<String, List<String>> newHeaders) { HttpSignature signature = new HttpSignature(outboundDefinition.keyId(), outboundDefinition.algorithm(), outboundDefinition.signedHeadersConfig() .headers(env.method(), env.headers())); switch (signature.getAlgorithm()) { case HttpSignProvider.ALGORITHM_RSA: signature.signatureBytes = signature.signRsaSha256(env, outboundDefinition.keyConfig() .orElseThrow(() -> new HttpSignatureException( "Private key configuration must be present to use " + HttpSignProvider.ALGORITHM_RSA + " " + "algorithm")), newHeaders); break; case HttpSignProvider.ALGORITHM_HMAC: signature.signatureBytes = signature.signHmacSha256(env, outboundDefinition.hmacSharedSecret() .orElseThrow(() -> new HttpSignatureException( "HMAC shared secret must be configured to use " + HttpSignProvider.ALGORITHM_HMAC + " algorithm")), newHeaders); break; default: throw new HttpSignatureException("Unsupported signature algorithm: " + signature.getAlgorithm()); } signature.base64Signature = Base64.getEncoder().encodeToString(signature.signatureBytes); return signature; } HttpSignature(String keyId, String algorithm, List<String> headers); private  HttpSignature(String header, String keyId,
                          String algorithm,
                          List<String> headers,
                          String base64Signature);  }
HttpSignature { static HttpSignature sign(SecurityEnvironment env, OutboundTargetDefinition outboundDefinition, Map<String, List<String>> newHeaders) { HttpSignature signature = new HttpSignature(outboundDefinition.keyId(), outboundDefinition.algorithm(), outboundDefinition.signedHeadersConfig() .headers(env.method(), env.headers())); switch (signature.getAlgorithm()) { case HttpSignProvider.ALGORITHM_RSA: signature.signatureBytes = signature.signRsaSha256(env, outboundDefinition.keyConfig() .orElseThrow(() -> new HttpSignatureException( "Private key configuration must be present to use " + HttpSignProvider.ALGORITHM_RSA + " " + "algorithm")), newHeaders); break; case HttpSignProvider.ALGORITHM_HMAC: signature.signatureBytes = signature.signHmacSha256(env, outboundDefinition.hmacSharedSecret() .orElseThrow(() -> new HttpSignatureException( "HMAC shared secret must be configured to use " + HttpSignProvider.ALGORITHM_HMAC + " algorithm")), newHeaders); break; default: throw new HttpSignatureException("Unsupported signature algorithm: " + signature.getAlgorithm()); } signature.base64Signature = Base64.getEncoder().encodeToString(signature.signatureBytes); return signature; } HttpSignature(String keyId, String algorithm, List<String> headers); private  HttpSignature(String header, String keyId,
                          String algorithm,
                          List<String> headers,
                          String base64Signature);   }