Skip to content

Commit

Permalink
[flutter_appauth] fix performance issue on Android (MaikuB#135)
Browse files Browse the repository at this point in the history
* improve performance on android

* bump plugin version

* update changelog to include link to issue that was fixed
  • Loading branch information
MaikuB authored Aug 25, 2020
1 parent 674f11c commit 020a19d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
4 changes: 4 additions & 0 deletions flutter_appauth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.2+3

* [Android] fix issue [79](https://github.com/MaikuB/flutter_appauth/issues/79) where an authorisation request could cause the UI to momentarily hang due to AppAuth trying to warm up the browser

## 0.9.2+2

* Updated example app to use a new client id that works with the demo IdentityServer instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.view.FlutterNativeView;

/**
* FlutterAppauthPlugin
Expand All @@ -57,6 +58,8 @@ public class FlutterAppauthPlugin implements FlutterPlugin, MethodCallHandler, P
private PendingOperation pendingOperation;
private String clientSecret;
private boolean allowInsecureConnections;
private AuthorizationService defaultAuthorizationService;
private AuthorizationService insecureAuthorizationService;

/**
* Plugin registration.
Expand All @@ -66,14 +69,27 @@ public static void registerWith(Registrar registrar) {
plugin.setActivity(registrar.activity());
plugin.onAttachedToEngine(registrar.context(), registrar.messenger());
registrar.addActivityResultListener(plugin);
registrar.addViewDestroyListener(
new PluginRegistry.ViewDestroyListener() {
@Override
public boolean onViewDestroy(FlutterNativeView view) {
plugin.disposeAuthorizationServices();
return false;
}
});
}


private void setActivity(Activity flutterActivity) {
this.mainActivity = flutterActivity;
}

private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) {
this.applicationContext = context;
defaultAuthorizationService = new AuthorizationService(this.applicationContext);
AppAuthConfiguration.Builder authConfigBuilder = new AppAuthConfiguration.Builder();
authConfigBuilder.setConnectionBuilder(InsecureConnectionBuilder.INSTANCE);
insecureAuthorizationService = new AuthorizationService(applicationContext, authConfigBuilder.build());
final MethodChannel channel = new MethodChannel(binaryMessenger, "crossingthestreams.io/flutter_appauth");
channel.setMethodCallHandler(this);
}
Expand All @@ -85,6 +101,7 @@ public void onAttachedToEngine(FlutterPluginBinding binding) {

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
disposeAuthorizationServices();
}

@Override
Expand All @@ -109,6 +126,13 @@ public void onDetachedFromActivity() {
this.mainActivity = null;
}

private void disposeAuthorizationServices() {
defaultAuthorizationService.dispose();
insecureAuthorizationService.dispose();
defaultAuthorizationService = null;
insecureAuthorizationService = null;
}

private void checkAndSetPendingOperation(String method, Result result) {
if (pendingOperation != null) {
throw new IllegalStateException(
Expand Down Expand Up @@ -282,13 +306,9 @@ private void performAuthorization(AuthorizationServiceConfiguration serviceConfi
authRequestBuilder.setAdditionalParameters(additionalParameters);
}

AppAuthConfiguration.Builder authConfigBuilder = new AppAuthConfiguration.Builder();
if (allowInsecureConnections) {
authConfigBuilder.setConnectionBuilder(InsecureConnectionBuilder.INSTANCE);
}

AuthorizationService authService = new AuthorizationService(applicationContext, authConfigBuilder.build());
Intent authIntent = authService.getAuthorizationRequestIntent(authRequestBuilder.build());
AuthorizationService authorizationService = allowInsecureConnections ? insecureAuthorizationService : defaultAuthorizationService;
Intent authIntent = authorizationService.getAuthorizationRequestIntent(authRequestBuilder.build());
mainActivity.startActivityForResult(authIntent, exchangeCode ? RC_AUTH_EXCHANGE_CODE : RC_AUTH);
}

Expand All @@ -310,12 +330,7 @@ private void performTokenRequest(AuthorizationServiceConfiguration serviceConfig
builder.setAdditionalParameters(tokenRequestParameters.additionalParameters);
}

AppAuthConfiguration.Builder authConfigBuilder = new AppAuthConfiguration.Builder();
if (allowInsecureConnections) {
authConfigBuilder.setConnectionBuilder(InsecureConnectionBuilder.INSTANCE);
}

AuthorizationService authService = new AuthorizationService(applicationContext, authConfigBuilder.build());
AuthorizationService.TokenResponseCallback tokenResponseCallback = new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(
Expand All @@ -330,10 +345,11 @@ public void onTokenRequestCompleted(
};

TokenRequest tokenRequest = builder.build();
AuthorizationService authorizationService = allowInsecureConnections ? insecureAuthorizationService : defaultAuthorizationService;
if (clientSecret == null) {
authService.performTokenRequest(tokenRequest, tokenResponseCallback);
authorizationService.performTokenRequest(tokenRequest, tokenResponseCallback);
} else {
authService.performTokenRequest(tokenRequest, new ClientSecretBasic(clientSecret), tokenResponseCallback);
authorizationService.performTokenRequest(tokenRequest, new ClientSecretBasic(clientSecret), tokenResponseCallback);
}

}
Expand Down Expand Up @@ -432,8 +448,6 @@ private Map<String, Object> authorizationResponseToMap(AuthorizationResponse aut
return responseMap;
}



private class PendingOperation {
final String method;
final Result result;
Expand Down
2 changes: 1 addition & 1 deletion flutter_appauth/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_appauth
description: This plugin provides an abstraction around the Android and iOS AppAuth SDKs so it can be used to communicate with OAuth 2.0 and OpenID Connect providers
version: 0.9.2+2
version: 0.9.2+3
homepage: https://github.com/MaikuB/flutter_appauth/tree/master/flutter_appauth

environment:
Expand Down

0 comments on commit 020a19d

Please sign in to comment.