Skip to content

Commit

Permalink
Merge pull request #407 from okta/fix_createConfig
Browse files Browse the repository at this point in the history
Fix createConfig on iOS and error handling on Android
  • Loading branch information
rajdeepnanua-okta authored Jan 18, 2024
2 parents cbf3a24 + c5e669e commit c93b61e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down
61 changes: 47 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class OktaStatusError extends Error {
}
}

export const createConfig = async({
/* eslint-disable max-params */
export function createConfigWithCallbacks(
issuer,
clientId,
redirectUri,
Expand All @@ -66,9 +67,10 @@ export const createConfig = async({
httpConnectionTimeout,
httpReadTimeout,
browserMatchAll = false,
oktaAuthConfig = {}
}) => {

oktaAuthConfig = {},
onSuccess,
onError
) {
assertIssuer(discoveryUri);
assertClientId(clientId);
assertRedirectUri(redirectUri);
Expand Down Expand Up @@ -112,12 +114,8 @@ export const createConfig = async({
scopes,
userAgentTemplate,
httpConnectionTimeout,
successResponse => {
return successResponse;
},
errorResponse => {
return errorResponse;
}
onSuccess,
onError
);
} else {

Expand All @@ -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 = () => {
Expand Down
2 changes: 1 addition & 1 deletion ios/OktaSdkBridge/OktaSdkBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down
4 changes: 2 additions & 2 deletions ios/OktaSdkBridge/ReactNativeOktaSdkBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c93b61e

Please sign in to comment.