From 727aeb13fee1e1aaff66417b9e63764a4c20eaa5 Mon Sep 17 00:00:00 2001 From: philthefox <43797286+philthefox@users.noreply.github.com> Date: Sun, 26 Jan 2025 11:21:05 +0100 Subject: [PATCH] #4810 Add support for custom service account JSON in createAuthClient --- lib/src/util/google_cloud_helpers_dart.dart | 16 ++++++++++++---- lib/src/util/google_cloud_helpers_flutter.dart | 4 +++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/src/util/google_cloud_helpers_dart.dart b/lib/src/util/google_cloud_helpers_dart.dart index 7ec0c779..bff56fe9 100644 --- a/lib/src/util/google_cloud_helpers_dart.dart +++ b/lib/src/util/google_cloud_helpers_dart.dart @@ -52,12 +52,20 @@ class GoogleCloudHelpers { /// /// The [scopes] need to be set, to the services you want to use. E.g. `FirestoreApi.datastoreScope`. static Future createAuthClient( - {required List scopes, String? pathToServiceAccountJson}) async { - if (pathToServiceAccountJson == null) { + {required List scopes, + String? pathToServiceAccountJson, + String? serviceAccountJsonRaw}) async { + assert(!(pathToServiceAccountJson != null && serviceAccountJsonRaw != null), + 'Only one of pathToServiceAccountJson or serviceAccountJson can be provided.'); + + if (pathToServiceAccountJson == null && serviceAccountJsonRaw == null) { return await clientViaApplicationDefaultCredentials(scopes: scopes); } else { - final serviceAccountJson = - jsonDecode(await File(pathToServiceAccountJson).readAsString()); + final serviceAccountJson = pathToServiceAccountJson != null + ? jsonDecode(await File(pathToServiceAccountJson).readAsString()) + : serviceAccountJsonRaw != null + ? jsonDecode(serviceAccountJsonRaw) + : null; final accountCredentials = ServiceAccountCredentials.fromJson(serviceAccountJson); diff --git a/lib/src/util/google_cloud_helpers_flutter.dart b/lib/src/util/google_cloud_helpers_flutter.dart index 57be370b..4664f96b 100644 --- a/lib/src/util/google_cloud_helpers_flutter.dart +++ b/lib/src/util/google_cloud_helpers_flutter.dart @@ -89,7 +89,9 @@ class GoogleCloudHelpers { /// Just a stub for now. See google_cloud_helpers_dart.dart for documentation. static Future createAuthClient( - {required List scopes, String? pathToServiceAccountJson}) async { + {required List scopes, + String? pathToServiceAccountJson, + String? serviceAccountJsonRaw}) async { throw UnimplementedError(); }