From a62182445804d9d60ad0b9106113df8368119d41 Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Wed, 16 Oct 2024 15:09:40 -0400 Subject: [PATCH 1/5] Use OkHttp for networking --- .../com/oktareactnative/HttpClientImpl.java | 157 +++++++++--------- 1 file changed, 76 insertions(+), 81 deletions(-) diff --git a/android/src/main/java/com/oktareactnative/HttpClientImpl.java b/android/src/main/java/com/oktareactnative/HttpClientImpl.java index 4f947b6d..cc1fff90 100644 --- a/android/src/main/java/com/oktareactnative/HttpClientImpl.java +++ b/android/src/main/java/com/oktareactnative/HttpClientImpl.java @@ -12,37 +12,39 @@ package com.oktareactnative; -import android.annotation.SuppressLint; +import static com.okta.oidc.net.ConnectionParameters.USER_AGENT; + import android.net.Uri; -import android.os.Build; import androidx.annotation.NonNull; import com.okta.oidc.BuildConfig; import com.okta.oidc.net.ConnectionParameters; import com.okta.oidc.net.OktaHttpClient; -import com.okta.oidc.net.request.TLSSocketFactory; -import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; -import javax.net.ssl.HttpsURLConnection; - -import static com.okta.oidc.net.ConnectionParameters.USER_AGENT; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; public class HttpClientImpl implements OktaHttpClient { private final String userAgentTemplate; private final int connectTimeoutMs; private final int readTimeoutMs; - - private HttpURLConnection mUrlConnection; + protected static OkHttpClient sOkHttpClient; + protected volatile Call mCall; + protected Response mResponse; + protected Exception mException; HttpClientImpl(String userAgentTemplate, int connectTimeoutMs, int readTimeoutMs) { this.userAgentTemplate = userAgentTemplate; @@ -50,128 +52,121 @@ public class HttpClientImpl implements OktaHttpClient { this.readTimeoutMs = readTimeoutMs; } - /* - * TLS v1.1, v1.2 in Android supports starting from API 16. - * But it enabled by default starting from API 20. - * This method enable these TLS versions on API < 20. - * */ - @SuppressLint("RestrictedApi") - private void enableTlsV1_2(HttpURLConnection urlConnection) { - try { - ((HttpsURLConnection) urlConnection) - .setSSLSocketFactory(new TLSSocketFactory()); - } catch (NoSuchAlgorithmException | KeyManagementException e) { - throw new RuntimeException("Cannot create SSLContext.", e); - } - } - private String getUserAgent() { String sdkVersion = "okta-oidc-android/" + BuildConfig.VERSION_NAME; return userAgentTemplate.replace("$UPSTREAM_SDK", sdkVersion); } - protected HttpURLConnection openConnection(URL url, ConnectionParameters params) - throws IOException { - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - if (mUrlConnection instanceof HttpsURLConnection && - Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) { - enableTlsV1_2(mUrlConnection); + protected Request buildRequest(Uri uri, ConnectionParameters param) { + if (sOkHttpClient == null) { + sOkHttpClient = new OkHttpClient.Builder() + .connectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS) + .readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS) + .build(); } - - conn.setConnectTimeout(connectTimeoutMs); - conn.setReadTimeout(readTimeoutMs); - conn.setInstanceFollowRedirects(false); - - Map requestProperties = params.requestProperties(); - String userAgent = getUserAgent(); - requestProperties.put(USER_AGENT, userAgent); - for (String property : requestProperties.keySet()) { - conn.setRequestProperty(property, requestProperties.get(property)); + Request.Builder requestBuilder = new Request.Builder().url(uri.toString()); + requestBuilder.addHeader(USER_AGENT, getUserAgent()); + for (Map.Entry headerEntry : param.requestProperties().entrySet()) { + String key = headerEntry.getKey(); + requestBuilder.addHeader(key, headerEntry.getValue()); } - - ConnectionParameters.RequestMethod requestMethod = params.requestMethod(); - Map postParameters = params.postParameters(); - conn.setRequestMethod(requestMethod.name()); - if (requestMethod == ConnectionParameters.RequestMethod.GET) { - conn.setDoInput(true); - } else if (requestMethod == ConnectionParameters.RequestMethod.POST) { - conn.setDoOutput(true); - if (postParameters != null && !postParameters.isEmpty()) { - DataOutputStream out = new DataOutputStream(conn.getOutputStream()); - out.write(params.getEncodedPostParameters()); - out.close(); + if (param.requestMethod() == ConnectionParameters.RequestMethod.GET) { + requestBuilder = requestBuilder.get(); + } else { + Map postParameters = param.postParameters(); + if (postParameters != null) { + FormBody.Builder formBuilder = new FormBody.Builder(); + for (Map.Entry postEntry : postParameters.entrySet()) { + String key = postEntry.getKey(); + formBuilder.add(key, postEntry.getValue()); + } + RequestBody formBody = formBuilder.build(); + requestBuilder.post(formBody); + } else { + requestBuilder.post(RequestBody.create(null, "")); } } - return conn; + return requestBuilder.build(); } @Override public InputStream connect(@NonNull Uri uri, @NonNull ConnectionParameters params) throws Exception { + Request request = buildRequest(uri, params); + mCall = sOkHttpClient.newCall(request); + final CountDownLatch latch = new CountDownLatch(1); + mCall.enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + mException = e; + latch.countDown(); + } - mUrlConnection = openConnection(new URL(uri.toString()), params); - mUrlConnection.connect(); - try { - return mUrlConnection.getInputStream(); - } catch (IOException e) { - return mUrlConnection.getErrorStream(); + @Override + public void onResponse(Call call, Response response) { + mResponse = response; + latch.countDown(); + } + }); + latch.await(); + if (mException != null) { + throw mException; + } + if (mResponse != null && mResponse.body() != null) { + return mResponse.body().byteStream(); } + return null; } - @Override public void cleanUp() { - mUrlConnection = null; + //NO-OP } @Override public void cancel() { - if (mUrlConnection != null) { - mUrlConnection.disconnect(); + if (mCall != null) { + mCall.cancel(); } } @Override public Map> getHeaderFields() { - if (mUrlConnection != null) { - return mUrlConnection.getHeaderFields(); + if (mResponse != null) { + return mResponse.headers().toMultimap(); } return null; } @Override public String getHeader(String header) { - if (mUrlConnection != null) { - return mUrlConnection.getHeaderField(header); + if (mResponse != null) { + return mResponse.header(header); } return null; } @Override public int getResponseCode() throws IOException { - if (mUrlConnection != null) { - return mUrlConnection.getResponseCode(); + if (mResponse != null) { + return mResponse.code(); } return -1; } @Override public int getContentLength() { - if (mUrlConnection != null) { - return mUrlConnection.getContentLength(); + if (mResponse != null && mResponse.body() != null) { + return (int) mResponse.body().contentLength(); } return -1; } @Override public String getResponseMessage() throws IOException { - if (mUrlConnection != null) { - return mUrlConnection.getResponseMessage(); + if (mResponse != null) { + return mResponse.message(); } return null; } - - public HttpURLConnection getUrlConnection() { - return mUrlConnection; - } } From c22a513445fa528c467fe8107080c3fce1976af4 Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Wed, 16 Oct 2024 15:13:54 -0400 Subject: [PATCH 2/5] Use git lfs in circleci --- .circleci/config.yml | 3 +++ e2e/yarn.lock | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f9e9d6cc..ef337dc2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -36,6 +36,9 @@ jobs: install-yarn: true node-version: 'latest' - install_android_sdk + - run: brew install git-lfs + - run: git lfs install + - run: git lfs pull - run: yarn install --frozen-lockfile - run: yarn build - run: gem install cocoapods diff --git a/e2e/yarn.lock b/e2e/yarn.lock index 1d24494d..e36e3abe 100644 --- a/e2e/yarn.lock +++ b/e2e/yarn.lock @@ -2328,7 +2328,7 @@ __metadata: "@okta/okta-react-native@file:../dist::locator=e2e%40workspace%3A.": version: 2.13.0 - resolution: "@okta/okta-react-native@file:../dist#../dist::hash=83611a&locator=e2e%40workspace%3A." + resolution: "@okta/okta-react-native@file:../dist#../dist::hash=441271&locator=e2e%40workspace%3A." dependencies: "@babel/plugin-transform-async-to-generator": "npm:^7.24.7" "@okta/configuration-validation": "npm:^1.1.0" @@ -2340,7 +2340,7 @@ __metadata: events: ^3.1.0 react: ^16.13.0 || ^17.0.0 || ^18.0.0 react-native: ">=0.70.1" - checksum: 10c0/c5d598bff7cac6f3cd5bdf0a0e3342823523edba873a2f39e59a4e96b1baf8b840b429e4a687a39e4d0e856d881fb2750eaa6114565fa0df813e6de83c3158d4 + checksum: 10c0/349ed084c858372c50b7c17267057b94452fcddce32658f8801351a8b61a06362e468ddd2d5cfaf6c608110b6caef7ec59f045f5d563b21dd6653cfcc4a720d4 languageName: node linkType: hard From 75fd4463713e175599e6b52ec2869050d2e17188 Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Wed, 16 Oct 2024 15:58:47 -0400 Subject: [PATCH 3/5] Use git lfs in bacon --- scripts/setup.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/setup.sh b/scripts/setup.sh index 0c4efdac..cb67dd2e 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -16,6 +16,8 @@ if [ "$distro" = "centos" ]; then # Add yarn to the $PATH so npm cli commands do not fail export PATH="${PATH}:$(yarn global bin)" elif [ "$distro" = "amzn" ]; then + yum install -y git-lfs + git-lfs install npm install -g yarn export PATH="$PATH:$(npm config get prefix)/bin" else @@ -30,6 +32,7 @@ git checkout -- scripts # ensure we're in a branch on the correct sha git checkout $BRANCH +git-lfs pull git reset --hard $SHA git config --global user.email "oktauploader@okta.com" From a086cf542227d52c978f59a965f8625d40e815cb Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Wed, 16 Oct 2024 16:34:12 -0400 Subject: [PATCH 4/5] Update vuln libs and update publish script --- e2e/android/forceVersions.gradle | 1 + e2e/package.json | 1 - e2e/yarn.lock | 80 ++------------------------------ package.json | 1 - scripts/publish.sh | 6 +-- scripts/setup.sh | 5 ++ 6 files changed, 10 insertions(+), 84 deletions(-) diff --git a/e2e/android/forceVersions.gradle b/e2e/android/forceVersions.gradle index 0a70f40f..ce3b5a0c 100644 --- a/e2e/android/forceVersions.gradle +++ b/e2e/android/forceVersions.gradle @@ -14,6 +14,7 @@ def forceVersions(ConfigurationContainer configurations) { force 'junit:junit:4.13.2' force 'commons-io:commons-io:2.15.1' force 'commons-codec:commons-codec:1.17.0' + force 'io.netty:netty-common:4.1.93.Final' } } } diff --git a/e2e/package.json b/e2e/package.json index bad3a012..b6d7117b 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -24,7 +24,6 @@ "react-native-reanimated": "^3.11.0", "react-native-safe-area-context": "4.10.1", "react-native-screens": "~3.31.1", - "react-native-web": "~0.19.12", "semver": "^7.6.2" }, "resolutions": { diff --git a/e2e/yarn.lock b/e2e/yarn.lock index e36e3abe..0a29dbe4 100644 --- a/e2e/yarn.lock +++ b/e2e/yarn.lock @@ -1491,7 +1491,7 @@ __metadata: languageName: node linkType: hard -"@babel/runtime@npm:^7.0.0, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.16.0, @babel/runtime@npm:^7.18.6, @babel/runtime@npm:^7.20.0, @babel/runtime@npm:^7.6.2": +"@babel/runtime@npm:^7.0.0, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.16.0, @babel/runtime@npm:^7.20.0, @babel/runtime@npm:^7.6.2": version: 7.24.0 resolution: "@babel/runtime@npm:7.24.0" dependencies: @@ -2720,7 +2720,7 @@ __metadata: languageName: node linkType: hard -"@react-native/normalize-colors@npm:0.74.83, @react-native/normalize-colors@npm:^0.74.1, @react-native/normalize-colors@npm:~0.74.83": +"@react-native/normalize-colors@npm:0.74.83, @react-native/normalize-colors@npm:~0.74.83": version: 0.74.83 resolution: "@react-native/normalize-colors@npm:0.74.83" checksum: 10c0/e1821e395c289c8aea3748044d3d0e39f792123e58281b8ae2d805ed4ac5517071d5b6325da1ceb9f45444c5cdad2b3ac2a6cd302d4f4a247aed68e10dcfedee @@ -4030,15 +4030,6 @@ __metadata: languageName: node linkType: hard -"css-in-js-utils@npm:^3.1.0": - version: 3.1.0 - resolution: "css-in-js-utils@npm:3.1.0" - dependencies: - hyphenate-style-name: "npm:^1.0.3" - checksum: 10c0/8bb042e8f7701a7edadc3cce5ce2d5cf41189631d7e2aed194d5a7059b25776dded2a0466cb9da1d1f3fc6c99dcecb51e45671148d073b8a2a71e34755152e52 - languageName: node - linkType: hard - "dag-map@npm:~1.0.0": version: 1.0.2 resolution: "dag-map@npm:1.0.2" @@ -4242,7 +4233,6 @@ __metadata: react-native-reanimated: "npm:^3.11.0" react-native-safe-area-context: "npm:4.10.1" react-native-screens: "npm:~3.31.1" - react-native-web: "npm:~0.19.12" semver: "npm:^7.6.2" languageName: unknown linkType: soft @@ -4681,13 +4671,6 @@ __metadata: languageName: node linkType: hard -"fast-loops@npm:^1.1.3": - version: 1.1.3 - resolution: "fast-loops@npm:1.1.3" - checksum: 10c0/ba71c001704c44a617053ed34b1a8c0d2ed9723022eb7b93c98299d9862f93213609b32c9daec7d606625ab318769d11da8bb06e9ddd9c28e3bda1249fb6e36d - languageName: node - linkType: hard - "fast-text-encoding@npm:^1.0.6": version: 1.0.6 resolution: "fast-text-encoding@npm:1.0.6" @@ -4740,7 +4723,7 @@ __metadata: languageName: node linkType: hard -"fbjs@npm:^3.0.0, fbjs@npm:^3.0.4": +"fbjs@npm:^3.0.0": version: 3.0.5 resolution: "fbjs@npm:3.0.5" dependencies: @@ -5208,13 +5191,6 @@ __metadata: languageName: node linkType: hard -"hyphenate-style-name@npm:^1.0.3": - version: 1.0.4 - resolution: "hyphenate-style-name@npm:1.0.4" - checksum: 10c0/b19c3e2cd1dc426f6f893752fec08140abf79058a1b6238422e45373ed81389f02e1a2ba2ef4e9b2430d4e900a0f5ba12307de82320604e81ac1b722abd2ee62 - languageName: node - linkType: hard - "iconv-lite@npm:^0.6.2": version: 0.6.3 resolution: "iconv-lite@npm:0.6.3" @@ -5287,16 +5263,6 @@ __metadata: languageName: node linkType: hard -"inline-style-prefixer@npm:^6.0.1": - version: 6.0.4 - resolution: "inline-style-prefixer@npm:6.0.4" - dependencies: - css-in-js-utils: "npm:^3.1.0" - fast-loops: "npm:^1.1.3" - checksum: 10c0/d3d42bf0c48d621ea4bcfb077b5d370b106995422300a3a472674f96c9b489d96b4aac6f29dea3bb26ff2dfd7293e4752098bc2b53407769eafdb66c6c4c1764 - languageName: node - linkType: hard - "internal-ip@npm:4.3.0": version: 4.3.0 resolution: "internal-ip@npm:4.3.0" @@ -6226,13 +6192,6 @@ __metadata: languageName: node linkType: hard -"memoize-one@npm:^6.0.0": - version: 6.0.0 - resolution: "memoize-one@npm:6.0.0" - checksum: 10c0/45c88e064fd715166619af72e8cf8a7a17224d6edf61f7a8633d740ed8c8c0558a4373876c9b8ffc5518c2b65a960266adf403cc215cb1e90f7e262b58991f54 - languageName: node - linkType: hard - "memory-cache@npm:~0.2.0": version: 0.2.0 resolution: "memory-cache@npm:0.2.0" @@ -7313,13 +7272,6 @@ __metadata: languageName: node linkType: hard -"postcss-value-parser@npm:^4.2.0": - version: 4.2.0 - resolution: "postcss-value-parser@npm:4.2.0" - checksum: 10c0/f4142a4f56565f77c1831168e04e3effd9ffcc5aebaf0f538eee4b2d465adfd4b85a44257bb48418202a63806a7da7fe9f56c330aebb3cac898e46b4cbf49161 - languageName: node - linkType: hard - "postcss@npm:~8.4.32": version: 8.4.35 resolution: "postcss@npm:8.4.35" @@ -7665,25 +7617,6 @@ __metadata: languageName: node linkType: hard -"react-native-web@npm:~0.19.12": - version: 0.19.12 - resolution: "react-native-web@npm:0.19.12" - dependencies: - "@babel/runtime": "npm:^7.18.6" - "@react-native/normalize-colors": "npm:^0.74.1" - fbjs: "npm:^3.0.4" - inline-style-prefixer: "npm:^6.0.1" - memoize-one: "npm:^6.0.0" - nullthrows: "npm:^1.1.1" - postcss-value-parser: "npm:^4.2.0" - styleq: "npm:^0.1.3" - peerDependencies: - react: ^18.0.0 - react-dom: ^18.0.0 - checksum: 10c0/90f4221cebd834d550c477b00e5f972f8e3afbbea6708f8346fc394a38c8589a5d87d5e8be37a0ac421cc5e8a898cd94e0a6e6b2db982d3164e011f7ac9581da - languageName: node - linkType: hard - "react-native@npm:0.74.1": version: 0.74.1 resolution: "react-native@npm:0.74.1" @@ -8583,13 +8516,6 @@ __metadata: languageName: node linkType: hard -"styleq@npm:^0.1.3": - version: 0.1.3 - resolution: "styleq@npm:0.1.3" - checksum: 10c0/975d951792e65052f1f6e41aaad46492642ce4922b3dc36d4b49b37c8509f9a776794d8f275360f00116a5e6ab1e31514bdcd5840656c4e3213da6803fa12941 - languageName: node - linkType: hard - "sucrase@npm:3.34.0": version: 3.34.0 resolution: "sucrase@npm:3.34.0" diff --git a/package.json b/package.json index 4dd9d483..22294341 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,4 @@ { - "private": true, "name": "@okta/okta-react-native", "title": "React Native Okta Sdk Bridge", "version": "2.13.0", diff --git a/scripts/publish.sh b/scripts/publish.sh index c30e7eff..0b2b0b2d 100644 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -5,10 +5,6 @@ source $OKTA_HOME/$REPO/scripts/setup.sh export TEST_SUITE_TYPE="build" export REGISTRY="${ARTIFACTORY_URL}/api/npm/npm-topic" -# Install required dependencies -export PATH="${PATH}:$(yarn global bin)" -yarn global add @okta/ci-append-sha - if [ -n "${action_branch}" ]; then echo "Publishing from bacon task using branch ${action_branch}" @@ -20,7 +16,7 @@ fi pushd ./dist -if ! ci-append-sha; then +if ! yarn dlx @okta/ci-append-sha; then echo "ci-append-sha failed! Exiting..." exit ${FAILED_SETUP} fi diff --git a/scripts/setup.sh b/scripts/setup.sh index cb67dd2e..176276f7 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -42,3 +42,8 @@ if ! yarn install ; then echo "yarn install failed! Exiting..." exit ${FAILED_SETUP} fi + +if ! yarn build ; then + echo "yarn build failed! Exiting..." + exit ${FAILED_SETUP} +fi \ No newline at end of file From 68ae12b6c1afe3ccab4d981edbe26853b2fa7fab Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Wed, 16 Oct 2024 17:26:22 -0400 Subject: [PATCH 5/5] Prepare release 2.13.0 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 030370e2..f0ff393b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.13.0 + +### Other +- [#431](https://github.com/okta/okta-react-native/pull/431) Use OkHttp for networking calls in Android + # 2.12.0 ### Other