@@ -78,11 +78,29 @@ class ServiceAccountCredential extends _OpenIdCredential
7878 @override
7979 final Certificate certificate;
8080
81- ServiceAccountCredential (serviceAccountPathOrObject)
82- : certificate = serviceAccountPathOrObject is String
83- ? Certificate .fromPath (serviceAccountPathOrObject)
84- : Certificate .fromJson (serviceAccountPathOrObject),
85- super (null , null ); // TODO two distinct constructors
81+ ServiceAccountCredential .fromJson (Map <String , dynamic > json)
82+ : certificate = Certificate .fromJson (json),
83+ super (json['client_id' ]! , null );
84+
85+ factory ServiceAccountCredential (serviceAccountPathOrObject) {
86+ {
87+ if (serviceAccountPathOrObject is Map ) {
88+ return ServiceAccountCredential .fromJson (
89+ serviceAccountPathOrObject.cast ());
90+ }
91+ try {
92+ return ServiceAccountCredential .fromJson (
93+ json.decode (File (serviceAccountPathOrObject).readAsStringSync ()));
94+ } on FirebaseException {
95+ rethrow ;
96+ } catch (error) {
97+ // Throw a nicely formed error message if the file contents cannot be parsed
98+ throw FirebaseAppError .invalidCredential (
99+ 'Failed to parse certificate key file: $error ' ,
100+ );
101+ }
102+ }
103+ }
86104
87105 String _createAuthJwt () {
88106 final claims = {
@@ -114,7 +132,7 @@ class ServiceAccountCredential extends _OpenIdCredential
114132}
115133
116134abstract class _OpenIdCredential implements Credential {
117- final String ? clientId;
135+ final String clientId;
118136 final String ? clientSecret;
119137
120138 _OpenIdCredential (this .clientId, this .clientSecret);
0 commit comments