From c55d39b87be874c31170779fe2b30f1f471fac1f Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Tue, 16 Jan 2024 14:00:00 -0500 Subject: [PATCH 1/2] Use native callbacks instead of promise in createConfig --- .../oktareactnative/OktaSdkBridgeModule.java | 10 ++++++---- index.js | 18 +++++++++++++++--- ios/OktaSdkBridge/OktaSdkBridge.swift | 8 ++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java b/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java index 051952e3..dba9d2db 100644 --- a/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java +++ b/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java @@ -27,6 +27,7 @@ import com.facebook.react.bridge.ActivityEventListener; import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; @@ -99,7 +100,8 @@ public void createConfig(String clientId, String androidChromeTabColor, ReadableMap timeouts, Boolean browserMatchAll, - Promise promise + Callback successCallback, + Callback errorCallback ) { try { @@ -128,7 +130,7 @@ public void createConfig(String clientId, webAuthBuilder.withTabColor(Color.parseColor(androidChromeTabColor)); } catch (IllegalArgumentException e) { // The color wasn't in the right format. - promise.reject(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e); + errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e); } } @@ -142,7 +144,7 @@ public void createConfig(String clientId, configureBuilder(authClientBuilder, userAgentTemplate, requireHardwareBackedKeyStore, connectTimeout, readTimeout); this.authClient = authClientBuilder.create(); - promise.resolve(true); + successCallback.invoke(true); if (SESSION_CLIENT_WEB.equals(sharedPreferences.getString(PREFS_KEY, SESSION_CLIENT_WEB))) { sessionClient = this.webClient.getSessionClient(); @@ -150,7 +152,7 @@ public void createConfig(String clientId, sessionClient = this.authClient.getSessionClient(); } } catch (Exception e) { - promise.reject(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e); + errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e); } } diff --git a/index.js b/index.js index 56062317..62305eff 100644 --- a/index.js +++ b/index.js @@ -104,7 +104,7 @@ export const createConfig = async({ if (Platform.OS === 'ios') { scopes = scopes.join(' '); - return NativeModules.OktaSdkBridge.createConfig( + NativeModules.OktaSdkBridge.createConfig( clientId, redirectUri, endSessionRedirectUri, @@ -112,7 +112,13 @@ export const createConfig = async({ scopes, userAgentTemplate, httpConnectionTimeout, - ); + successCallback => { + return successCallback + }, + errorCallback => { + return errorCallback + } + ) } const timeouts = { @@ -120,7 +126,7 @@ export const createConfig = async({ httpReadTimeout, }; - return NativeModules.OktaSdkBridge.createConfig( + NativeModules.OktaSdkBridge.createConfig( clientId, redirectUri, endSessionRedirectUri, @@ -131,6 +137,12 @@ export const createConfig = async({ androidChromeTabColor, timeouts, browserMatchAll, + successCallback => { + return successCallback + }, + errorCallback => { + return errorCallback + } ); }; diff --git a/ios/OktaSdkBridge/OktaSdkBridge.swift b/ios/OktaSdkBridge/OktaSdkBridge.swift index 2169b1da..183aa1c0 100644 --- a/ios/OktaSdkBridge/OktaSdkBridge.swift +++ b/ios/OktaSdkBridge/OktaSdkBridge.swift @@ -87,8 +87,8 @@ class OktaSdkBridge: RCTEventEmitter { scopes: String, userAgentTemplate: String, requestTimeout: Int, - promiseResolver: RCTPromiseResolveBlock, - promiseRejecter: RCTPromiseRejectBlock) { + successCallback: RCTResponseSenderBlock, + errorCallback: RCTResponseSenderBlock) { do { let uaVersion = OktaUserAgent.userAgentVersion() let userAgent = userAgentTemplate.replacingOccurrences(of: "$UPSTREAM_SDK", with: "okta-oidc-ios/\(uaVersion)") @@ -106,9 +106,9 @@ class OktaSdkBridge: RCTEventEmitter { oktaOidc = try OktaOidc(configuration: config) self.requestTimeout = requestTimeout - promiseResolver(true) + successCallback([true]) } catch let error { - promiseRejecter(OktaReactNativeError.oktaOidcError.errorCode, error.localizedDescription, error) + errorCallback([OktaReactNativeError.oktaOidcError.errorCode, error.localizedDescription, error]) } } From fd592da9d60b52946fc79bc6fb3fdcfd5fec63ad Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Tue, 16 Jan 2024 14:41:48 -0500 Subject: [PATCH 2/2] Fix tests --- index.js | 59 +++++++++++++++--------------- ios/Tests/OktaSdkBridgeTests.swift | 2 +- test/index.test.js | 10 +++++ 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 62305eff..de93619e 100644 --- a/index.js +++ b/index.js @@ -112,38 +112,39 @@ export const createConfig = async({ scopes, userAgentTemplate, httpConnectionTimeout, - successCallback => { - return successCallback + successResponse => { + return successResponse; }, - errorCallback => { - return errorCallback + errorResponse => { + return errorResponse; } - ) - } + ); + } else { - const timeouts = { - httpConnectionTimeout, - httpReadTimeout, - }; - - NativeModules.OktaSdkBridge.createConfig( - clientId, - redirectUri, - endSessionRedirectUri, - discoveryUri, - scopes, - userAgentTemplate, - requireHardwareBackedKeyStore, - androidChromeTabColor, - timeouts, - browserMatchAll, - successCallback => { - return successCallback - }, - errorCallback => { - return errorCallback - } - ); + const timeouts = { + httpConnectionTimeout, + httpReadTimeout, + }; + + NativeModules.OktaSdkBridge.createConfig( + clientId, + redirectUri, + endSessionRedirectUri, + discoveryUri, + scopes, + userAgentTemplate, + requireHardwareBackedKeyStore, + androidChromeTabColor, + timeouts, + browserMatchAll, + successResponse => { + return successResponse; + }, + errorResponse => { + return errorResponse; + } + ); + } }; export const getAuthClient = () => { diff --git a/ios/Tests/OktaSdkBridgeTests.swift b/ios/Tests/OktaSdkBridgeTests.swift index 3e0bd2af..b80f52e0 100644 --- a/ios/Tests/OktaSdkBridgeTests.swift +++ b/ios/Tests/OktaSdkBridgeTests.swift @@ -63,7 +63,7 @@ final class OktaSdkBridgeTests: XCTestCase { requestTimeout: 20) { (result) in XCTAssertNotNil(result) expectation.fulfill() - } promiseRejecter: { (_, _, _) in + } errorCallback: { (_) in XCTAssert(false, "createConfig failed.") } diff --git a/test/index.test.js b/test/index.test.js index ad26c60f..a5b2c23a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -158,6 +158,8 @@ describe('OktaReactNative', () => { processedScope, `okta-react-native/${version} $UPSTREAM_SDK react-native/${reactNativeVersion} ios/1.0.0`, defaultTimeouts.httpConnectionTimeout, + expect.any(Function), // Native bridge success callback + expect.any(Function) // Native bridge error callback ); }); @@ -177,6 +179,8 @@ describe('OktaReactNative', () => { undefined, defaultTimeouts, false, + expect.any(Function), // Native bridge success callback + expect.any(Function) // Native bridge error callback ); }); @@ -198,6 +202,8 @@ describe('OktaReactNative', () => { '#FF00AA', defaultTimeouts, false, + expect.any(Function), // Native bridge success callback + expect.any(Function) // Native bridge error callback ); }); @@ -219,6 +225,8 @@ describe('OktaReactNative', () => { undefined, defaultTimeouts, true, + expect.any(Function), // Native bridge success callback + expect.any(Function) // Native bridge error callback ); }); @@ -240,6 +248,8 @@ describe('OktaReactNative', () => { undefined, { httpConnectionTimeout: 12, httpReadTimeout: 34 }, false, + expect.any(Function), // Native bridge success callback + expect.any(Function) // Native bridge error callback ); });