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-1396] akhil/1396/multi_user_level_authentication_poc_master #574

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7677bfe
added tokenList
akhil-deriv Apr 26, 2024
23b9613
updated deriv auth service
akhil-deriv Apr 26, 2024
a118636
added print statement check
akhil-deriv Apr 29, 2024
88df298
feat: implemented multi-token authorization in Deriv Auth
akhil-deriv May 6, 2024
31ddecb
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv May 6, 2024
622b528
fix: fixed test cases for deriv auth
akhil-deriv May 7, 2024
b3a7a39
added tokenLogin changes
akhil-deriv May 30, 2024
9d9f946
added a print statement
akhil-deriv May 30, 2024
20f834d
added a print statement
akhil-deriv Jun 6, 2024
8287d8a
removed print statements
akhil-deriv Jun 10, 2024
4bea0f3
implemented reviewer suggestions
akhil-deriv Jun 11, 2024
08462d0
fixed test case
akhil-deriv Jun 11, 2024
3e3f9b3
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jun 11, 2024
d85ce0f
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jun 18, 2024
2fbfb47
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jun 20, 2024
6c99e14
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jun 28, 2024
2e90307
updated deriv_auth example
akhil-deriv Jun 28, 2024
80c75f0
updated package errors
akhil-deriv Jun 28, 2024
bd1c702
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jun 28, 2024
5d5456d
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jul 1, 2024
d387779
temporarily commented trackers
akhil-deriv Jul 1, 2024
5038110
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jul 2, 2024
3c5a022
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jul 3, 2024
4503c81
updated pubspec
akhil-deriv Jul 3, 2024
df5b81d
implemented new multi token authorization change
akhil-deriv Jul 3, 2024
267c861
Merge branch 'master' into akhil/1396/multi_user_level_authentication…
ramin-deriv Jul 4, 2024
96fe2f7
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jul 4, 2024
0a1e142
updated deriv auth authorize methods
akhil-deriv Jul 4, 2024
e829ad0
updated loginRequest in Deriv Auth to support multi token authorization
akhil-deriv Jul 14, 2024
55e5d91
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jul 15, 2024
4607a6f
uncommented login tracking
akhil-deriv Jul 15, 2024
a18b717
removed related TODO's
akhil-deriv Jul 15, 2024
adb1e93
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jul 23, 2024
39f6d58
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Jul 24, 2024
2c0b0a5
updated the naming for multiTokenLogin
akhil-deriv Jul 24, 2024
ea0753e
updated naming and documentation for mutiAuthorize
akhil-deriv Jul 24, 2024
4c57602
added optional multi token for login
akhil-deriv Jul 24, 2024
74eb3e0
Merge branch 'master' into akhil/1396/multi_user_level_authentication…
ramin-deriv Jul 25, 2024
2a5af10
made useMultiToken default to false
akhil-deriv Jul 25, 2024
8f3f49a
add a list empty check when getting defaultAccountID
akhil-deriv Jul 25, 2024
9779a79
code optimization
akhil-deriv Jul 25, 2024
23535ca
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Aug 5, 2024
f9e1c1d
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Aug 28, 2024
9c33fbf
added a fix for how we save the token in the enhanced authorize entity
akhil-deriv Aug 28, 2024
b0f049e
Merge branch 'master' into akhil/1396/multi_user_level_authentication…
akhil-deriv Aug 28, 2024
5e36c6a
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Sep 3, 2024
cb521f6
added loginId for logout
akhil-deriv Sep 4, 2024
978ea38
added loginId for passkeys for multiToken
akhil-deriv Sep 4, 2024
b5cebb0
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Sep 10, 2024
e2c1262
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Sep 16, 2024
ae4f17d
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Sep 22, 2024
8aede4c
Merge remote-tracking branch 'upstream/master' into akhil/1396/multi_…
akhil-deriv Sep 25, 2024
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
20 changes: 16 additions & 4 deletions packages/deriv_auth/lib/features/auth/cubit/deriv_auth_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
}

