import 'package:dio/dio.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 { late Dio _enteDio; AuthenticatorGateway() { _enteDio = Network.instance.enteDio; } Future createKey(String encKey, String header) async { await _enteDio.post( "/authenticator/key", data: { "encryptedKey": encKey, "header": header, }, ); } Future getKey() async { try { final response = await _enteDio.get("/authenticator/key"); return AuthKey.fromMap(response.data); } on DioException catch (e) { if (e.response != null && (e.response!.statusCode ?? 0) == 404) { throw AuthenticatorKeyNotFound(); } else { rethrow; } } catch (e) { rethrow; } } Future createEntity(String encryptedData, String header) async { final response = await _enteDio.post( "/authenticator/entity", data: { "encryptedData": encryptedData, "header": header, }, ); return AuthEntity.fromMap(response.data); } Future updateEntity( String id, String encryptedData, String header, ) async { await _enteDio.put( "/authenticator/entity", data: { "id": id, "encryptedData": encryptedData, "header": header, }, ); } Future deleteEntity( String id, ) async { await _enteDio.delete( "/authenticator/entity", queryParameters: { "id": id, }, ); } Future> getDiff(int sinceTime, {int limit = 500}) async { try { final response = await _enteDio.get( "/authenticator/entity/diff", queryParameters: { "sinceTime": sinceTime, "limit": limit, }, ); final List authEntities = []; final diff = response.data["diff"] as List; for (var entry in diff) { final AuthEntity entity = AuthEntity.fromMap(entry); authEntities.add(entity); } return authEntities; } catch (e) { if (e is DioException && e.response?.statusCode == 401) { throw UnauthorizedError(); } else { rethrow; } } } }