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

TF-3348 Move access token from persistent to memory #3404

Open
wants to merge 3 commits into
base: maintenance-v0.14.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions lib/features/login/data/extensions/token_oidc_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ extension TokenOidcExtension on TokenOIDC {
TokenOidcCache toTokenOidcCache() {
return TokenOidcCache(token, tokenId.uuid, refreshToken, expiredTime: expiredTime);
}

TokenOidcCache toTokenOidcCacheWithoutToken() {
return TokenOidcCache('', tokenId.uuid, refreshToken, expiredTime: expiredTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_ex
class TokenOidcCacheManager {
final TokenOidcCacheClient _tokenOidcCacheClient;

TokenOidcCacheManager(this._tokenOidcCacheClient);
const TokenOidcCacheManager(this._tokenOidcCacheClient);

Future<TokenOIDC> getTokenOidc(String tokenIdHash) async {
log('TokenOidcCacheManager::getTokenOidc(): tokenIdHash: $tokenIdHash');
Expand Down
58 changes: 58 additions & 0 deletions lib/features/login/data/local/web_token_oidc_cache_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:core/utils/app_logger.dart';
import 'package:model/oidc/token_oidc.dart';
import 'package:tmail_ui_user/features/caching/clients/token_oidc_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/extensions/token_oidc_cache_extension.dart';
import 'package:tmail_ui_user/features/login/data/extensions/token_oidc_extension.dart';
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/model/token_oidc_cache.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
import 'package:universal_html/html.dart';

class WebTokenOidcCacheManager extends TokenOidcCacheManager {
const WebTokenOidcCacheManager(this._tokenOidcCacheClient)
: super(_tokenOidcCacheClient);

final TokenOidcCacheClient _tokenOidcCacheClient;

static const _sessionStorageTokenKey = 'twake_mail_token_session_storage';

@override
Future<TokenOIDC> getTokenOidc(String tokenIdHash) async {
log('WebTokenOidcCacheManager::getTokenOidc(): tokenIdHash: $tokenIdHash');
final tokenHiveCache = await _tokenOidcCacheClient.getItem(tokenIdHash);
final tokenSessionStorageCache = window.sessionStorage[_sessionStorageTokenKey];
log('WebTokenOidcCacheManager::getTokenOidc(): tokenHiveCache: $tokenHiveCache');
log('WebTokenOidcCacheManager::getTokenOidc(): tokenSessionStorageCache: $tokenSessionStorageCache');
if (tokenHiveCache == null) {
throw NotFoundStoredTokenException();
} else {
return TokenOidcCache(
tokenSessionStorageCache ?? 'dummy_token',
dab246 marked this conversation as resolved.
Show resolved Hide resolved
tokenHiveCache.tokenId,
tokenHiveCache.refreshToken,
expiredTime: tokenSessionStorageCache != null
? tokenHiveCache.expiredTime
: DateTime.now().subtract(const Duration(hours: 1)),
dab246 marked this conversation as resolved.
Show resolved Hide resolved
).toTokenOidc();
}
}

@override
Future<void> persistOneTokenOidc(TokenOIDC tokenOIDC) async {
log('TokenOidcCacheManager::persistOneTokenOidc(): $tokenOIDC');
await _tokenOidcCacheClient.clearAllData();
log('TokenOidcCacheManager::persistOneTokenOidc(): key: ${tokenOIDC.tokenId.uuid}');
log('TokenOidcCacheManager::persistOneTokenOidc(): key\'s hash: ${tokenOIDC.tokenIdHash}');
log('TokenOidcCacheManager::persistOneTokenOidc(): token: ${tokenOIDC.token}');
final tokenHiveCache = tokenOIDC.toTokenOidcCacheWithoutToken();
await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenHiveCache);
window.sessionStorage[_sessionStorageTokenKey] = tokenOIDC.token;
log('TokenOidcCacheManager::persistOneTokenOidc(): done');
}

@override
Future<void> deleteTokenOidc() async {
await _tokenOidcCacheClient.clearAllData();
window.sessionStorage.remove(_sessionStorageTokenKey);
}
}
8 changes: 7 additions & 1 deletion lib/main/bindings/local/local_bindings.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import 'package:core/utils/file_utils.dart';
import 'package:core/utils/platform_info.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand Down Expand Up @@ -29,6 +30,7 @@ import 'package:tmail_ui_user/features/login/data/local/authentication_info_cach
import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/local/oidc_configuration_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/local/web_token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/features/mailbox/data/local/mailbox_cache_manager.dart';
import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/data/local/local_spam_report_manager.dart';
Expand Down Expand Up @@ -63,7 +65,11 @@ class LocalBindings extends Bindings {
Get.put(RecentSearchCacheClient());
Get.put(RecentSearchCacheManager(Get.find<RecentSearchCacheClient>()));
Get.put(TokenOidcCacheClient());
Get.put(TokenOidcCacheManager(Get.find<TokenOidcCacheClient>()));
if (PlatformInfo.isWeb) {
Get.put<TokenOidcCacheManager>(WebTokenOidcCacheManager(Get.find<TokenOidcCacheClient>()));
} else {
Get.put(TokenOidcCacheManager(Get.find<TokenOidcCacheClient>()));
}
Get.put(AccountCacheClient());
Get.put(AccountCacheManager(Get.find<AccountCacheClient>()));
Get.put(EncryptionKeyCacheClient());
Expand Down
17 changes: 13 additions & 4 deletions lib/main/bindings/local/local_isolate_bindings.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

import 'package:core/utils/platform_info.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/caching/clients/account_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/encryption_key_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/token_oidc_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/local/encryption_key_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/local/web_token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart';

class LocalIsolateBindings extends Bindings {
Expand All @@ -17,10 +19,17 @@ class LocalIsolateBindings extends Bindings {

void _bindingCaching() {
Get.put(TokenOidcCacheClient(), tag: BindingTag.isolateTag);
Get.put(TokenOidcCacheManager(
Get.find<TokenOidcCacheClient>(tag: BindingTag.isolateTag)),
tag: BindingTag.isolateTag
);
if (PlatformInfo.isWeb) {
Get.put<TokenOidcCacheManager>(WebTokenOidcCacheManager(
Get.find<TokenOidcCacheClient>(tag: BindingTag.isolateTag)),
tag: BindingTag.isolateTag
);
} else {
Get.put(TokenOidcCacheManager(
Get.find<TokenOidcCacheClient>(tag: BindingTag.isolateTag)),
tag: BindingTag.isolateTag
);
}
Get.put(AccountCacheClient(), tag: BindingTag.isolateTag);
Get.put(AccountCacheManager(
Get.find<AccountCacheClient>(tag: BindingTag.isolateTag)),
Expand Down
60 changes: 60 additions & 0 deletions test/main/bindings/local/local_bindings_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:core/utils/file_utils.dart';
import 'package:core/utils/platform_info.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/instance_manager.dart';
import 'package:mockito/annotations.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/local/web_token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/main/bindings/local/local_bindings.dart';

import 'local_bindings_test.mocks.dart';

@GenerateNiceMocks([
MockSpec<FlutterSecureStorage>(),
MockSpec<SharedPreferences>(),
MockSpec<FileUtils>(),
])
void main() {
late LocalBindings localBindings;

setUp(() {
localBindings = LocalBindings();
Get.put<FlutterSecureStorage>(MockFlutterSecureStorage());
Get.put<SharedPreferences>(MockSharedPreferences());
Get.put<FileUtils>(MockFileUtils());
});

group('local bindings test:', () {
test(
'should inject WebTokenOidcCacheManager '
'when platform is web',
() {
// arrange
PlatformInfo.isTestingForWeb = true;
localBindings.dependencies();

// act
final cacheManager = Get.find<TokenOidcCacheManager>();

// assert
expect(cacheManager, isInstanceOf<WebTokenOidcCacheManager>());
PlatformInfo.isTestingForWeb = false;
});

test(
'should inject TokenOidcCacheManager '
'when platform is not web (default)',
() {
// arrange
localBindings.dependencies();

// act
final cacheManager = Get.find<TokenOidcCacheManager>();

// assert
expect(cacheManager, isInstanceOf<TokenOidcCacheManager>());
});
});
}
Loading