Skip to content

Commit

Permalink
[flutter_appauth] refactored Android related to allowInsecureConnecti…
Browse files Browse the repository at this point in the history
…ons (#555)

* refactored code on managing AuthorizationService instances

* refactored code on getting appropriate ConnectionBuilder

* Google Java Format

* release prep

---------

Co-authored-by: github-actions <>
  • Loading branch information
MaikuB authored Oct 13, 2024
1 parent 0d96bcd commit 96c801f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 51 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 @@
## [8.0.0-dev.4]

* [Android] no functional changes but some code around dealing with `allowInsecureConnections` has been done in response to issue [554](https://github.com/MaikuB/flutter_appauth/issues/554)

## [8.0.0-dev.3]

* Includes changes from the 7.0.1 release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.openid.appauth.ResponseTypeValues;
import net.openid.appauth.TokenRequest;
import net.openid.appauth.TokenResponse;
import net.openid.appauth.connectivity.ConnectionBuilder;
import net.openid.appauth.connectivity.DefaultConnectionBuilder;

import org.json.JSONException;
Expand Down Expand Up @@ -94,12 +95,7 @@ private void setActivity(Activity 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);
authConfigBuilder.setSkipIssuerHttpsCheck(true);
insecureAuthorizationService =
new AuthorizationService(applicationContext, authConfigBuilder.build());
createAuthorizationServices();
final MethodChannel channel =
new MethodChannel(binaryMessenger, "crossingthestreams.io/flutter_appauth");
channel.setMethodCallHandler(this);
Expand Down Expand Up @@ -137,6 +133,15 @@ public void onDetachedFromActivity() {
this.mainActivity = null;
}

private void createAuthorizationServices() {
defaultAuthorizationService = new AuthorizationService(this.applicationContext);
AppAuthConfiguration.Builder authConfigBuilder = new AppAuthConfiguration.Builder();
authConfigBuilder.setConnectionBuilder(InsecureConnectionBuilder.INSTANCE);
authConfigBuilder.setSkipIssuerHttpsCheck(true);
insecureAuthorizationService =
new AuthorizationService(applicationContext, authConfigBuilder.build());
}

private void disposeAuthorizationServices() {
defaultAuthorizationService.dispose();
insecureAuthorizationService.dispose();
Expand Down Expand Up @@ -340,22 +345,20 @@ public void onFetchConfigurationCompleted(
};
if (tokenRequestParameters.discoveryUrl != null) {
AuthorizationServiceConfiguration.fetchFromUrl(
Uri.parse(tokenRequestParameters.discoveryUrl),
callback,
allowInsecureConnections
? InsecureConnectionBuilder.INSTANCE
: DefaultConnectionBuilder.INSTANCE);
Uri.parse(tokenRequestParameters.discoveryUrl), callback, getConnectionBuilder());
} else {
AuthorizationServiceConfiguration.fetchFromIssuer(
Uri.parse(tokenRequestParameters.issuer),
callback,
allowInsecureConnections
? InsecureConnectionBuilder.INSTANCE
: DefaultConnectionBuilder.INSTANCE);
Uri.parse(tokenRequestParameters.issuer), callback, getConnectionBuilder());
}
}
}

private @NonNull ConnectionBuilder getConnectionBuilder() {
return allowInsecureConnections
? InsecureConnectionBuilder.INSTANCE
: DefaultConnectionBuilder.INSTANCE;
}

private AuthorizationServiceConfiguration processServiceConfigurationParameters(
Map<String, String> serviceConfigurationArguments) {
final String endSessionEndpoint = serviceConfigurationArguments.get("endSessionEndpoint");
Expand Down Expand Up @@ -389,18 +392,10 @@ public void onFetchConfigurationCompleted(
};
if (tokenRequestParameters.discoveryUrl != null) {
AuthorizationServiceConfiguration.fetchFromUrl(
Uri.parse(tokenRequestParameters.discoveryUrl),
callback,
allowInsecureConnections
? InsecureConnectionBuilder.INSTANCE
: DefaultConnectionBuilder.INSTANCE);
Uri.parse(tokenRequestParameters.discoveryUrl), callback, getConnectionBuilder());
} else {
AuthorizationServiceConfiguration.fetchFromIssuer(
Uri.parse(tokenRequestParameters.issuer),
callback,
allowInsecureConnections
? InsecureConnectionBuilder.INSTANCE
: DefaultConnectionBuilder.INSTANCE);
Uri.parse(tokenRequestParameters.issuer), callback, getConnectionBuilder());
}
}
}
Expand Down Expand Up @@ -461,8 +456,7 @@ private void performAuthorization(
authRequestBuilder.setAdditionalParameters(additionalParameters);
}

