TF-644 Add accessToken in download interactor attachments for web

This commit is contained in:
dab246
2022-06-13 18:01:06 +07:00
committed by Dat H. Pham
parent 4b91ce2d7c
commit 4a2addd77a
5 changed files with 90 additions and 37 deletions
@@ -218,8 +218,6 @@ class EmailAPI {
? accountRequest.bearerToken
: accountRequest.basicAuth;
log('EmailAPI::exportAttachment(): authentication: $authentication');
return _downloadManager.downloadFile(
attachment.getDownloadUrl(baseDownloadUrl, accountId),
getTemporaryDirectory(),
@@ -234,10 +232,14 @@ class EmailAPI {
String baseDownloadUrl,
AccountRequest accountRequest,
) async {
final authentication = accountRequest.authenticationType == AuthenticationType.oidc
? accountRequest.bearerToken
: accountRequest.basicAuth;
return _downloadManager.downloadFileForWeb(
attachment.getDownloadUrl(baseDownloadUrl, accountId),
attachment.name ?? '',
accountRequest.basicAuth,
authentication,
);
}
@@ -9,7 +9,7 @@ class DownloadAttachmentForWebSuccess extends UIState {
}
class DownloadAttachmentForWebFailure extends FeatureFailure {
final exception;
final dynamic exception;
DownloadAttachmentForWebFailure(this.exception);
@@ -1,41 +1,79 @@
import 'dart:async';
import 'package:core/core.dart';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:model/model.dart';
import 'package:model/account/account_request.dart';
import 'package:model/account/authentication_type.dart';
import 'package:model/account/password.dart';
import 'package:model/account/user_name.dart';
import 'package:model/email/attachment.dart';
import 'package:model/oidc/token_oidc.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/email/domain/state/download_attachment_for_web_state.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/repository/credential_repository.dart';
class DownloadAttachmentForWebInteractor {
final EmailRepository emailRepository;
final CredentialRepository credentialRepository;
final AccountRepository _accountRepository;
final AuthenticationOIDCRepository _authenticationOIDCRepository;
DownloadAttachmentForWebInteractor(this.emailRepository, this.credentialRepository);
DownloadAttachmentForWebInteractor(
this.emailRepository,
this.credentialRepository,
this._accountRepository,
this._authenticationOIDCRepository);
Stream<Either<Failure, Success>> execute(Attachment attachment, AccountId accountId, String baseDownloadUrl,) async* {
Stream<Either<Failure, Success>> execute(
Attachment attachment,
AccountId accountId,
String baseDownloadUrl
) async* {
try {
final result = await Future.wait(
[credentialRepository.getUserName(), credentialRepository.getPassword()],
eagerError: true
).then((List responses) async {
final accountRequest = AccountRequest(
userName: responses.first,
password: responses.last,
authenticationType: AuthenticationType.basic);
final currentAccount = await _accountRepository.getCurrentAccount();
final result = await Future.wait([
if (currentAccount.authenticationType == AuthenticationType.oidc)
_authenticationOIDCRepository.getStoredTokenOIDC(currentAccount.id)
else
...[
credentialRepository.getUserName(),
credentialRepository.getPassword()
]
], eagerError: true).then((List responses) async {
AccountRequest accountRequest;
if (currentAccount.authenticationType == AuthenticationType.oidc) {
final tokenOidc = responses.first as TokenOIDC;
accountRequest = AccountRequest(
token: tokenOidc.toToken(),
authenticationType: AuthenticationType.oidc);
} else {
accountRequest = AccountRequest(
userName: responses.first as UserName,
password: responses.last as Password,
authenticationType: AuthenticationType.basic);
}
return await emailRepository.downloadAttachmentForWeb(
attachment,
accountId,
baseDownloadUrl,
accountRequest);
});
if (result) {
yield Right<Failure, Success>(DownloadAttachmentForWebSuccess());
} else {
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(null));
}
} catch (exception) {
log('DownloadAttachmentForWebInteractor::execute(): exception: $exception');
yield Left<Failure, Success>(DownloadAttachmentForWebFailure(exception));
}
}
@@ -98,7 +98,8 @@ class EmailBindings extends BaseBindings {
Get.lazyPut(() => DownloadAttachmentForWebInteractor(
Get.find<EmailRepository>(),
Get.find<CredentialRepository>(),
));
Get.find<AccountRepository>(),
Get.find<AuthenticationOIDCRepository>()));
Get.lazyPut(() => RefreshTokenOIDCInteractor(
Get.find<AuthenticationOIDCRepository>(),
Get.find<AccountRepository>(),