diff --git a/lib/src/utils/secure_storage_repository.dart b/lib/src/utils/secure_storage_repository.dart index a3fb354..5886a53 100644 --- a/lib/src/utils/secure_storage_repository.dart +++ b/lib/src/utils/secure_storage_repository.dart @@ -23,9 +23,11 @@ class SecureStorageMiddleware implements SecureStorageRepository { final String? _credential; - SecureStorageMiddleware( - {required this.secureStorage, this.authMiddleware, String? credential}) - : androidOptions = const AndroidOptions( + SecureStorageMiddleware({ + required this.secureStorage, + this.authMiddleware, + String? credential, + }) : androidOptions = const AndroidOptions( encryptedSharedPreferences: true, ), iosOptions = const IOSOptions( @@ -33,91 +35,84 @@ class SecureStorageMiddleware implements SecureStorageRepository { ), _credential = credential; + Future _authenticate({required SSAuthOperationOptions options}) async { + if (!options.requiresAuth) return; + if (authMiddleware == null) throw SecureStorageAuthMiddlewareError(); + + return await authMiddleware?.authenticate( + localizedReason: options.authReason, + ); + } + @override - Future delete(String key, - {SSAuthOperationOptions? options = - const SSAuthOperationOptions()}) async { - if (options!.requiresAuth) { - if (authMiddleware == null) { - throw SecureStorageAuthMiddlewareError(); - } - await authMiddleware?.authenticate(localizedReason: options.authReason); - } + Future delete( + String key, { + SSAuthOperationOptions? options = const SSAuthOperationOptions(), + }) async { + await _authenticate(options: options!); await secureStorage.delete( - key: "${options.ssNameSpace ?? "vaariance"}_$key"); + key: "${options.ssNameSpace ?? "vaariance"}_$key", + ); } @override - Future read(String key, - {SSAuthOperationOptions? options = - const SSAuthOperationOptions()}) async { - if (options!.requiresAuth) { - if (authMiddleware == null) { - throw SecureStorageAuthMiddlewareError(); - } - await authMiddleware?.authenticate(localizedReason: options.authReason); - } + Future read( + String key, { + SSAuthOperationOptions? options = const SSAuthOperationOptions(), + }) async { + await _authenticate(options: options!); return await secureStorage.read( - key: "${options.ssNameSpace ?? "vaariance"}_$key"); + key: "${options.ssNameSpace ?? "vaariance"}_$key", + ); } @override - Future readCredential(CredentialType type, - {SSAuthOperationOptions? options = - const SSAuthOperationOptions()}) async { - if (options!.requiresAuth) { - if (authMiddleware == null) { - throw SecureStorageAuthMiddlewareError(); - } - await authMiddleware?.authenticate(localizedReason: options.authReason); - } + Future readCredential( + CredentialType type, { + SSAuthOperationOptions? options = const SSAuthOperationOptions(), + }) async { + await _authenticate(options: options!); return await secureStorage.read( - key: "${options.ssNameSpace ?? "vaariance"}_${type.name}"); + key: "${options.ssNameSpace ?? "vaariance"}_${type.name}", + ); } @override - Future save(String key, String value, - {SSAuthOperationOptions? options = - const SSAuthOperationOptions()}) async { - if (options!.requiresAuth) { - if (authMiddleware == null) { - throw SecureStorageAuthMiddlewareError(); - } - await authMiddleware?.authenticate(localizedReason: options.authReason); - } - + Future save( + String key, + String value, { + SSAuthOperationOptions? options = const SSAuthOperationOptions(), + }) async { + await _authenticate(options: options!); await secureStorage.write( - key: "${options.ssNameSpace ?? "vaariance"}_$key", value: value); + key: "${options.ssNameSpace ?? "vaariance"}_$key", + value: value, + ); } @override - Future saveCredential(CredentialType type, - {SSAuthOperationOptions? options = - const SSAuthOperationOptions()}) async { - if (options!.requiresAuth) { - if (authMiddleware == null) { - throw SecureStorageAuthMiddlewareError(); - } - await authMiddleware?.authenticate(localizedReason: options.authReason); - } + Future saveCredential( + CredentialType type, { + SSAuthOperationOptions? options = const SSAuthOperationOptions(), + }) async { + await _authenticate(options: options!); await secureStorage.write( - key: "${options.ssNameSpace ?? "vaariance"}_${type.name}", - value: _credential); + key: "${options.ssNameSpace ?? "vaariance"}_${type.name}", + value: _credential, + ); } @override - Future update(String key, String value, - {SSAuthOperationOptions? options = - const SSAuthOperationOptions()}) async { - if (options!.requiresAuth) { - if (authMiddleware == null) { - throw SecureStorageAuthMiddlewareError(); - } - await authMiddleware?.authenticate(localizedReason: options.authReason); - } - + Future update( + String key, + String value, { + SSAuthOperationOptions? options = const SSAuthOperationOptions(), + }) async { + await _authenticate(options: options!); await secureStorage.write( - key: "${options.ssNameSpace ?? "vaariance"}_$key", value: value); + key: "${options.ssNameSpace ?? "vaariance"}_$key", + value: value, + ); } }