Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deriv_auth): [DERG-1299] add user agent to login #341

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ abstract class BaseTokenService {
required BaseHttpClient client,
required String jwtToken,
required AuthConnectionInfo connectionInfo,
String? userAgent,
zohreh-deriv marked this conversation as resolved.
Show resolved Hide resolved
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DerivTokenService implements BaseTokenService {
required BaseHttpClient client,
required String jwtToken,
required AuthConnectionInfo connectionInfo,
String? userAgent,
}) async {
/// Extract login url from connection info.
final String baseUrl = 'https://${connectionInfo.endpoint}/oauth2/api/v1';
Expand All @@ -22,7 +23,10 @@ class DerivTokenService implements BaseTokenService {
url: loginUrl,
jsonBody:
request.copyWith(appId: int.parse(connectionInfo.appId)).toJson(),
headers: <String, String>{'Authorization': 'Bearer $jwtToken'},
headers: <String, String>{
'Authorization': 'Bearer $jwtToken',
'User-Agent': userAgent ?? ''
zohreh-deriv marked this conversation as resolved.
Show resolved Hide resolved
},
);

return GetTokensResponseModel.fromJson(jsonResponse);
Expand Down
36 changes: 23 additions & 13 deletions packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
required String email,
required String password,
String? otp,
String? userAgent,
}) async {
emit(DerivAuthLoadingState());

await _loginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: email,
password: password,
otp: otp,
),
false,
isSocialLogin: false,
userAgent: userAgent,
);
}

Expand All @@ -48,34 +50,38 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
required String oneAllConnectionToken,
final String? signupProvider,
String? otp,
String? userAgent,
}) async {
emit(DerivAuthLoadingState());

await _loginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.social,
oneAllConnectionToken: oneAllConnectionToken,
signupProvider: signupProvider,
otp: otp,
),
true,
isSocialLogin: true,
userAgent: userAgent,
);
}

@override
Future<void> socialAuth({
required SocialAuthDto socialAuthDto,
String? otp,
String? userAgent,
}) async {
emit(DerivAuthLoadingState());

await _loginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.socialLogin,
socialAuthDto: socialAuthDto,
otp: otp,
),
true,
isSocialLogin: true,
userAgent: userAgent,
);
}

Expand All @@ -89,11 +95,16 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
);
}

