diff --git a/packages/deriv_auth/lib/core/services/token/services/base_token_service.dart b/packages/deriv_auth/lib/core/services/token/services/base_token_service.dart index e7f71f029..56f251c2e 100644 --- a/packages/deriv_auth/lib/core/services/token/services/base_token_service.dart +++ b/packages/deriv_auth/lib/core/services/token/services/base_token_service.dart @@ -9,8 +9,8 @@ abstract class BaseTokenService { Future getUserTokens({ required GetTokensRequestModel request, required BaseHttpClient client, - required String userAgent, required String jwtToken, required AuthConnectionInfo connectionInfo, + String? userAgent, }); } diff --git a/packages/deriv_auth/lib/core/services/token/services/deriv_token_service.dart b/packages/deriv_auth/lib/core/services/token/services/deriv_token_service.dart index aa19418c6..41063a3d6 100644 --- a/packages/deriv_auth/lib/core/services/token/services/deriv_token_service.dart +++ b/packages/deriv_auth/lib/core/services/token/services/deriv_token_service.dart @@ -10,9 +10,9 @@ class DerivTokenService implements BaseTokenService { Future getUserTokens({ required GetTokensRequestModel request, required BaseHttpClient client, - required String userAgent, required String jwtToken, required AuthConnectionInfo connectionInfo, + String? userAgent, }) async { /// Extract login url from connection info. final String baseUrl = 'https://${connectionInfo.endpoint}/oauth2/api/v1'; @@ -25,7 +25,7 @@ class DerivTokenService implements BaseTokenService { request.copyWith(appId: int.parse(connectionInfo.appId)).toJson(), headers: { 'Authorization': 'Bearer $jwtToken', - 'User-Agent': userAgent, + 'User-Agent': userAgent ?? '', }, ); diff --git a/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart b/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart index 1020991fd..cfdd6ba9f 100644 --- a/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart +++ b/packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart @@ -95,7 +95,7 @@ class DerivAuthCubit extends Cubit implements DerivAuthIO { try { final String userAgent = await getUserAgent(); final AuthorizeEntity authorizeEntity = - await authService.onLoginRequest(request, userAgent); + await authService.onLoginRequest(request, userAgent: userAgent); final LandingCompanyEntity landingCompanyEntity = await authService.getLandingCompany(authorizeEntity.country); _isUserMigrated = _checkUserMigrated(authorizeEntity); diff --git a/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart b/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart index 7cefdf0bd..05d5541d3 100644 --- a/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart +++ b/packages/deriv_auth/lib/features/auth/services/base_auth_service.dart @@ -7,9 +7,9 @@ import 'package:deriv_auth/core/services/token/models/login_request.dart'; abstract class BaseAuthService { /// Function before logging user in. Future onLoginRequest( - GetTokensRequestModel request, - String userAgent, - ); + GetTokensRequestModel request, { + String? userAgent, + }); /// Log in a user with [token]. Future login( diff --git a/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart b/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart index fde589097..285e68ca0 100644 --- a/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart +++ b/packages/deriv_auth/lib/features/auth/services/deriv_auth_service.dart @@ -41,10 +41,10 @@ class DerivAuthService extends BaseAuthService { @override Future onLoginRequest( - GetTokensRequestModel request, - String userAgent, [ + GetTokensRequestModel request, { + String? userAgent, Function? onInvalidJwtToken, - ]) async { + }) async { try { final String jwtToken = await jwtService.getJwtToken(); @@ -80,7 +80,7 @@ class DerivAuthService extends BaseAuthService { jwtService.clearJwtToken(); - return onLoginRequest(request, userAgent); + return onLoginRequest(request, userAgent: userAgent); } else { throw _mapHttpErrorToDerivAuthError(error); } diff --git a/packages/deriv_auth/test/core/services/token/services/deriv_token_service_test.dart b/packages/deriv_auth/test/core/services/token/services/deriv_token_service_test.dart index f5d1779b4..826f72c9c 100644 --- a/packages/deriv_auth/test/core/services/token/services/deriv_token_service_test.dart +++ b/packages/deriv_auth/test/core/services/token/services/deriv_token_service_test.dart @@ -42,7 +42,6 @@ void main() { final GetTokensResponseModel response = await _tokenService.getUserTokens( request: GetTokensRequestModel(), client: _client, - userAgent: 'user_agent', jwtToken: _jwtToken, connectionInfo: _connectionInfo, ); diff --git a/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart b/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart index fcad1b149..0c3254c1b 100644 --- a/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart +++ b/packages/deriv_auth/test/features/auth/cubit/deriv_auth_cubit_test.dart @@ -90,7 +90,7 @@ void main() { () { registerFallbackValue(GetTokensRequestModel(type: AuthType.system)); - when(() => service.onLoginRequest(any(), any())).thenAnswer( + when(() => service.onLoginRequest(any())).thenAnswer( (_) => Future.value(mockedValidAuthorizeEntity)); final List> expectedResponse = @@ -106,7 +106,7 @@ void main() { authCubit.systemLogin(email: 'email', password: 'password'); - verify(() => service.onLoginRequest(any(), any())).called(1); + verify(() => service.onLoginRequest(any())).called(1); }); test( @@ -114,7 +114,7 @@ void main() { () { registerFallbackValue(GetTokensRequestModel(type: AuthType.social)); - when(() => service.onLoginRequest(any(), any())).thenAnswer( + when(() => service.onLoginRequest(any())).thenAnswer( (_) => Future.value(mockedValidAuthorizeEntity)); final List> expectedResponse = @@ -130,7 +130,7 @@ void main() { authCubit.socialLogin(oneAllConnectionToken: 'token'); - verify(() => service.onLoginRequest(any(), any())).called(1); + verify(() => service.onLoginRequest(any())).called(1); }); test( @@ -138,7 +138,7 @@ void main() { () { registerFallbackValue(GetTokensRequestModel(type: AuthType.social)); - when(() => service.onLoginRequest(any(), any())).thenAnswer( + when(() => service.onLoginRequest(any())).thenAnswer( (_) => Future.value(mockedValidAuthorizeEntity)); final List> expectedResponse = @@ -156,11 +156,11 @@ void main() { socialAuthDto: mockSocialAuthDto, ); - verify(() => service.onLoginRequest(any(), any())).called(1); + verify(() => service.onLoginRequest(any())).called(1); }); test('should emit [AuthLoggedInState] upon a successful otp login.', () { - when(() => service.onLoginRequest(any(), any())).thenAnswer( + when(() => service.onLoginRequest(any())).thenAnswer( (_) => Future.value(mockedValidAuthorizeEntity)); final List> expectedResponse = @@ -175,15 +175,14 @@ void main() { ); authCubit.systemLogin(email: 'email', password: 'pass', otp: 'otp'); - verify(() => service.onLoginRequest(any(), any())).called(1); + verify(() => service.onLoginRequest(any())).called(1); }); test('should emit [AuthErrorState] when an exception occurs in service.', () { registerFallbackValue(GetTokensRequestModel(type: AuthType.system)); - when(() => service.onLoginRequest(any(), any())) - .thenThrow(DerivAuthException( + when(() => service.onLoginRequest(any())).thenThrow(DerivAuthException( message: 'message', type: AuthErrorType.expiredAccount, )); @@ -201,7 +200,7 @@ void main() { authCubit.systemLogin(email: 'email', password: 'pass'); - verify(() => service.onLoginRequest(any(), any())).called(1); + verify(() => service.onLoginRequest(any())).called(1); }); test('should emit [AuthLoggedOutState] upon a successful logout.', () { when(() => service.logout()).thenAnswer((_) => Future.value()); diff --git a/packages/deriv_auth/test/features/auth/service/deriv_auth_service_test.dart b/packages/deriv_auth/test/features/auth/service/deriv_auth_service_test.dart index 902883b94..3930d7011 100644 --- a/packages/deriv_auth/test/features/auth/service/deriv_auth_service_test.dart +++ b/packages/deriv_auth/test/features/auth/service/deriv_auth_service_test.dart @@ -60,7 +60,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenAnswer( @@ -76,7 +75,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(invalidJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenThrow( @@ -135,7 +133,6 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', ); expect(response.userId, mockedValidAuthorizeEntity.userId); @@ -162,7 +159,6 @@ void main() { signupProvider: 'signupProvider', oneAllConnectionToken: 'oneAllConnectionToken', ), - 'user_agent', ); expect(response.userId, mockedValidAuthorizeEntity.userId); @@ -189,7 +185,6 @@ void main() { signupProvider: 'signupProvider', socialAuthDto: mockSocialAuthDto, ), - 'user_agent', ); expect(response.userId, mockedValidAuthorizeEntity.userId); @@ -218,8 +213,7 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', - () { + onInvalidJwtToken: () { when(() => jwtService.getJwtToken()).thenAnswer( (_) => Future.value(validJwtToken), ); @@ -247,7 +241,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenThrow( @@ -266,7 +259,6 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', ), throwsA( isA().having( @@ -286,7 +278,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenThrow( @@ -305,7 +296,6 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', ), throwsA( isA().having( @@ -325,7 +315,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenThrow( @@ -344,7 +333,6 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', ), throwsA( isA().having( @@ -436,7 +424,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenThrow( @@ -455,7 +442,6 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', ), throwsA( isA().having( @@ -475,7 +461,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenThrow( @@ -494,7 +479,6 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', ), throwsA( isA().having( @@ -514,7 +498,6 @@ void main() { when(() => tokenService.getUserTokens( request: any(named: 'request'), client: any(named: 'client'), - userAgent: 'user_agent', jwtToken: any(named: 'jwtToken', that: equals(validJwtToken)), connectionInfo: any(named: 'connectionInfo'), )).thenThrow( @@ -533,7 +516,6 @@ void main() { password: 'pass', signupProvider: 'signupProvider', ), - 'user_agent', ), throwsA( isA().having(