Skip to content

Commit

Permalink
feat: Use GOOGLE_APPLICATION_CREDENTIALS if json value (#32)
Browse files Browse the repository at this point in the history
* Check GOOGLE_APPLICATION_CREDENTIALS env-var in fromApplicationDefaultCredentials

* tiny fix

* check if decoded env-var is Map type

* add test for :fromApplicationDefaultCredentials

* add test for invalid service account json

* remove platform dependency

* Apply suggestions from code review

* hide envSymbol

---------

Co-authored-by: Remi Rousselet <darky12s@gmail.com>
  • Loading branch information
codekeyz and rrousselGit committed May 6, 2024
1 parent 98cf2d1 commit 5003b77
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/dart_firebase_admin/lib/dart_firebase_admin.dart
@@ -1 +1 @@
export 'src/app.dart';
export 'src/app.dart' hide envSymbol;
30 changes: 25 additions & 5 deletions packages/dart_firebase_admin/lib/src/app/credential.dart
@@ -1,5 +1,8 @@
part of '../app.dart';

@internal
const envSymbol = #_envSymbol;

/// Authentication information for Firebase Admin SDK.
class Credential {
Credential._(
Expand Down Expand Up @@ -41,11 +44,28 @@ class Credential {
}

/// Log in to firebase using the environment variable.
Credential.fromApplicationDefaultCredentials({String? serviceAccountId})
: this._(
null,
serviceAccountId: serviceAccountId,
);
factory Credential.fromApplicationDefaultCredentials({
String? serviceAccountId,
}) {
ServiceAccountCredentials? creds;

final env =
Zone.current[envSymbol] as Map<String, String>? ?? Platform.environment;
final maybeConfig = env['GOOGLE_APPLICATION_CREDENTIALS'];
if (maybeConfig != null) {
try {
final decodedValue = jsonDecode(maybeConfig);
if (decodedValue is Map) {
creds = ServiceAccountCredentials.fromJson(decodedValue);
}
} on FormatException catch (_) {}
}

return Credential._(
creds,
serviceAccountId: serviceAccountId,
);
}

@internal
final String? serviceAccountId;
Expand Down
41 changes: 41 additions & 0 deletions packages/dart_firebase_admin/test/credential_test.dart
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -66,5 +67,45 @@ void main() {
Credential.fromServiceAccount(fs.file('service-account.json'));
});
});

group('fromApplicationDefaultCredentials', () {
test(
'completes if `GOOGLE_APPLICATION_CREDENTIALS` environment-variable is valid service account JSON',
() {
final fakeServiceAccount = {
'GOOGLE_APPLICATION_CREDENTIALS': '''
{
"type": "service_account",
"client_id": "id",
"private_key": ${jsonEncode(_fakeRSAKey)},
"client_email": "foo@bar.com"
}
''',
};
final credential = runZoned(
Credential.fromApplicationDefaultCredentials,
zoneValues: {envSymbol: fakeServiceAccount},
);
expect(credential.serviceAccountCredentials, isNotNull);

// Verify if service account is actually being used
expect(
credential.serviceAccountCredentials!.email,
'foo@bar.com',
);
});

test(
'does nothing if `GOOGLE_APPLICATION_CREDENTIALS` environment-variable is not valid service account JSON',
() {
final credential = runZoned(
Credential.fromApplicationDefaultCredentials,
zoneValues: {
envSymbol: {'GOOGLE_APPLICATION_CREDENTIALS': ''},
},
);
expect(credential.serviceAccountCredentials, isNull);
});
});
});
}

0 comments on commit 5003b77

Please sign in to comment.