Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refacftored OAuth2 #1324

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 21 additions & 32 deletions native/src/main/java/io/ballerina/stdlib/oauth2/OAuth2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf
String[] flatHeaders = headersList.toArray(String[]::new);
request = buildHttpRequest(uri, flatHeaders, textPayload);
}

if (secureSocket != null) {
try {
SSLContext sslContext = getSslContext(secureSocket);
Expand All @@ -104,20 +103,18 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf
} catch (Exception e) {
return createError("Failed to init SSL context. " + e.getMessage());
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove additional spaces. You can configure your IDE to remove the trailing spaces when saving a file.

Suggested change
}
}

HttpClient client = buildHttpClient(httpVersion);
return callEndpoint(client, request);
}

private static URI buildUri(String url, BMap<BString, ?> 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 != 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];
Expand All @@ -134,44 +131,41 @@ private static SSLContext getSslContext(BMap<BString, ?> 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) {
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<BString, BString> trustStore = (BMap<BString, BString>) 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;
throw new Exception("Failed to initialize SSLContext.");
}

private static HttpClient.Version getHttpVersion(String httpVersion) {
Expand Down Expand Up @@ -209,10 +203,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<BString, BString> trustStore) throws Exception {
Expand Down Expand Up @@ -250,14 +243,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 {
Expand Down
Loading