Future<void> _loginRequest(
GetTokensRequestModel request, bool isSocialLogin) async {
Future<void> _loginRequest({
required GetTokensRequestModel request,
required bool isSocialLogin,
String? userAgent,
}) async {
try {
final AuthorizeEntity authorizeEntity =
await authService.onLoginRequest(request);
final AuthorizeEntity authorizeEntity = await authService.onLoginRequest(
request: request,
userAgent: userAgent,
);
final LandingCompanyEntity landingCompanyEntity =
await authService.getLandingCompany(authorizeEntity.country);
_isUserMigrated = _checkUserMigrated(authorizeEntity);
Expand Down Expand Up @@ -182,10 +193,9 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
bool get isMigratedToWallets => _isUserMigrated;

bool _checkUserMigrated(AuthorizeEntity authorizeEntity) =>
authorizeEntity.accountList?.any(
(AccountListItem account) =>
authorizeEntity.accountList?.any((AccountListItem account) =>
account.accountCategory == AccountCategoryEnum.wallet) ??
false;
false;

@override
Stream<DerivAuthState> get output => stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import 'package:deriv_auth/core/services/token/models/login_request.dart';
/// Interface to define all authentication-related functionality.
abstract class BaseAuthService {
/// Function before logging user in.
Future<AuthorizeEntity> onLoginRequest(
GetTokensRequestModel request,
);
Future<AuthorizeEntity> onLoginRequest({
required GetTokensRequestModel request,
String? userAgent,
});

/// Log in a user with [token].
Future<AuthorizeEntity> login(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ class DerivAuthService extends BaseAuthService {
final BaseTokenService tokenService;

@override
Future<AuthorizeEntity> onLoginRequest(
GetTokensRequestModel request, [
Future<AuthorizeEntity> onLoginRequest({
required GetTokensRequestModel request,
String? userAgent,
Function? onInvalidJwtToken,
]) async {
}) async {
try {
final String jwtToken = await jwtService.getJwtToken();

Expand All @@ -52,6 +53,7 @@ class DerivAuthService extends BaseAuthService {
client: HttpClient(),
jwtToken: jwtToken,
connectionInfo: connectionInfo,
userAgent: userAgent,
);

final List<AccountModel> _supportedAccounts =
Expand All @@ -78,7 +80,7 @@ class DerivAuthService extends BaseAuthService {

jwtService.clearJwtToken();

return onLoginRequest(request);
return onLoginRequest(request: request, userAgent: userAgent);
} else {
throw _mapHttpErrorToDerivAuthError(error);
}
Expand Down Expand Up @@ -200,7 +202,7 @@ class DerivAuthService extends BaseAuthService {
return DerivAuthException(
type: AuthErrorType.invalidResidence,
message: exception.message,
);
);

default:
return DerivAuthException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ void main() {
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.system));

when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service.onLoginRequest(request: any(named: 'request')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
Expand All @@ -106,16 +107,18 @@ void main() {

authCubit.systemLogin(email: 'email', password: 'password');

verify(() => service.onLoginRequest(any())).called(1);
verify(() => service.onLoginRequest(request: any(named: 'request')))
.called(1);
});

test(
'should emit [AuthLoggedInState] upon a successful social login with one-all.',
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.social));

when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service.onLoginRequest(request: any(named: 'request')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
Expand All @@ -130,16 +133,18 @@ void main() {

authCubit.socialLogin(oneAllConnectionToken: 'token');

verify(() => service.onLoginRequest(any())).called(1);
verify(() => service.onLoginRequest(request: any(named: 'request')))
.called(1);
});

test(
'should emit [AuthLoggedInState] upon a successful social login with in-house service.',
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.social));

when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service.onLoginRequest(request: any(named: 'request')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
Expand All @@ -156,12 +161,14 @@ void main() {
socialAuthDto: mockSocialAuthDto,
);

verify(() => service.onLoginRequest(any())).called(1);
verify(() => service.onLoginRequest(request: any(named: 'request')))
.called(1);
});

test('should emit [AuthLoggedInState] upon a successful otp login.', () {
when(() => service.onLoginRequest(any())).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service.onLoginRequest(request: any(named: 'request')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
Expand All @@ -175,14 +182,16 @@ void main() {
);
authCubit.systemLogin(email: 'email', password: 'pass', otp: 'otp');

verify(() => service.onLoginRequest(any())).called(1);
verify(() => service.onLoginRequest(request: any(named: 'request')))
.called(1);
});

test('should emit [AuthErrorState] when an exception occurs in service.',
() {
registerFallbackValue(GetTokensRequestModel(type: AuthType.system));

when(() => service.onLoginRequest(any())).thenThrow(DerivAuthException(
when(() => service.onLoginRequest(request: any(named: 'request')))
.thenThrow(DerivAuthException(
message: 'message',
type: AuthErrorType.expiredAccount,
));
Expand All @@ -200,7 +209,8 @@ void main() {

authCubit.systemLogin(email: 'email', password: 'pass');

verify(() => service.onLoginRequest(any())).called(1);
verify(() => service.onLoginRequest(request: any(named: 'request')))
.called(1);
});
test('should emit [AuthLoggedOutState] upon a successful logout.', () {
when(() => service.logout()).thenAnswer((_) => Future<void>.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void main() {
);

final AuthorizeEntity response = await authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
Expand All @@ -154,7 +154,7 @@ void main() {
);

final AuthorizeEntity response = await authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.social,
signupProvider: 'signupProvider',
oneAllConnectionToken: 'oneAllConnectionToken',
Expand All @@ -180,7 +180,7 @@ void main() {
);

final AuthorizeEntity response = await authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.socialLogin,
signupProvider: 'signupProvider',
socialAuthDto: mockSocialAuthDto,
Expand All @@ -207,16 +207,17 @@ void main() {
);

final AuthorizeEntity response = await authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
signupProvider: 'signupProvider',
), () {
when(() => jwtService.getJwtToken()).thenAnswer(
(_) => Future<String>.value(validJwtToken),
);
});
),
onInvalidJwtToken: () {
when(() => jwtService.getJwtToken()).thenAnswer(
(_) => Future<String>.value(validJwtToken),
);
});

verify(() => jwtService.getJwtToken()).called(2);

Expand Down Expand Up @@ -251,7 +252,7 @@ void main() {

expect(
authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
Expand Down Expand Up @@ -288,7 +289,7 @@ void main() {

expect(
authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
Expand Down Expand Up @@ -325,7 +326,7 @@ void main() {

expect(
authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
Expand Down Expand Up @@ -434,7 +435,7 @@ void main() {

expect(
authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
Expand Down Expand Up @@ -471,7 +472,7 @@ void main() {

expect(
authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
Expand Down Expand Up @@ -508,7 +509,7 @@ void main() {

expect(
authService.onLoginRequest(
GetTokensRequestModel(
request: GetTokensRequestModel(
type: AuthType.system,
email: 'email',
password: 'pass',
Expand Down