Ensure the AuthenticatorGateway does not cache a stale endpoint

This commit is contained in:
vishnukvmd 2024-03-06 20:33:47 +05:30
parent a45129b75b
commit 07b496be4c
2 changed files with 20 additions and 54 deletions

View file

@ -1,43 +1,29 @@
import 'package:dio/dio.dart';
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/core/errors.dart';
import 'package:ente_auth/core/network.dart';
import 'package:ente_auth/models/authenticator/auth_entity.dart';
import 'package:ente_auth/models/authenticator/auth_key.dart';
class AuthenticatorGateway {
final Dio _dio;
final Configuration _config;
late String _basedEndpoint;
late Dio _enteDio;
AuthenticatorGateway(this._dio, this._config) {
_basedEndpoint = _config.getHttpEndpoint() + "/authenticator";
AuthenticatorGateway() {
_enteDio = Network.instance.enteDio;
}
Future<void> createKey(String encKey, String header) async {
await _dio.post(
_basedEndpoint + "/key",
await _enteDio.post(
"/authenticator/key",
data: {
"encryptedKey": encKey,
"header": header,
},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
}
Future<AuthKey> getKey() async {
try {
final response = await _dio.get(
_basedEndpoint + "/key",
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
final response = await _enteDio.get("/authenticator/key");
return AuthKey.fromMap(response.data);
} on DioError catch (e) {
if (e.response != null && (e.response!.statusCode ?? 0) == 404) {
@ -51,17 +37,12 @@ class AuthenticatorGateway {
}
Future<AuthEntity> createEntity(String encryptedData, String header) async {
final response = await _dio.post(
_basedEndpoint + "/entity",
final response = await _enteDio.post(
"/authenticator/entity",
data: {
"encryptedData": encryptedData,
"header": header,
},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
return AuthEntity.fromMap(response.data);
}
@ -71,50 +52,35 @@ class AuthenticatorGateway {
String encryptedData,
String header,
) async {
await _dio.put(
_basedEndpoint + "/entity",
await _enteDio.put(
"/authenticator/entity",
data: {
"id": id,
"encryptedData": encryptedData,
"header": header,
},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
}
Future<void> deleteEntity(
String id,
) async {
await _dio.delete(
_basedEndpoint + "/entity",
await _enteDio.delete(
"/authenticator/entity",
queryParameters: {
"id": id,
},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
}
Future<List<AuthEntity>> getDiff(int sinceTime, {int limit = 500}) async {
try {
final response = await _dio.get(
_basedEndpoint + "/entity/diff",
final response = await _enteDio.get(
"/authenticator/entity/diff",
queryParameters: {
"sinceTime": sinceTime,
"limit": limit,
},
options: Options(
headers: {
"X-Auth-Token": _config.getToken(),
},
),
);
final List<AuthEntity> authEntities = <AuthEntity>[];
final diff = response.data["diff"] as List;

View file

@ -5,7 +5,6 @@ import 'dart:math';
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/core/errors.dart';
import 'package:ente_auth/core/event_bus.dart';
import 'package:ente_auth/core/network.dart';
import 'package:ente_auth/events/codes_updated_event.dart';
import 'package:ente_auth/events/signed_in_event.dart';
import 'package:ente_auth/events/trigger_logout_event.dart';
@ -26,6 +25,7 @@ enum AccountMode {
online,
offline,
}
extension on AccountMode {
bool get isOnline => this == AccountMode.online;
bool get isOffline => this == AccountMode.offline;
@ -56,7 +56,7 @@ class AuthenticatorService {
_prefs = await SharedPreferences.getInstance();
_db = AuthenticatorDB.instance;
_offlineDb = OfflineAuthenticatorDB.instance;
_gateway = AuthenticatorGateway(Network.instance.getDio(), _config);
_gateway = AuthenticatorGateway();
if (Configuration.instance.hasConfiguredAccount()) {
unawaited(onlineSync());
}
@ -154,7 +154,7 @@ class AuthenticatorService {
} else {
debugPrint("Skipping delete since account mode is offline");
}
if(accountMode.isOnline) {
if (accountMode.isOnline) {
await _db.deleteByIDs(generatedIDs: [genID]);
} else {
await _offlineDb.deleteByIDs(generatedIDs: [genID]);
@ -163,7 +163,7 @@ class AuthenticatorService {
Future<bool> onlineSync() async {
try {
if(getAccountMode().isOffline) {
if (getAccountMode().isOffline) {
debugPrint("Skipping sync since account mode is offline");
return false;
}
@ -253,7 +253,7 @@ class AuthenticatorService {
}
Future<Uint8List> getOrCreateAuthDataKey(AccountMode mode) async {
if(mode.isOffline) {
if (mode.isOffline) {
return _config.getOfflineSecretKey()!;
}
if (_config.getAuthSecretKey() != null) {