@override
Future<void> tokenLogin(String token) async {
Future<void> tokenLogin(String token, {List<String>? tokenList}) async {
emit(DerivAuthLoadingState());

await _tokenLoginRequest(
token,
akhil-deriv marked this conversation as resolved.
Show resolved Hide resolved
tokenList: tokenList,
accounts: await authService.getLatestAccounts(),
);
}
Expand Down Expand Up @@ -126,10 +127,11 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
Future<void> _tokenLoginRequest(
String token, {
required List<AccountModel> accounts,
List<String>? tokenList,
}) async {
try {
final AuthorizeEntity authorizeEntity =
await authService.login(token, accounts: accounts);
final AuthorizeEntity authorizeEntity = await authService.login(token,
accounts: accounts, tokenList: tokenList);
final LandingCompanyEntity landingCompanyEntity =
await authService.getLandingCompany(authorizeEntity.country);
_isUserMigrated = _checkUserMigrated(authorizeEntity);
Expand All @@ -152,18 +154,28 @@ class DerivAuthCubit extends Cubit<DerivAuthState> implements DerivAuthIO {
Future<void> authorizeDefaultAccount() async {
emit(DerivAuthLoadingState());

List<String> tokenList = <String>[];
final List<AccountModel> accountList =
await authService.getLatestAccounts();
final String? defaultAccountToken =
(await authService.getDefaultAccount())?.token;

if (defaultAccountToken == null) {
emit(DerivAuthLoggedOutState());

return;
} else {
tokenList = accountList
.where((AccountModel account) =>
account.token != null && defaultAccountToken != account.token)
.map((AccountModel account) => account.token!)
.toList();
}

await _tokenLoginRequest(
defaultAccountToken,
accounts: await authService.getLatestAccounts(),
accounts: accountList,
tokenList: tokenList,
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/deriv_auth/lib/features/auth/deriv_auth_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class DerivAuthIO {
});

/// Log user in with [token] after reset password or sign up.
Future<void> tokenLogin(String token);
Future<void> tokenLogin(String token, {List<String>? tokenList});

/// Log user out.
Future<void> logout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import 'package:deriv_auth/core/models/landig_comany_model.dart';
/// Interface of all authentication functions required from client.
abstract class BaseAuthRepository {
/// Authorize user with [token].
Future<AuthorizeResponseEntity> authorize(String? token);
Future<AuthorizeResponseEntity> authorize(
String? token, {
List<String>? tokenList,
});

/// Client functionality after user logs in.
Future<void> onLogin(AuthorizeEntity authorizeEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class BaseAuthService {
Future<AuthorizeEntity> login(
String token, {
required List<AccountModel> accounts,
List<String>? tokenList,
String? signupProvider,
String? refreshToken,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ class DerivAuthService extends BaseAuthService {
_filterSupportedAccounts(_response.accounts);

final String? _defaultAccountToken = _supportedAccounts.first.token;
final List<String> _tokenList = _supportedAccounts
akhil-deriv marked this conversation as resolved.
Show resolved Hide resolved
.sublist(1)
.where((AccountModel account) => account.token != null)
.map((AccountModel account) => account.token!)
.toList();

if (_defaultAccountToken != null) {
return login(
_defaultAccountToken,
tokenList: _tokenList.isEmpty ? null : _tokenList,
accounts: _supportedAccounts,
signupProvider: request.signupProvider,
refreshToken: _response.refreshToken,
Expand Down Expand Up @@ -90,12 +96,14 @@ class DerivAuthService extends BaseAuthService {
Future<AuthorizeEntity> login(
String token, {
required List<AccountModel> accounts,
List<String>? tokenList,
String? signupProvider,
String? refreshToken,
}) async {
try {
final AuthorizeEntity? responseAuthorizeEntity =
(await authRepository.authorize(token)).authorize;
(await authRepository.authorize(token, tokenList: tokenList))
.authorize;

_checkAuthorizeValidity(responseAuthorizeEntity);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'package:bloc_test/bloc_test.dart';
import 'package:deriv_auth/core/exceptions/deriv_auth_exception.dart';
import 'package:deriv_auth/core/models/account_model.dart';
Expand Down Expand Up @@ -32,7 +33,10 @@ void main() {
expect(authCubit.output, isA<Stream<DerivAuthState>>());
});

test('should emit [AuthLoggedOutState] for the first app start.', () {
test('should emit [AuthLoggedOutState] for the first app start.',
() async {
when(() => service.getLatestAccounts()).thenAnswer(
(_) => Future<List<AccountModel>>.value(<AccountModel>[]));
when(() => service.getDefaultAccount())
.thenAnswer((_) => Future<AccountModel?>.value());

Expand All @@ -42,20 +46,22 @@ void main() {
isA<DerivAuthLoggedOutState>(),
];

expectLater(
unawaited(expectLater(
authCubit.stream,
emitsInOrder(expectedResponse),
);
));

authCubit.authorizeDefaultAccount();
await authCubit.authorizeDefaultAccount();

verify(() => service.getLatestAccounts()).called(1);
verify(() => service.getDefaultAccount()).called(1);
verifyNever(
() => service.login(any(), accounts: any(named: 'accounts')),
);
});

test('should emit [AuthLoggedInState] if there is default account.', () {
test('should emit [AuthLoggedInState] if there is default account.',
() async {
when(() => service.getDefaultAccount())
.thenAnswer((_) => Future<AccountModel?>.value(mockedAccountModel));

Expand All @@ -66,22 +72,24 @@ void main() {
when(() => service.getLandingCompany(any())).thenAnswer((_) =>
Future<LandingCompanyEntity>.value(const LandingCompanyEntity()));

when(() => service.login(any(), accounts: any(named: 'accounts')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service
.login(any(),
accounts: any(named: 'accounts'),
tokenList: <String>[])).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
isA<DerivAuthLoadingState>(),
isA<DerivAuthLoggedInState>(),
];

expectLater(
unawaited(expectLater(
authCubit.stream,
emitsInOrder(expectedResponse),
);
));

authCubit.authorizeDefaultAccount();
await authCubit.authorizeDefaultAccount();

verify(() => service.getDefaultAccount()).called(1);
});
Expand Down Expand Up @@ -242,9 +250,11 @@ void main() {
Future<List<AccountModel>>.value(
<AccountModel>[mockedAccountModel]));

when(() => service.login(any(), accounts: any(named: 'accounts')))
.thenAnswer((_) =>
Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));
when(() => service
.login(any(),
accounts: any(named: 'accounts'),
tokenList: <String>[])).thenAnswer(
(_) => Future<AuthorizeEntity>.value(mockedValidAuthorizeEntity));

final List<TypeMatcher<DerivAuthState>> expectedResponse =
<TypeMatcher<DerivAuthState>>[
Expand All @@ -260,10 +270,11 @@ void main() {
emitsInOrder(expectedResponse),
);

authCubit.tokenLogin(_token);
authCubit.tokenLogin(_token, tokenList: <String>[]);

verify(
() => service.login(any(), accounts: any(named: 'accounts')),
() => service.login(any(),
accounts: any(named: 'accounts'), tokenList: <String>[]),
).called(1);
});

Expand Down Expand Up @@ -296,7 +307,8 @@ void main() {
authCubit.tokenLogin(_token);

verify(
() => service.login(any(), accounts: any(named: 'accounts')),
() => service.login(any(),
accounts: any(named: 'accounts'), tokenList: <String>[]),
).called(1);
});

Expand Down
Loading