Skip to content

Commit

Permalink
SSO lock
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Apr 30, 2024
1 parent 2ad0c84 commit aff97ea
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
9 changes: 3 additions & 6 deletions lib/network/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:fk_user_agent/fk_user_agent.dart';
import 'package:flutter/foundation.dart';
import 'package:sit/r.dart';
import 'package:sit/session/sso.dart';

final _rand = Random();

Expand All @@ -19,12 +18,10 @@ class DioInit {
if (!kIsWeb) {
dio.interceptors.add(CookieManager(cookieJar));
}
if (kDebugMode) {
dio.interceptors.add(LogInterceptor(logPrint: (obj) {
networkLogger.i(obj);
}));
}
if (kDebugMode && R.debugNetwork) {
dio.interceptors.add(LogInterceptor());
}
if (kDebugMode && R.debugNetwork && R.poorNetworkSimulation) {
dio.interceptors.add(PoorNetworkDioInterceptor());
}
if (config != null) {
Expand Down
1 change: 1 addition & 0 deletions lib/r.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class R {
static const debugCupertino = kDebugMode ? false : false;

static const debugNetwork = true;
static const poorNetworkSimulation = true;

/// The default window size is small enough for any modern desktop device.
static const Size defaultWindowSize = Size(500, 800);
Expand Down
39 changes: 26 additions & 13 deletions lib/session/sso.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class SsoSession {
/// Lock it to prevent simultaneous login.
static final _loginLock = Lock();

/// Lock all requests through SSO.
static final _ssoLock = Lock();

SsoSession({
required this.dio,
required this.cookieJar,
Expand Down Expand Up @@ -110,7 +113,6 @@ class SsoSession {
ProgressCallback? onReceiveProgress,
}) async {
options ??= Options();

Future<Response> requestNormally() async {
final response = await dio.request(
url,
Expand All @@ -125,7 +127,14 @@ class SsoSession {
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
return await processRedirect(dio, response, headers: _neededHeaders);
final debugDepths = <Response>[];
final finalResponse = await processRedirect(
dio,
response,
headers: _neededHeaders,
debugDepths: debugDepths,
);
return finalResponse;
}

// request normally at first
Expand Down Expand Up @@ -327,17 +336,21 @@ class SsoSession {
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
try {
return await _request(
url,
queryParameters: para,
data: data,
options: options,
);
} catch (error, stackTrace) {
debugPrintError(error, stackTrace);
rethrow;
}
networkLogger.i("$SsoSession.request ${DateTime.now().toIso8601String()}");
return await _ssoLock.synchronized<Response>(() async {
networkLogger.i("$SsoSession.request-synchronized ${DateTime.now().toIso8601String()}");
try {
return await _request(
url,
queryParameters: para,
data: data,
options: options,
);
} catch (error, stackTrace) {
debugPrintError(error, stackTrace);
rethrow;
}
});
}
}

Expand Down
20 changes: 12 additions & 8 deletions lib/utils/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ Future<Response> processRedirect(
Dio dio,
Response response, {
Map<String, dynamic>? headers,
List<Response>? debugDepths,
}) async {
//Prevent the redirect being processed by HttpClient, with the 302 response caught manually.
debugDepths?.add(response);
// Prevent the redirect being processed by HttpClient, with the 302 response caught manually.
if (response.statusCode == 302 && response.headers['location'] != null && response.headers['location']!.isNotEmpty) {
String location = response.headers['location']![0];
if (location.isEmpty) return response;
if (!Uri.parse(location).isAbsolute) {
location = '${response.requestOptions.uri.origin}/$location';
}
final redirectedResponse = await dio.get(
location,
options: disableRedirectFormEncodedOptions(
responseType: response.requestOptions.responseType,
headers: headers,
),
);
return processRedirect(
dio,
await dio.get(
location,
options: disableRedirectFormEncodedOptions(
responseType: response.requestOptions.responseType,
headers: headers,
),
),
redirectedResponse,
headers: headers,
debugDepths: debugDepths,
);
} else {
return response;
Expand Down

0 comments on commit aff97ea

Please sign in to comment.