Skip to content

Commit 8c7c051

Browse files
TF-3348 Move accessToken from persistent storage to session storage for web
1 parent bfb20d3 commit 8c7c051

File tree

7 files changed

+145
-6
lines changed

7 files changed

+145
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

22
abstract class CacheManagerInteraction {
3+
const CacheManagerInteraction();
4+
35
Future<void> migrateHiveToIsolatedHive();
46
}

lib/features/login/data/extensions/token_oidc_extension.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ extension TokenOidcExtension on TokenOIDC {
55
TokenOidcCache toTokenOidcCache() {
66
return TokenOidcCache(token, tokenId.uuid, refreshToken, expiredTime: expiredTime);
77
}
8+
9+
TokenOidcCache toTokenOidcCacheWithoutToken() {
10+
return TokenOidcCache('', tokenId.uuid, refreshToken, expiredTime: expiredTime);
11+
}
812
}

lib/features/login/data/local/token_oidc_cache_manager.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_ex
99
class TokenOidcCacheManager extends CacheManagerInteraction {
1010
final TokenOidcCacheClient _tokenOidcCacheClient;
1111

12-
TokenOidcCacheManager(this._tokenOidcCacheClient);
12+
const TokenOidcCacheManager(this._tokenOidcCacheClient);
1313

1414
Future<TokenOIDC> getTokenOidc(String tokenIdHash) async {
1515
log('TokenOidcCacheManager::getTokenOidc(): tokenIdHash: $tokenIdHash');
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import 'package:core/utils/app_logger.dart';
2+
import 'package:model/oidc/token_oidc.dart';
3+
import 'package:tmail_ui_user/features/caching/clients/token_oidc_cache_client.dart';
4+
import 'package:tmail_ui_user/features/login/data/extensions/token_oidc_cache_extension.dart';
5+
import 'package:tmail_ui_user/features/login/data/extensions/token_oidc_extension.dart';
6+
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
7+
import 'package:tmail_ui_user/features/login/data/model/token_oidc_cache.dart';
8+
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
9+
import 'package:universal_html/html.dart';
10+
11+
class WebTokenOidcCacheManager extends TokenOidcCacheManager {
12+
const WebTokenOidcCacheManager(this._tokenOidcCacheClient)
13+
: super(_tokenOidcCacheClient);
14+
15+
final TokenOidcCacheClient _tokenOidcCacheClient;
16+
17+
static const _sessionStorageTokenKey = 'twake_mail_token_session_storage';
18+
19+
@override
20+
Future<TokenOIDC> getTokenOidc(String tokenIdHash) async {
21+
log('WebTokenOidcCacheManager::getTokenOidc(): tokenIdHash: $tokenIdHash');
22+
final tokenHiveCache = await _tokenOidcCacheClient.getItem(tokenIdHash);
23+
final tokenSessionStorageCache = window.sessionStorage[_sessionStorageTokenKey];
24+
log('WebTokenOidcCacheManager::getTokenOidc(): tokenHiveCache: $tokenHiveCache');
25+
log('WebTokenOidcCacheManager::getTokenOidc(): tokenSessionStorageCache: $tokenSessionStorageCache');
26+
if (tokenHiveCache == null) {
27+
throw NotFoundStoredTokenException();
28+
} else {
29+
return TokenOidcCache(
30+
tokenSessionStorageCache ?? 'dummy_token',
31+
tokenHiveCache.tokenId,
32+
tokenHiveCache.refreshToken,
33+
expiredTime: tokenSessionStorageCache != null
34+
? tokenHiveCache.expiredTime
35+
: DateTime.now().subtract(const Duration(hours: 1)),
36+
).toTokenOidc();
37+
}
38+
}
39+
40+
@override
41+
Future<void> persistOneTokenOidc(TokenOIDC tokenOIDC) async {
42+
log('TokenOidcCacheManager::persistOneTokenOidc(): $tokenOIDC');
43+
await _tokenOidcCacheClient.clearAllData();
44+
log('TokenOidcCacheManager::persistOneTokenOidc(): key: ${tokenOIDC.tokenId.uuid}');
45+
log('TokenOidcCacheManager::persistOneTokenOidc(): key\'s hash: ${tokenOIDC.tokenIdHash}');
46+
log('TokenOidcCacheManager::persistOneTokenOidc(): token: ${tokenOIDC.token}');
47+
final tokenHiveCache = tokenOIDC.toTokenOidcCacheWithoutToken();
48+
await _tokenOidcCacheClient.insertItem(tokenOIDC.tokenIdHash, tokenHiveCache);
49+
window.sessionStorage[_sessionStorageTokenKey] = tokenOIDC.token;
50+
log('TokenOidcCacheManager::persistOneTokenOidc(): done');
51+
}
52+
53+
@override
54+
Future<void> deleteTokenOidc() async {
55+
await _tokenOidcCacheClient.clearAllData();
56+
window.sessionStorage.remove(_sessionStorageTokenKey);
57+
}
58+
}

lib/main/bindings/local/local_bindings.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import 'package:core/utils/file_utils.dart';
3+
import 'package:core/utils/platform_info.dart';
34
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
45
import 'package:get/get.dart';
56
import 'package:shared_preferences/shared_preferences.dart';
@@ -31,6 +32,7 @@ import 'package:tmail_ui_user/features/login/data/local/authentication_info_cach
3132
import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart';
3233
import 'package:tmail_ui_user/features/login/data/local/oidc_configuration_cache_manager.dart';
3334
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
35+
import 'package:tmail_ui_user/features/login/data/local/web_token_oidc_cache_manager.dart';
3436
import 'package:tmail_ui_user/features/mailbox/data/local/mailbox_cache_manager.dart';
3537
import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.dart';
3638
import 'package:tmail_ui_user/features/mailbox_dashboard/data/local/local_sort_order_manager.dart';
@@ -66,7 +68,11 @@ class LocalBindings extends Bindings {
6668
Get.put(RecentSearchCacheClient());
6769
Get.put(RecentSearchCacheManager(Get.find<RecentSearchCacheClient>()));
6870
Get.put(TokenOidcCacheClient());
69-
Get.put(TokenOidcCacheManager(Get.find<TokenOidcCacheClient>()));
71+
if (PlatformInfo.isWeb) {
72+
Get.put<TokenOidcCacheManager>(WebTokenOidcCacheManager(Get.find<TokenOidcCacheClient>()));
73+
} else {
74+
Get.put(TokenOidcCacheManager(Get.find<TokenOidcCacheClient>()));
75+
}
7076
Get.put(AccountCacheClient());
7177
Get.put(AccountCacheManager(Get.find<AccountCacheClient>()));
7278
Get.put(EncryptionKeyCacheClient());

lib/main/bindings/local/local_isolate_bindings.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

2+
import 'package:core/utils/platform_info.dart';
23
import 'package:get/get.dart';
34
import 'package:tmail_ui_user/features/caching/clients/account_cache_client.dart';
45
import 'package:tmail_ui_user/features/caching/clients/encryption_key_cache_client.dart';
56
import 'package:tmail_ui_user/features/caching/clients/token_oidc_cache_client.dart';
67
import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart';
78
import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart';
89
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
10+
import 'package:tmail_ui_user/features/login/data/local/web_token_oidc_cache_manager.dart';
911
import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart';
1012

1113
class LocalIsolateBindings extends Bindings {
@@ -17,10 +19,17 @@ class LocalIsolateBindings extends Bindings {
1719

1820
void _bindingCaching() {
1921
Get.put(TokenOidcCacheClient(), tag: BindingTag.isolateTag);
20-
Get.put(TokenOidcCacheManager(
21-
Get.find<TokenOidcCacheClient>(tag: BindingTag.isolateTag)),
22-
tag: BindingTag.isolateTag
23-
);
22+
if (PlatformInfo.isWeb) {
23+
Get.put<TokenOidcCacheManager>(WebTokenOidcCacheManager(
24+
Get.find<TokenOidcCacheClient>(tag: BindingTag.isolateTag)),
25+
tag: BindingTag.isolateTag
26+
);
27+
} else {
28+
Get.put(TokenOidcCacheManager(
29+
Get.find<TokenOidcCacheClient>(tag: BindingTag.isolateTag)),
30+
tag: BindingTag.isolateTag
31+
);
32+
}
2433
Get.put(AccountCacheClient(), tag: BindingTag.isolateTag);
2534
Get.put(AccountCacheManager(
2635
Get.find<AccountCacheClient>(tag: BindingTag.isolateTag)),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import 'package:core/utils/file_utils.dart';
2+
import 'package:core/utils/platform_info.dart';
3+
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:get/instance_manager.dart';
6+
import 'package:mockito/annotations.dart';
7+
import 'package:shared_preferences/shared_preferences.dart';
8+
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
9+
import 'package:tmail_ui_user/features/login/data/local/web_token_oidc_cache_manager.dart';
10+
import 'package:tmail_ui_user/main/bindings/local/local_bindings.dart';
11+
12+
import 'local_bindings_test.mocks.dart';
13+
14+
@GenerateNiceMocks([
15+
MockSpec<FlutterSecureStorage>(),
16+
MockSpec<SharedPreferences>(),
17+
MockSpec<FileUtils>(),
18+
])
19+
void main() {
20+
late LocalBindings localBindings;
21+
22+
setUp(() {
23+
localBindings = LocalBindings();
24+
Get.put<FlutterSecureStorage>(MockFlutterSecureStorage());
25+
Get.put<SharedPreferences>(MockSharedPreferences());
26+
Get.put<FileUtils>(MockFileUtils());
27+
});
28+
29+
group('local bindings test:', () {
30+
test(
31+
'should inject WebTokenOidcCacheManager '
32+
'when platform is web',
33+
() {
34+
// arrange
35+
PlatformInfo.isTestingForWeb = true;
36+
localBindings.dependencies();
37+
38+
// act
39+
final cacheManager = Get.find<TokenOidcCacheManager>();
40+
41+
// assert
42+
expect(cacheManager, isInstanceOf<WebTokenOidcCacheManager>());
43+
PlatformInfo.isTestingForWeb = false;
44+
});
45+
46+
test(
47+
'should inject TokenOidcCacheManager '
48+
'when platform is not web (default)',
49+
() {
50+
// arrange
51+
localBindings.dependencies();
52+
53+
// act
54+
final cacheManager = Get.find<TokenOidcCacheManager>();
55+
56+
// assert
57+
expect(cacheManager, isInstanceOf<TokenOidcCacheManager>());
58+
});
59+
});
60+
}

0 commit comments

Comments
 (0)