TF-605 Use bearer token download attachment on Android

This commit is contained in:
dab246
2022-06-02 17:37:39 +07:00
committed by Dat H. Pham
parent 69b2dbf3cf
commit ba49d7699a
16 changed files with 243 additions and 14 deletions
@@ -4,4 +4,6 @@ abstract class AccountDatasource {
Future<Account> getCurrentAccount();
Future<void> setCurrentAccount(Account newCurrentAccount);
Future<void> deleteCurrentAccount(String accountId);
}
@@ -15,4 +15,11 @@ abstract class AuthenticationOIDCDataSource {
Future<void> persistAuthorityOidc(String authority);
Future<OIDCConfiguration> getStoredOidcConfiguration();
Future<TokenOIDC> refreshingTokensOIDC(
String clientId,
String redirectUrl,
String discoveryUrl,
List<String> scopes,
String refreshToken);
}
@@ -62,4 +62,11 @@ class AuthenticationOIDCDataSourceImpl extends AuthenticationOIDCDataSource {
Future<void> persistAuthorityOidc(String authority) {
return _oidcConfigurationCacheManager.persistAuthorityOidc(authority);
}
@override
Future<TokenOIDC> refreshingTokensOIDC(String clientId, String redirectUrl,
String discoveryUrl, List<String> scopes, String refreshToken) {
return _oidcHttpClient.refreshingTokensOIDC(
clientId, redirectUrl, discoveryUrl, scopes, refreshToken);
}
}
@@ -19,4 +19,9 @@ class HiveAccountDatasourceImpl extends AccountDatasource {
log('HiveAccountDatasourceImpl::setCurrentAccount(): $_accountCacheManager');
return _accountCacheManager.setSelectedAccount(newCurrentAccount);
}
@override
Future<void> deleteCurrentAccount(String accountId) {
return _accountCacheManager.deleteSelectedAccount(accountId);
}
}
@@ -49,6 +49,8 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
_token = newToken;
}
OIDCConfiguration? get oidcConfig => _configOIDC;
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
switch(_authenticationType) {
@@ -19,4 +19,9 @@ class AccountRepositoryImpl extends AccountRepository {
log('AccountRepositoryImpl::setCurrentAccount(): $newCurrentAccount');
return _accountDatasource.setCurrentAccount(newCurrentAccount);
}
@override
Future<void> deleteCurrentAccount(String accountId) {
return _accountDatasource.deleteCurrentAccount(accountId);
}
}
@@ -44,4 +44,20 @@ class AuthenticationOIDCRepositoryImpl extends AuthenticationOIDCRepository {
Future<void> persistAuthorityOidc(String authority) {
return _oidcDataSource.persistAuthorityOidc(authority);
}
@override
Future<TokenOIDC> refreshingTokensOIDC(
String clientId,
String redirectUrl,
String discoveryUrl,
List<String> scopes,
String refreshToken
) {
return _oidcDataSource.refreshingTokensOIDC(
clientId,
redirectUrl,
discoveryUrl,
scopes,
refreshToken);
}
}
@@ -35,4 +35,11 @@ class NotFoundAccessTokenException implements Exception {
class AccessTokenInvalidException implements Exception {
AccessTokenInvalidException();
}
class DownloadAttachmentHasTokenExpiredException implements Exception {
final String refreshToken;
DownloadAttachmentHasTokenExpiredException(this.refreshToken);
}
@@ -5,4 +5,6 @@ abstract class AccountRepository {
Future<Account> getCurrentAccount();
Future<void> setCurrentAccount(Account newCurrentAccount);
Future<void> deleteCurrentAccount(String accountId);
}
@@ -15,4 +15,11 @@ abstract class AuthenticationOIDCRepository {
Future<void> persistAuthorityOidc(String authority);
Future<OIDCConfiguration> getStoredOidcConfiguration();
Future<TokenOIDC> refreshingTokensOIDC(
String clientId,
String redirectUrl,
String discoveryUrl,
List<String> scopes,
String refreshToken);
}
@@ -0,0 +1,21 @@
import 'package:core/core.dart';
import 'package:model/model.dart';
class RefreshTokenOIDCSuccess extends UIState {
final TokenOIDC tokenOIDC;
RefreshTokenOIDCSuccess(this.tokenOIDC);
@override
List<Object> get props => [tokenOIDC];
}
class RefreshTokenOIDCFailure extends FeatureFailure {
final dynamic exception;
RefreshTokenOIDCFailure(this.exception);
@override
List<Object> get props => [exception];
}
@@ -0,0 +1,40 @@
import 'package:core/core.dart';
import 'package:dartz/dartz.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart';
import 'package:tmail_ui_user/features/login/domain/state/refresh_token_oidc_state.dart';
class RefreshTokenOIDCInteractor {
final AuthenticationOIDCRepository authenticationOIDCRepository;
final AccountRepository _accountRepository;
RefreshTokenOIDCInteractor(this.authenticationOIDCRepository, this._accountRepository);
Future<Either<Failure, Success>> execute(
OIDCConfiguration config,
String refreshToken) async {
try {
final newTokenOIDC = await authenticationOIDCRepository.refreshingTokensOIDC(
config.clientId,
config.redirectUrl,
config.discoveryUrl,
config.scopes,
refreshToken);
await Future.wait([
_accountRepository.setCurrentAccount(Account(
newTokenOIDC.tokenId.hashCode.toString(),
AuthenticationType.oidc,
isSelected: true)),
authenticationOIDCRepository.persistTokenOIDC(newTokenOIDC),
]);
return Right<Failure, Success>(RefreshTokenOIDCSuccess(newTokenOIDC));
} catch (e) {
logError('RefreshTokenOIDCInteractor::execute(): $e');
return Left<Failure, Success>(RefreshTokenOIDCFailure(e));
}
}
}