diff --git a/lib/features/login/data/network/interceptors/authorization_interceptors.dart b/lib/features/login/data/network/interceptors/authorization_interceptors.dart index 938eb47d8..15627cfc4 100644 --- a/lib/features/login/data/network/interceptors/authorization_interceptors.dart +++ b/lib/features/login/data/network/interceptors/authorization_interceptors.dart @@ -29,6 +29,11 @@ import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart'; class AuthorizationInterceptors extends QueuedInterceptorsWrapper { static const String _refreshAttemptedKey = '_authInterceptorRefreshAttempted'; + /// Set to `true` in [RequestOptions.extra] for requests that must never + /// carry the user's credentials, e.g. discovery requests sent to hosts + /// guessed from the email address domain. + static const String skipAuthorizationKey = 'skipAuthorization'; + final Dio _dio; final AuthenticationClientBase _authenticationClient; final TokenOidcCacheManager _tokenOidcCacheManager; @@ -153,6 +158,12 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper { @override void onRequest(RequestOptions options, RequestInterceptorHandler handler) { + if (options.extra[skipAuthorizationKey] == true) { + options.headers.remove(HttpHeaders.authorizationHeader); + super.onRequest(options, handler); + return; + } + switch(_authenticationType) { case AuthenticationType.basic: if (_authorization != null) { diff --git a/lib/features/login/data/network/oidc_http_client.dart b/lib/features/login/data/network/oidc_http_client.dart index 883f0033c..8935ef89d 100644 --- a/lib/features/login/data/network/oidc_http_client.dart +++ b/lib/features/login/data/network/oidc_http_client.dart @@ -5,7 +5,7 @@ import 'dart:io'; import 'package:core/data/model/query/query_parameter.dart'; import 'package:core/data/network/dio_client.dart'; import 'package:core/utils/app_logger.dart'; -import 'package:dio/dio.dart' show DioException; +import 'package:dio/dio.dart' show DioException, Options; import 'package:get/get_connect/http/src/exceptions/exceptions.dart'; import 'package:model/oidc/oidc_configuration.dart'; import 'package:model/oidc/request/oidc_request.dart'; @@ -15,6 +15,7 @@ import 'package:model/oidc/response/oidc_user_info.dart'; import 'package:tmail_ui_user/features/login/data/extensions/service_path_extension.dart'; import 'package:tmail_ui_user/features/login/data/network/config/oidc_constant.dart'; import 'package:tmail_ui_user/features/login/data/network/endpoint.dart'; +import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart'; import 'package:tmail_ui_user/features/login/data/network/oidc_error.dart'; import 'package:tmail_ui_user/features/login/domain/exceptions/login_exception.dart'; import 'package:tmail_ui_user/main/utils/app_config.dart'; @@ -34,7 +35,12 @@ class OIDCHttpClient { StringQueryParameter('resource', oidcRequest.resourceUrl), StringQueryParameter('rel', OIDCRequest.relUrl), ]) - .generateEndpointPath() + .generateEndpointPath(), + // WebFinger is public discovery, possibly sent to hosts guessed from + // the email address domain: never attach the user's credentials. + options: Options( + extra: {AuthorizationInterceptors.skipAuthorizationKey: true}, + ), ); log('OIDCHttpClient::checkOIDCIsAvailable(): RESULT: $result'); if (result is Map) { diff --git a/test/features/login/data/network/interceptors/authorization_interceptors_skip_test.dart b/test/features/login/data/network/interceptors/authorization_interceptors_skip_test.dart new file mode 100644 index 000000000..28be1cd81 --- /dev/null +++ b/test/features/login/data/network/interceptors/authorization_interceptors_skip_test.dart @@ -0,0 +1,54 @@ +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:jmap_dart_client/jmap/core/user_name.dart'; +import 'package:model/account/password.dart'; +import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart'; +import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart'; +import 'package:tmail_ui_user/features/login/data/network/authentication_client/authentication_client_base.dart'; +import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart'; +import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart'; + +class _FakeAuthenticationClient extends Fake implements AuthenticationClientBase {} + +class _FakeTokenOidcCacheManager extends Fake implements TokenOidcCacheManager {} + +class _FakeAccountCacheManager extends Fake implements AccountCacheManager {} + +class _FakeIOSSharingManager extends Fake implements IOSSharingManager {} + +void main() { + late AuthorizationInterceptors interceptors; + + setUp(() { + interceptors = AuthorizationInterceptors( + Dio(), + _FakeAuthenticationClient(), + _FakeTokenOidcCacheManager(), + _FakeAccountCacheManager(), + _FakeIOSSharingManager(), + ); + interceptors.setBasicAuthorization(UserName('alice'), Password('secret')); + }); + + RequestOptions intercept(RequestOptions options) { + interceptors.onRequest(options, RequestInterceptorHandler()); + return options; + } + + test('SHOULD attach credentials to regular requests', () { + final options = intercept(RequestOptions(path: 'https://jmap.example.com/jmap')); + + expect(options.headers[HttpHeaders.authorizationHeader], startsWith('Basic ')); + }); + + test('SHOULD NOT attach credentials when the request opts out', () { + final options = intercept(RequestOptions( + path: 'https://guessed.example.org/.well-known/webfinger', + extra: {AuthorizationInterceptors.skipAuthorizationKey: true}, + )); + + expect(options.headers.containsKey(HttpHeaders.authorizationHeader), isFalse); + }); +} diff --git a/test/features/login/data/network/oidc_http_client_test.dart b/test/features/login/data/network/oidc_http_client_test.dart index 1282f1b78..a92a74257 100644 --- a/test/features/login/data/network/oidc_http_client_test.dart +++ b/test/features/login/data/network/oidc_http_client_test.dart @@ -24,7 +24,7 @@ void main() { 'and status code is 404', () { // arrange - when(dioClient.get(any)).thenThrow(DioException( + when(dioClient.get(any, options: anyNamed('options'))).thenThrow(DioException( requestOptions: requestOptions, response: Response(requestOptions: requestOptions, statusCode: 404))); @@ -41,7 +41,7 @@ void main() { 'and status code is not 404', () { // arrange - when(dioClient.get(any)).thenThrow(DioException( + when(dioClient.get(any, options: anyNamed('options'))).thenThrow(DioException( requestOptions: requestOptions, response: Response(requestOptions: requestOptions, statusCode: 403))); @@ -57,7 +57,7 @@ void main() { 'and dioClient throw exception that is not DioException', () { // arrange - when(dioClient.get(any)).thenThrow(Exception()); + when(dioClient.get(any, options: anyNamed('options'))).thenThrow(Exception()); // assert expect(