Skip to content

Commit

Permalink
TF-3348 Move accessToken from persistent storage to session storage f…
Browse files Browse the repository at this point in the history
…or web
  • Loading branch information
tddang-linagora committed Jan 7, 2025
1 parent 57a22b9 commit 4da3b81
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 12 deletions.
14 changes: 7 additions & 7 deletions lib/features/login/data/local/token_oidc_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import 'package:tmail_ui_user/features/login/data/extensions/token_oidc_extensio
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';

class TokenOidcCacheManager {
final TokenOidcCacheClient _tokenOidcCacheClient;
final TokenOidcCacheClient tokenOidcCacheClient;

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

Future<TokenOIDC> getTokenOidc(String tokenIdHash) async {
log('TokenOidcCacheManager::getTokenOidc(): tokenIdHash: $tokenIdHash');
final tokenCache = await _tokenOidcCacheClient.getItem(tokenIdHash);
final tokenCache = await tokenOidcCacheClient.getItem(tokenIdHash);
log('TokenOidcCacheManager::getTokenOidc(): tokenCache: $tokenCache');
if (tokenCache == null) {
throw NotFoundStoredTokenException();
Expand All @@ -23,17 +23,17 @@ class TokenOidcCacheManager {

Future<void> persistOneTokenOidc(TokenOIDC tokenOIDC) async {
log('TokenOidcCacheManager::persistOneTokenOidc(): $tokenOIDC');
await _tokenOidcCacheClient.clearAllData();
await tokenOidcCacheClient.clearAllData();
log('TokenOidcCacheManager::persistOneTokenOidc(): key: ${tokenOIDC.tokenId.uuid}');
log('TokenOidcCacheManager::persistOneTokenOidc(): key\'s hash: ${tokenOIDC.tokenIdHash}');
log('TokenOidcCacheManager::persistOneTokenOidc(): token: ${tokenOIDC.token}');
await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenOIDC.toTokenOidcCache());
await tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenOIDC.toTokenOidcCache());
log('TokenOidcCacheManager::persistOneTokenOidc(): done');
}

Future<void> deleteTokenOidc() async {
await _tokenOidcCacheClient.clearAllData();
await tokenOidcCacheClient.clearAllData();
}

Future<void> closeTokenOIDCHiveCacheBox() => _tokenOidcCacheClient.closeBox();
Future<void> closeTokenOIDCHiveCacheBox() => tokenOidcCacheClient.closeBox();
}
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/login/data/extensions/token_oidc_cache_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(super.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',
tokenHiveCache.tokenId,
tokenHiveCache.refreshToken,
expiredTime: tokenSessionStorageCache != null
? tokenHiveCache.expiredTime
: DateTime.now().subtract(const Duration(hours: 1)),
).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 = TokenOidcCache(
'',
tokenOIDC.tokenId.uuid,
tokenOIDC.refreshToken,
expiredTime: tokenOIDC.expiredTime,
);
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

0 comments on commit 4da3b81

Please sign in to comment.