AuthorizationService authorizationService =
allowInsecureConnections ? insecureAuthorizationService : defaultAuthorizationService;
AuthorizationService authorizationService = getAuthorizationService();

try {
Intent authIntent =
Expand Down Expand Up @@ -513,8 +507,7 @@ public void onTokenRequestCompleted(TokenResponse resp, AuthorizationException e
};

TokenRequest tokenRequest = builder.build();
AuthorizationService authorizationService =
allowInsecureConnections ? insecureAuthorizationService : defaultAuthorizationService;
AuthorizationService authorizationService = getAuthorizationService();
if (clientSecret == null) {
authorizationService.performTokenRequest(tokenRequest, tokenResponseCallback);
} else {
Expand Down Expand Up @@ -548,18 +541,10 @@ public void onFetchConfigurationCompleted(

if (endSessionRequestParameters.discoveryUrl != null) {
AuthorizationServiceConfiguration.fetchFromUrl(
Uri.parse(endSessionRequestParameters.discoveryUrl),
callback,
allowInsecureConnections
? InsecureConnectionBuilder.INSTANCE
: DefaultConnectionBuilder.INSTANCE);
Uri.parse(endSessionRequestParameters.discoveryUrl), callback, getConnectionBuilder());
} else {
AuthorizationServiceConfiguration.fetchFromIssuer(
Uri.parse(endSessionRequestParameters.issuer),
callback,
allowInsecureConnections
? InsecureConnectionBuilder.INSTANCE
: DefaultConnectionBuilder.INSTANCE);
Uri.parse(endSessionRequestParameters.issuer), callback, getConnectionBuilder());
}
}
}
Expand Down Expand Up @@ -588,12 +573,17 @@ private void performEndSessionRequest(
}

final EndSessionRequest endSessionRequest = endSessionRequestBuilder.build();
AuthorizationService authorizationService =
allowInsecureConnections ? insecureAuthorizationService : defaultAuthorizationService;
AuthorizationService authorizationService = getAuthorizationService();
Intent endSessionIntent = authorizationService.getEndSessionRequestIntent(endSessionRequest);
mainActivity.startActivityForResult(endSessionIntent, RC_END_SESSION);
}

private AuthorizationService getAuthorizationService() {
AuthorizationService authorizationService =
allowInsecureConnections ? insecureAuthorizationService : defaultAuthorizationService;
return authorizationService;
}

private void finishWithTokenError(AuthorizationException ex) {
finishWithError(
TOKEN_ERROR_CODE,
Expand Down Expand Up @@ -702,14 +692,7 @@ private void processAuthorizationData(
boolean exchangeCode) {
if (authException == null) {
if (exchangeCode) {
AppAuthConfiguration.Builder authConfigBuilder = new AppAuthConfiguration.Builder();
if (allowInsecureConnections) {
authConfigBuilder.setConnectionBuilder(InsecureConnectionBuilder.INSTANCE);
authConfigBuilder.setSkipIssuerHttpsCheck(true);
}

AuthorizationService authService =
new AuthorizationService(applicationContext, authConfigBuilder.build());
AuthorizationService authService = getAuthorizationService();
AuthorizationService.TokenResponseCallback tokenResponseCallback =
new AuthorizationService.TokenResponseCallback() {
@Override
Expand Down
2 changes: 1 addition & 1 deletion flutter_appauth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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: 8.0.0-dev.3
version: 8.0.0-dev.4
homepage: https://github.com/MaikuB/flutter_appauth/tree/master/flutter_appauth

environment:
Expand Down

0 comments on commit 96c801f

Please sign in to comment.