Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Ensure the AuthenticatorGateway does not cache a stale endpoint

vishnukvmd07b496be4c2f
diff --git a/auth/lib/gateway/authenticator.dart b/auth/lib/gateway/authenticator.dart
index 1ea1bee328..ee19c79b0d 100644
--- a/auth/lib/gateway/authenticator.dart
+++ b/auth/lib/gateway/authenticator.dart
@@ -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;
diff --git a/auth/lib/services/authenticator_service.dart b/auth/lib/services/authenticator_service.dart
index cdc9a4fb31..12e8b52e0b 100644
--- a/auth/lib/services/authenticator_service.dart
+++ b/auth/lib/services/authenticator_service.dart
@@ -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) {