TF-624 Use AuthenticationClient to Get/Refresh token and logout oidc
This commit is contained in:
@@ -10,12 +10,12 @@ import 'package:model/oidc/token.dart';
|
||||
import 'package:model/oidc/token_oidc.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/account_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/network/oidc_http_client.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/authentication_client/authentication_client_base.dart';
|
||||
|
||||
class AuthorizationInterceptors extends InterceptorsWrapper {
|
||||
|
||||
final Dio _dio;
|
||||
final OIDCHttpClient _oidcHttpClient;
|
||||
final AuthenticationClientBase _authenticationClient;
|
||||
final TokenOidcCacheManager _tokenOidcCacheManager;
|
||||
final AccountCacheManager _accountCacheManager;
|
||||
|
||||
@@ -26,7 +26,7 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
|
||||
|
||||
AuthorizationInterceptors(
|
||||
this._dio,
|
||||
this._oidcHttpClient,
|
||||
this._authenticationClient,
|
||||
this._tokenOidcCacheManager,
|
||||
this._accountCacheManager
|
||||
);
|
||||
@@ -79,7 +79,7 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
|
||||
if ((_isTokenExpired() || err.response?.statusCode == 401) &&
|
||||
_isAuthenticationOidcValid()) {
|
||||
try {
|
||||
final newToken = await _oidcHttpClient.refreshingTokensOIDC(
|
||||
final newToken = await _authenticationClient.refreshingTokensOIDC(
|
||||
_configOIDC!.clientId,
|
||||
_configOIDC!.redirectUrl,
|
||||
_configOIDC!.discoveryUrl,
|
||||
|
||||
@@ -4,26 +4,19 @@ import 'dart:convert';
|
||||
import 'package:core/data/model/query/query_parameter.dart';
|
||||
import 'package:core/data/network/dio_client.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:flutter_appauth/flutter_appauth.dart';
|
||||
import 'package:model/oidc/oidc_configuration.dart';
|
||||
import 'package:model/oidc/request/oidc_request.dart';
|
||||
import 'package:model/oidc/response/oidc_response.dart';
|
||||
import 'package:model/oidc/token_id.dart';
|
||||
import 'package:model/oidc/token_oidc.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/extensions/authentication_token_extension.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/extensions/service_path_extension.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/extensions/token_response_extension.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/config/oidc_constant.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/endpoint.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/oidc_error.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
||||
|
||||
class OIDCHttpClient {
|
||||
|
||||
final DioClient _dioClient;
|
||||
final FlutterAppAuth _appAuth;
|
||||
|
||||
OIDCHttpClient(this._dioClient, this._appAuth);
|
||||
OIDCHttpClient(this._dioClient);
|
||||
|
||||
Future<OIDCResponse?> checkOIDCIsAvailable(OIDCRequest oidcRequest) async {
|
||||
final result = await _dioClient.get(
|
||||
@@ -54,59 +47,4 @@ class OIDCHttpClient {
|
||||
scopes: OIDCConstant.oidcScope
|
||||
);
|
||||
}
|
||||
|
||||
Future<TokenOIDC> getTokenOIDC(String clientId, String redirectUrl, String discoveryUrl, List<String> scopes) async {
|
||||
final authorizationTokenResponse = await _appAuth.authorizeAndExchangeCode(AuthorizationTokenRequest(
|
||||
clientId,
|
||||
redirectUrl,
|
||||
discoveryUrl: discoveryUrl,
|
||||
scopes: scopes,
|
||||
preferEphemeralSession: true));
|
||||
|
||||
log('OIDCHttpClient::getTokenOIDC(): token: ${authorizationTokenResponse?.accessToken}');
|
||||
|
||||
if (authorizationTokenResponse != null) {
|
||||
final tokenOIDC = authorizationTokenResponse.toTokenOIDC();
|
||||
if (tokenOIDC.isTokenValid()) {
|
||||
return tokenOIDC;
|
||||
} else {
|
||||
throw AccessTokenInvalidException();
|
||||
}
|
||||
} else {
|
||||
throw NotFoundAccessTokenException();
|
||||
}
|
||||
}
|
||||
|
||||
Future<TokenOIDC> refreshingTokensOIDC(String clientId, String redirectUrl,
|
||||
String discoveryUrl, List<String> scopes, String refreshToken) async {
|
||||
final tokenResponse = await _appAuth.token(TokenRequest(
|
||||
clientId,
|
||||
redirectUrl,
|
||||
discoveryUrl: discoveryUrl,
|
||||
refreshToken: refreshToken,
|
||||
scopes: scopes));
|
||||
|
||||
log('OIDCHttpClient::refreshingTokensOIDC(): refreshToken: ${tokenResponse?.accessToken}');
|
||||
|
||||
if (tokenResponse != null) {
|
||||
final tokenOIDC = tokenResponse.toTokenOIDC();
|
||||
if (tokenOIDC.isTokenValid()) {
|
||||
return tokenOIDC;
|
||||
} else {
|
||||
throw AccessTokenInvalidException();
|
||||
}
|
||||
} else {
|
||||
throw NotFoundAccessTokenException();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> logoutOidc(TokenId tokenId, OIDCConfiguration config) async {
|
||||
final endSession = await _appAuth.endSession(EndSessionRequest(
|
||||
idTokenHint: tokenId.uuid,
|
||||
postLogoutRedirectUrl: config.redirectUrl,
|
||||
discoveryUrl: config.discoveryUrl
|
||||
));
|
||||
log('OIDCHttpClient::logoutOidc(): ${endSession?.state}');
|
||||
return endSession?.state?.isNotEmpty == true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user