From c5e669e4c581abc07cf103018687a08518ef5c95 Mon Sep 17 00:00:00 2001 From: Rajdeep Nanua Date: Thu, 18 Jan 2024 11:55:53 -0500 Subject: [PATCH] Fix createConfig on iOS and error handling on Android --- .../oktareactnative/OktaSdkBridgeModule.java | 4 +- index.js | 61 ++++++++++++++----- ios/OktaSdkBridge/OktaSdkBridge.swift | 2 +- ios/OktaSdkBridge/ReactNativeOktaSdkBridge.m | 4 +- test/index.test.js | 10 ++- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java b/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java index dba9d2db..f02c97aa 100644 --- a/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java +++ b/android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java @@ -130,7 +130,7 @@ public void createConfig(String clientId, webAuthBuilder.withTabColor(Color.parseColor(androidChromeTabColor)); } catch (IllegalArgumentException e) { // The color wasn't in the right format. - errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e); + errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), getStackTraceString(e)); } } @@ -152,7 +152,7 @@ public void createConfig(String clientId, sessionClient = this.authClient.getSessionClient(); } } catch (Exception e) { - errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e); + errorCallback.invoke(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), getStackTraceString(e)); } } diff --git a/index.js b/index.js index de93619e..af727db0 100644 --- a/index.js +++ b/index.js @@ -54,7 +54,8 @@ class OktaStatusError extends Error { } } -export const createConfig = async({ +/* eslint-disable max-params */ +export function createConfigWithCallbacks( issuer, clientId, redirectUri, @@ -66,9 +67,10 @@ export const createConfig = async({ httpConnectionTimeout, httpReadTimeout, browserMatchAll = false, - oktaAuthConfig = {} -}) => { - + oktaAuthConfig = {}, + onSuccess, + onError +) { assertIssuer(discoveryUri); assertClientId(clientId); assertRedirectUri(redirectUri); @@ -112,12 +114,8 @@ export const createConfig = async({ scopes, userAgentTemplate, httpConnectionTimeout, - successResponse => { - return successResponse; - }, - errorResponse => { - return errorResponse; - } + onSuccess, + onError ); } else { @@ -137,14 +135,49 @@ export const createConfig = async({ androidChromeTabColor, timeouts, browserMatchAll, + onSuccess, + onError + ); + } +} +/* eslint-enable max-params */ + +export const createConfig = async({ + issuer, + clientId, + redirectUri, + endSessionRedirectUri, + discoveryUri, + scopes, + requireHardwareBackedKeyStore, + androidChromeTabColor, + httpConnectionTimeout, + httpReadTimeout, + browserMatchAll = false, + oktaAuthConfig = {} +}) => { + return await new Promise((resolve, reject) => { + createConfigWithCallbacks( + issuer, + clientId, + redirectUri, + endSessionRedirectUri, + discoveryUri, + scopes, + requireHardwareBackedKeyStore, + androidChromeTabColor, + httpConnectionTimeout, + httpReadTimeout, + browserMatchAll, + oktaAuthConfig, successResponse => { - return successResponse; + resolve(successResponse); }, - errorResponse => { - return errorResponse; + (errorCode, localizedMessage, stackTrace) => { + reject([errorCode, localizedMessage, stackTrace]); } ); - } + }); }; export const getAuthClient = () => { diff --git a/ios/OktaSdkBridge/OktaSdkBridge.swift b/ios/OktaSdkBridge/OktaSdkBridge.swift index 183aa1c0..909aa8b2 100644 --- a/ios/OktaSdkBridge/OktaSdkBridge.swift +++ b/ios/OktaSdkBridge/OktaSdkBridge.swift @@ -108,7 +108,7 @@ class OktaSdkBridge: RCTEventEmitter { successCallback([true]) } catch let error { - errorCallback([OktaReactNativeError.oktaOidcError.errorCode, error.localizedDescription, error]) + errorCallback([OktaReactNativeError.oktaOidcError.errorCode!, error.localizedDescription, Thread.callStackSymbols]) } } diff --git a/ios/OktaSdkBridge/ReactNativeOktaSdkBridge.m b/ios/OktaSdkBridge/ReactNativeOktaSdkBridge.m index 67538f0a..d19639ce 100644 --- a/ios/OktaSdkBridge/ReactNativeOktaSdkBridge.m +++ b/ios/OktaSdkBridge/ReactNativeOktaSdkBridge.m @@ -23,8 +23,8 @@ @interface RCT_EXTERN_MODULE(OktaSdkBridge, RCTEventEmitter) scopes:(NSString *)scopes userAgentTemplate:(NSString *)userAgentTemplate requestTimeout:(NSInteger)requestTimeout - promiseResolver:(RCTPromiseResolveBlock *)promiseResolver - promiseRejecter:(RCTPromiseRejectBlock *)promiseRejecter + successCallback:(RCTResponseSenderBlock *)successCallback + errorCallback:(RCTResponseSenderBlock *)errorCallback ) RCT_EXTERN_METHOD( diff --git a/test/index.test.js b/test/index.test.js index a5b2c23a..59610d53 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -132,7 +132,10 @@ describe('OktaReactNative', () => { mockConfig2: 'mock config 2' } }; - await createConfig(config); + let promise = createConfig(config); + let onSuccess = mockCreateConfig.mock.calls[0][7]; + onSuccess(true); + await promise; expect(OktaAuth).toHaveBeenCalledWith({ issuer: 'https://dummy_issuer', clientId: 'dummy_client_id', @@ -254,7 +257,10 @@ describe('OktaReactNative', () => { }); it('adds an environment to oktaAuth\'s _oktaUserAgent', async () => { - await createConfig(config); + let promise = createConfig(config); + let onSuccess = mockCreateConfig.mock.calls[0][10]; + onSuccess(true); + await promise; let mockOktaUserAgentAddEnvironment = getAuthClient()._oktaUserAgent.addEnvironment; expect(mockOktaUserAgentAddEnvironment).toHaveBeenCalledTimes(1);