Skip to content

Commit

Permalink
feat: added support for domain switching
Browse files Browse the repository at this point in the history
  • Loading branch information
desusai7 committed Jul 19, 2024
1 parent 03fc5ed commit 417478b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
70 changes: 35 additions & 35 deletions android/src/main/java/com/auth0/react/A0Auth0Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public void initializeAuth0(String clientId, String domain) {
}

@ReactMethod
public void hasValidAuth0Instance(Promise promise) {
promise.resolve(this.auth0 != null && this.secureCredentialsManager != null);
public void hasValidAuth0Instance(String clientId, String domain, Promise promise) {
promise.resolve(this.auth0 != null && this.auth0.getClientId().equals(clientId) && this.auth0.getDomainUrl().contains(domain));
}

@ReactMethod
public void getCredentials(String scope, double minTtl, ReadableMap parameters, boolean forceRefresh, Promise promise) {
Map<String,String> cleanedParameters = new HashMap<>();
Map<String, String> cleanedParameters = new HashMap<>();
for (Map.Entry<String, Object> entry : parameters.toHashMap().entrySet()) {
if (entry.getValue() != null) {
cleanedParameters.put(entry.getKey(), entry.getValue().toString());
Expand Down Expand Up @@ -111,7 +111,7 @@ public void enableLocalAuthentication(String title, String description, Promise
try {
A0Auth0Module.this.secureCredentialsManager.requireAuthentication(activity, LOCAL_AUTH_REQUEST_CODE, title, description);
promise.resolve(true);
} catch (CredentialsManagerException e){
} catch (CredentialsManagerException e) {
promise.reject(ERROR_CODE, e.getMessage(), e);
}
});
Expand Down Expand Up @@ -144,69 +144,69 @@ public String getName() {
@ReactMethod
public void webAuth(String scheme, String redirectUri, String state, String nonce, String audience, String scope, String connection, int maxAge, String organization, String invitationUrl, int leeway, boolean ephemeralSession, int safariViewControllerPresentationStyle, ReadableMap additionalParameters, Promise promise) {
this.webAuthPromise = promise;
Map<String,String> cleanedParameters = new HashMap<>();
Map<String, String> cleanedParameters = new HashMap<>();
for (Map.Entry<String, Object> entry : additionalParameters.toHashMap().entrySet()) {
if (entry.getValue() != null) {
cleanedParameters.put(entry.getKey(), entry.getValue().toString());
}
}
WebAuthProvider.Builder builder = WebAuthProvider.login(this.auth0)
.withScheme(scheme);
if(state != null) {
if (state != null) {
builder.withState(state);
}
if(nonce != null) {
if (nonce != null) {
builder.withNonce(nonce);
}
if(audience != null) {
if (audience != null) {
builder.withAudience(audience);
}
if(scope != null) {
if (scope != null) {
builder.withScope(scope);
}
if(connection != null) {
if (connection != null) {
builder.withConnection(connection);
}
if(maxAge != 0) {
if (maxAge != 0) {
builder.withMaxAge(maxAge);
}
if(organization != null) {
if (organization != null) {
builder.withOrganization(organization);
}
if(invitationUrl != null) {
if (invitationUrl != null) {
builder.withInvitationUrl(invitationUrl);
}
if(leeway != 0) {
if (leeway != 0) {
builder.withIdTokenVerificationLeeway(leeway);
}
if(redirectUri != null) {
if (redirectUri != null) {
builder.withRedirectUri(redirectUri);
}
builder.withParameters(cleanedParameters);
builder.start(reactContext.getCurrentActivity(), new com.auth0.android.callback.Callback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials result) {
ReadableMap map = CredentialsParser.toMap(result);
promise.resolve(map);
webAuthPromise = null;
}
@Override
public void onSuccess(Credentials result) {
ReadableMap map = CredentialsParser.toMap(result);
promise.resolve(map);
webAuthPromise = null;
}

@Override
public void onFailure(@NonNull AuthenticationException error) {
handleError(error, promise);
webAuthPromise = null;
}
});
@Override
public void onFailure(@NonNull AuthenticationException error) {
handleError(error, promise);
webAuthPromise = null;
}
});
}

@ReactMethod
public void webAuthLogout(String scheme, boolean federated, String redirectUri, Promise promise) {
WebAuthProvider.LogoutBuilder builder = WebAuthProvider.logout(this.auth0)
.withScheme(scheme);
if(federated) {
if (federated) {
builder.withFederated();
}
if(redirectUri != null) {
if (redirectUri != null) {
builder.withReturnToUrl(redirectUri);
}
builder.start(reactContext.getCurrentActivity(), new com.auth0.android.callback.Callback<Void, AuthenticationException>() {
Expand All @@ -223,19 +223,19 @@ public void onFailure(AuthenticationException e) {
}

private void handleError(AuthenticationException error, Promise promise) {
if(error.isBrowserAppNotAvailable()) {
if (error.isBrowserAppNotAvailable()) {
promise.reject("a0.browser_not_available", "No Browser application is installed.", error);
return;
}
if(error.isCanceled()) {
if (error.isCanceled()) {
promise.reject("a0.session.user_cancelled", "User cancelled the Auth", error);
return;
}
if(error.isNetworkError()) {
if (error.isNetworkError()) {
promise.reject("a0.network_error", "Network error", error);
return;
}
if(error.isIdTokenValidationError()) {
if (error.isIdTokenValidationError()) {
promise.reject("a0.session.invalid_idtoken", "Error validating ID Token", error);
return;
}
Expand All @@ -245,14 +245,14 @@ private void handleError(AuthenticationException error, Promise promise) {

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if(requestCode == LOCAL_AUTH_REQUEST_CODE) {
if (requestCode == LOCAL_AUTH_REQUEST_CODE) {
secureCredentialsManager.checkAuthenticationResult(requestCode, resultCode);
}
}

@Override
public void onNewIntent(Intent intent) {
if(webAuthPromise != null) {
if (webAuthPromise != null) {
webAuthPromise.reject("a0.session.browser_terminated", "The browser window was closed by a new instance of the application");
webAuthPromise = null;
}
Expand Down
8 changes: 4 additions & 4 deletions ios/A0Auth0.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ - (dispatch_queue_t)methodQueue

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(hasValidAuth0Instance:(RCTPromiseResolveBlock)resolve
RCT_EXPORT_METHOD(hasValidAuth0Instance:(NSString *)clientId domain:(NSString *)domain resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
BOOL valid = [self checkHasValidNativeBridgeInstance];
BOOL valid = [self checkHasValidNativeBridgeInstance:clientId domain:domain];
resolve(@(valid));
}

Expand Down Expand Up @@ -74,8 +74,8 @@ + (BOOL)requiresMainQueueSetup {

UIBackgroundTaskIdentifier taskId;

- (BOOL)checkHasValidNativeBridgeInstance {
BOOL valid = self.nativeBridge != nil;
- (BOOL)checkHasValidNativeBridgeInstance:(NSString*) clientId domain:(NSString *)domain {
BOOL valid = self.nativeBridge != nil && [self.nativeBridge.getClientId isEqual:clientId] && [self.nativeBridge.getDomain isEqual:domain];
return valid;
}

Expand Down
8 changes: 8 additions & 0 deletions ios/NativeBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ public class NativeBridge: NSObject {
let policyValue = self.convert(policyInt: evaluationPolicy)
self.credentialsManager.enableBiometrics(withTitle: titleValue, cancelTitle: cancelTitle, fallbackTitle: fallbackTitle, evaluationPolicy: policyValue)
}

@objc public func getClientId() -> String {
return clientId
}

@objc public func getDomain() -> String {
return domain
}

func convert(policyInt: Int) -> LAPolicy {
if (policyInt == 2) {
Expand Down
2 changes: 1 addition & 1 deletion src/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export type Auth0Module = {
| ((title?: string, description?: string) => Promise<void>);
hasValidCredentials: (minTtl?: number) => Promise<boolean>;
clearCredentials: () => Promise<void>;
hasValidAuth0Instance: () => Promise<boolean>;
hasValidAuth0Instance: (clientId: String, domain: String) => Promise<boolean>;
initializeAuth0: (clientId: string, domain: string) => Promise<void>;
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/nativeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function _ensureNativeModuleIsInitialized(
clientId: string,
domain: string
) {
const hasValid = await nativeModule.hasValidAuth0Instance();
const hasValid = await nativeModule.hasValidAuth0Instance(clientId, domain);
if (!hasValid) {
await nativeModule.initializeAuth0(clientId, domain);
}
Expand Down

0 comments on commit 417478b

Please sign in to comment.