From 891eeefeb4e5aaf86e70c92793eb405fc38ee448 Mon Sep 17 00:00:00 2001 From: ritikk112 Date: Mon, 14 Oct 2024 00:29:06 +0530 Subject: [PATCH 1/3] refactored OAuth2 --- .../ballerina/stdlib/oauth2/OAuth2Client.java | 50 ++++++++----------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java b/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java index 1ef5ddbc..1020e710 100644 --- a/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java +++ b/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java @@ -25,6 +25,7 @@ import io.ballerina.runtime.api.values.BString; import io.ballerina.stdlib.crypto.nativeimpl.Decode; +import java.util.Optional; import java.io.FileInputStream; import java.io.IOException; import java.net.URI; @@ -70,7 +71,7 @@ public static Object doHttpRequest(BString url, BMap clientConf headersList.add(entry.getValue().getValue()); } - BMap customHeaders = getBMapValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_HEADERS); + Optional> customHeaders = getBMapValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_HEADERS); if (customHeaders != null) { for (Map.Entry entry : customHeaders.entrySet()) { headersList.add(entry.getKey().getValue()); @@ -79,7 +80,7 @@ public static Object doHttpRequest(BString url, BMap clientConf } String httpVersion = getBStringValueIfPresent(clientConfig, OAuth2Constants.HTTP_VERSION).getValue(); - BMap secureSocket = getBMapValueIfPresent(clientConfig, OAuth2Constants.SECURE_SOCKET); + Optional> secureSocket = getBMapValueIfPresent(clientConfig, OAuth2Constants.SECURE_SOCKET); HttpRequest request; URI uri; @@ -114,10 +115,8 @@ private static URI buildUri(String url, BMap secureSocket) throws Il if (urlParts.length == 1) { urlParts = secureSocket != null ? new String[]{OAuth2Constants.HTTPS_SCHEME, urlParts[0]} : new String[]{OAuth2Constants.HTTP_SCHEME, urlParts[0]}; - } else { - if (urlParts[0].equals(OAuth2Constants.HTTP_SCHEME) && secureSocket != null) { - err.println(OAuth2Constants.RUNTIME_WARNING_PREFIX + OAuth2Constants.HTTPS_RECOMMENDATION_ERROR); - } + } else if (urlParts[0].equals(OAuth2Constants.HTTP_SCHEME) && secureSocket != null){ + err.println(OAuth2Constants.RUNTIME_WARNING_PREFIX + OAuth2Constants.HTTPS_RECOMMENDATION_ERROR); } urlParts[1] = urlParts[1].replaceAll(OAuth2Constants.DOUBLE_SLASH, OAuth2Constants.SINGLE_SLASH); url = urlParts[0] + OAuth2Constants.SCHEME_SEPARATOR + urlParts[1]; @@ -129,7 +128,7 @@ private static SSLContext getSslContext(BMap secureSocket) throws Ex if (disable) { return initSslContext(); } - BMap key = (BMap) getBMapValueIfPresent(secureSocket, OAuth2Constants.KEY); + Optional> key = Optional.ofNullable(getBMapValueIfPresent(secureSocket, OAuth2Constants.KEY)); Object cert = secureSocket.get(OAuth2Constants.CERT); if (cert == null) { throw new Exception("Need to configure 'crypto:TrustStore' or 'cert' with client SSL certificates file."); @@ -138,43 +137,41 @@ private static SSLContext getSslContext(BMap secureSocket) throws Ex TrustManagerFactory tmf; if (cert instanceof BString) { if (key != null) { + tmf = getTrustManagerFactory((BString) cert); if (key.containsKey(OAuth2Constants.CERT_FILE)) { BString certFile = key.get(OAuth2Constants.CERT_FILE); BString keyFile = key.get(OAuth2Constants.KEY_FILE); BString keyPassword = getBStringValueIfPresent(key, OAuth2Constants.KEY_PASSWORD); kmf = getKeyManagerFactory(certFile, keyFile, keyPassword); - } else { - kmf = getKeyManagerFactory(key); + return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } - tmf = getTrustManagerFactory((BString) cert); + kmf = getKeyManagerFactory(key); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); - } else { - tmf = getTrustManagerFactory((BString) cert); - return buildSslContext(null, tmf.getTrustManagers()); } + tmf = getTrustManagerFactory((BString) cert); + return buildSslContext(null, tmf.getTrustManagers()); } if (cert instanceof BMap) { - BMap trustStore = (BMap) cert; + Optional> trustStore = (BMap) cert; if (key != null) { + tmf = getTrustManagerFactory(trustStore); if (key.containsKey(OAuth2Constants.CERT_FILE)) { BString certFile = key.get(OAuth2Constants.CERT_FILE); BString keyFile = key.get(OAuth2Constants.KEY_FILE); BString keyPassword = getBStringValueIfPresent(key, OAuth2Constants.KEY_PASSWORD); kmf = getKeyManagerFactory(certFile, keyFile, keyPassword); - } else { - kmf = getKeyManagerFactory(key); + return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } - tmf = getTrustManagerFactory(trustStore); + kmf = getKeyManagerFactory(key); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); - } else { - tmf = getTrustManagerFactory(trustStore); - return buildSslContext(null, tmf.getTrustManagers()); } + return buildSslContext(null, tmf.getTrustManagers()); } return null; } private static HttpClient.Version getHttpVersion(String httpVersion) { + // return (OAuth2Constants.HTTP_2.equals(httpVersion))?HttpClient.Version.HTTP_2 : HttpClient.Version.HTTP_1_1 if (OAuth2Constants.HTTP_2.equals(httpVersion)) { return HttpClient.Version.HTTP_2; } @@ -209,10 +206,9 @@ private static TrustManagerFactory getTrustManagerFactory(BString cert) throws E TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ts); return tmf; - } else { - throw new Exception("Failed to get the public key from Crypto API. " + - ((BError) publicKeyMap).getErrorMessage().getValue()); } + throw new Exception("Failed to get the public key from Crypto API. " + + ((BError) publicKeyMap).getErrorMessage().getValue()); } private static TrustManagerFactory getTrustManagerFactory(BMap trustStore) throws Exception { @@ -250,14 +246,10 @@ private static KeyManagerFactory getKeyManagerFactory(BString certFile, BString KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, "".toCharArray()); return kmf; - } else { - throw new Exception("Failed to get the private key from Crypto API. " + - ((BError) privateKeyMap).getErrorMessage().getValue()); } - } else { - throw new Exception("Failed to get the public key from Crypto API. " + - ((BError) publicKey).getErrorMessage().getValue()); } + throw new Exception("Failed to get the public key from Crypto API. " + + ((BError) publicKey).getErrorMessage().getValue()); } private static KeyStore getKeyStore(BString path, BString password) throws Exception { From b2872aea2bc07d123bb7aea5c0777eaeb7307f88 Mon Sep 17 00:00:00 2001 From: ritikk112 Date: Mon, 14 Oct 2024 04:16:26 +0530 Subject: [PATCH 2/3] refactored oauth --- .../ballerina/stdlib/oauth2/OAuth2Client.java | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java b/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java index 1020e710..47049874 100644 --- a/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java +++ b/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java @@ -72,12 +72,12 @@ public static Object doHttpRequest(BString url, BMap clientConf } Optional> customHeaders = getBMapValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_HEADERS); - if (customHeaders != null) { + customHeaders.ifPresent( customHeaders -> { for (Map.Entry entry : customHeaders.entrySet()) { headersList.add(entry.getKey().getValue()); headersList.add(((BString) entry.getValue()).getValue()); } - } + }); String httpVersion = getBStringValueIfPresent(clientConfig, OAuth2Constants.HTTP_VERSION).getValue(); Optional> secureSocket = getBMapValueIfPresent(clientConfig, OAuth2Constants.SECURE_SOCKET); @@ -96,26 +96,25 @@ public static Object doHttpRequest(BString url, BMap clientConf String[] flatHeaders = headersList.toArray(String[]::new); request = buildHttpRequest(uri, flatHeaders, textPayload); } - - if (secureSocket != null) { + if (secureSocket.isPresent()) { try { - SSLContext sslContext = getSslContext(secureSocket); + SSLContext sslContext = getSslContext(secureSocket.get()); HttpClient client = buildHttpClient(httpVersion, sslContext); return callEndpoint(client, request); } catch (Exception e) { return createError("Failed to init SSL context. " + e.getMessage()); } - } + } HttpClient client = buildHttpClient(httpVersion); return callEndpoint(client, request); } - private static URI buildUri(String url, BMap secureSocket) throws IllegalArgumentException { + private static URI buildUri(String url, Optional> secureSocket) throws IllegalArgumentException { String[] urlParts = url.split(OAuth2Constants.SCHEME_SEPARATOR, 2); if (urlParts.length == 1) { - urlParts = secureSocket != null ? new String[]{OAuth2Constants.HTTPS_SCHEME, urlParts[0]} : + urlParts = secureSocket.isPresent() ? new String[]{OAuth2Constants.HTTPS_SCHEME, urlParts[0]} : new String[]{OAuth2Constants.HTTP_SCHEME, urlParts[0]}; - } else if (urlParts[0].equals(OAuth2Constants.HTTP_SCHEME) && secureSocket != null){ + } else if (urlParts[0].equals(OAuth2Constants.HTTP_SCHEME) && secureSocket.isPresent()){ err.println(OAuth2Constants.RUNTIME_WARNING_PREFIX + OAuth2Constants.HTTPS_RECOMMENDATION_ERROR); } urlParts[1] = urlParts[1].replaceAll(OAuth2Constants.DOUBLE_SLASH, OAuth2Constants.SINGLE_SLASH); @@ -133,45 +132,44 @@ private static SSLContext getSslContext(BMap secureSocket) throws Ex if (cert == null) { throw new Exception("Need to configure 'crypto:TrustStore' or 'cert' with client SSL certificates file."); } - KeyManagerFactory kmf; - TrustManagerFactory tmf; + KeyManagerFactory kmf = null; + TrustManagerFactory tmf = null; if (cert instanceof BString) { - if (key != null) { + if (key.isPresent()) { tmf = getTrustManagerFactory((BString) cert); - if (key.containsKey(OAuth2Constants.CERT_FILE)) { - BString certFile = key.get(OAuth2Constants.CERT_FILE); - BString keyFile = key.get(OAuth2Constants.KEY_FILE); - BString keyPassword = getBStringValueIfPresent(key, OAuth2Constants.KEY_PASSWORD); + if (key.get().containsKey(OAuth2Constants.CERT_FILE)) { + BString certFile = key.get().get(OAuth2Constants.CERT_FILE); + BString keyFile = key.get().get(OAuth2Constants.KEY_FILE); + BString keyPassword = getBStringValueIfPresent(key.get(), OAuth2Constants.KEY_PASSWORD); kmf = getKeyManagerFactory(certFile, keyFile, keyPassword); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } - kmf = getKeyManagerFactory(key); + kmf = getKeyManagerFactory(key.get()); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); - } + } tmf = getTrustManagerFactory((BString) cert); return buildSslContext(null, tmf.getTrustManagers()); } if (cert instanceof BMap) { - Optional> trustStore = (BMap) cert; - if (key != null) { + BMap trustStore = (BMap) cert; + if(key.isPresent()){ tmf = getTrustManagerFactory(trustStore); - if (key.containsKey(OAuth2Constants.CERT_FILE)) { + if (key.get().containsKey(OAuth2Constants.CERT_FILE)) { BString certFile = key.get(OAuth2Constants.CERT_FILE); BString keyFile = key.get(OAuth2Constants.KEY_FILE); BString keyPassword = getBStringValueIfPresent(key, OAuth2Constants.KEY_PASSWORD); kmf = getKeyManagerFactory(certFile, keyFile, keyPassword); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } - kmf = getKeyManagerFactory(key); + kmf = getKeyManagerFactory(key.get()); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } return buildSslContext(null, tmf.getTrustManagers()); } - return null; + throw new Exception("Failed to initialize SSLContext."); } private static HttpClient.Version getHttpVersion(String httpVersion) { - // return (OAuth2Constants.HTTP_2.equals(httpVersion))?HttpClient.Version.HTTP_2 : HttpClient.Version.HTTP_1_1 if (OAuth2Constants.HTTP_2.equals(httpVersion)) { return HttpClient.Version.HTTP_2; } @@ -301,6 +299,9 @@ private static Object callEndpoint(HttpClient client, HttpRequest request) { return createError("Failed to get a success response from the endpoint. Response code: '" + response.statusCode() + "'. Response body: '" + response.body() + "'"); } catch (IOException | InterruptedException e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); // Restore interrupted status + } return createError("Failed to send the request to the endpoint. " + e.getMessage()); } } From ce81c04f43dc7e66ef9f117c69276109ca13efe8 Mon Sep 17 00:00:00 2001 From: ritikk112 Date: Tue, 15 Oct 2024 18:14:44 +0530 Subject: [PATCH 3/3] Refactored with minor adjustments --- .../ballerina/stdlib/oauth2/OAuth2Client.java | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java b/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java index 47049874..b686cc7a 100644 --- a/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java +++ b/native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java @@ -25,7 +25,6 @@ import io.ballerina.runtime.api.values.BString; import io.ballerina.stdlib.crypto.nativeimpl.Decode; -import java.util.Optional; import java.io.FileInputStream; import java.io.IOException; import java.net.URI; @@ -71,16 +70,16 @@ public static Object doHttpRequest(BString url, BMap clientConf headersList.add(entry.getValue().getValue()); } - Optional> customHeaders = getBMapValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_HEADERS); - customHeaders.ifPresent( customHeaders -> { + BMap customHeaders = getBMapValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_HEADERS); + if(customHeaders != null){ for (Map.Entry entry : customHeaders.entrySet()) { headersList.add(entry.getKey().getValue()); headersList.add(((BString) entry.getValue()).getValue()); } - }); + } String httpVersion = getBStringValueIfPresent(clientConfig, OAuth2Constants.HTTP_VERSION).getValue(); - Optional> secureSocket = getBMapValueIfPresent(clientConfig, OAuth2Constants.SECURE_SOCKET); + BMap secureSocket = getBMapValueIfPresent(clientConfig, OAuth2Constants.SECURE_SOCKET); HttpRequest request; URI uri; @@ -96,9 +95,9 @@ public static Object doHttpRequest(BString url, BMap clientConf String[] flatHeaders = headersList.toArray(String[]::new); request = buildHttpRequest(uri, flatHeaders, textPayload); } - if (secureSocket.isPresent()) { + if (secureSocket != null) { try { - SSLContext sslContext = getSslContext(secureSocket.get()); + SSLContext sslContext = getSslContext(secureSocket); HttpClient client = buildHttpClient(httpVersion, sslContext); return callEndpoint(client, request); } catch (Exception e) { @@ -109,12 +108,12 @@ public static Object doHttpRequest(BString url, BMap clientConf return callEndpoint(client, request); } - private static URI buildUri(String url, Optional> secureSocket) throws IllegalArgumentException { + private static URI buildUri(String url, BMap secureSocket) throws IllegalArgumentException { String[] urlParts = url.split(OAuth2Constants.SCHEME_SEPARATOR, 2); if (urlParts.length == 1) { - urlParts = secureSocket.isPresent() ? new String[]{OAuth2Constants.HTTPS_SCHEME, urlParts[0]} : + urlParts = (secureSocket!=null) ? new String[]{OAuth2Constants.HTTPS_SCHEME, urlParts[0]} : new String[]{OAuth2Constants.HTTP_SCHEME, urlParts[0]}; - } else if (urlParts[0].equals(OAuth2Constants.HTTP_SCHEME) && secureSocket.isPresent()){ + } else if (urlParts[0].equals(OAuth2Constants.HTTP_SCHEME) && secureSocket != null){ err.println(OAuth2Constants.RUNTIME_WARNING_PREFIX + OAuth2Constants.HTTPS_RECOMMENDATION_ERROR); } urlParts[1] = urlParts[1].replaceAll(OAuth2Constants.DOUBLE_SLASH, OAuth2Constants.SINGLE_SLASH); @@ -127,7 +126,7 @@ private static SSLContext getSslContext(BMap secureSocket) throws Ex if (disable) { return initSslContext(); } - Optional> key = Optional.ofNullable(getBMapValueIfPresent(secureSocket, OAuth2Constants.KEY)); + BMap key = (BMap) getBMapValueIfPresent(secureSocket, OAuth2Constants.KEY); Object cert = secureSocket.get(OAuth2Constants.CERT); if (cert == null) { throw new Exception("Need to configure 'crypto:TrustStore' or 'cert' with client SSL certificates file."); @@ -135,16 +134,16 @@ private static SSLContext getSslContext(BMap secureSocket) throws Ex KeyManagerFactory kmf = null; TrustManagerFactory tmf = null; if (cert instanceof BString) { - if (key.isPresent()) { + if (key != null) { tmf = getTrustManagerFactory((BString) cert); - if (key.get().containsKey(OAuth2Constants.CERT_FILE)) { - BString certFile = key.get().get(OAuth2Constants.CERT_FILE); - BString keyFile = key.get().get(OAuth2Constants.KEY_FILE); - BString keyPassword = getBStringValueIfPresent(key.get(), OAuth2Constants.KEY_PASSWORD); + if (key.containsKey(OAuth2Constants.CERT_FILE)) { + BString certFile = key.get(OAuth2Constants.CERT_FILE); + BString keyFile = key.get(OAuth2Constants.KEY_FILE); + BString keyPassword = getBStringValueIfPresent(key, OAuth2Constants.KEY_PASSWORD); kmf = getKeyManagerFactory(certFile, keyFile, keyPassword); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } - kmf = getKeyManagerFactory(key.get()); + kmf = getKeyManagerFactory(key); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } tmf = getTrustManagerFactory((BString) cert); @@ -152,16 +151,16 @@ private static SSLContext getSslContext(BMap secureSocket) throws Ex } if (cert instanceof BMap) { BMap trustStore = (BMap) cert; - if(key.isPresent()){ + if(key != null){ tmf = getTrustManagerFactory(trustStore); - if (key.get().containsKey(OAuth2Constants.CERT_FILE)) { + if (key.containsKey(OAuth2Constants.CERT_FILE)) { BString certFile = key.get(OAuth2Constants.CERT_FILE); BString keyFile = key.get(OAuth2Constants.KEY_FILE); BString keyPassword = getBStringValueIfPresent(key, OAuth2Constants.KEY_PASSWORD); kmf = getKeyManagerFactory(certFile, keyFile, keyPassword); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } - kmf = getKeyManagerFactory(key.get()); + kmf = getKeyManagerFactory(key); return buildSslContext(kmf.getKeyManagers(), tmf.getTrustManagers()); } return buildSslContext(null, tmf.getTrustManagers()); @@ -299,9 +298,6 @@ private static Object callEndpoint(HttpClient client, HttpRequest request) { return createError("Failed to get a success response from the endpoint. Response code: '" + response.statusCode() + "'. Response body: '" + response.body() + "'"); } catch (IOException | InterruptedException e) { - if (e instanceof InterruptedException) { - Thread.currentThread().interrupt(); // Restore interrupted status - } return createError("Failed to send the request to the endpoint. " + e.getMessage()); } }