瀏覽代碼

Ensure the AuthenticatorGateway does not cache a stale endpoint

vishnukvmd 1 年之前
父節點
當前提交
07b496be4c
共有 2 個文件被更改,包括 20 次插入54 次删除
  1. 15 49
      auth/lib/gateway/authenticator.dart
  2. 5 5
      auth/lib/services/authenticator_service.dart

+ 15 - 49
auth/lib/gateway/authenticator.dart

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

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