Skip to content

Commit 9b6d124

Browse files
committed
fix for latest openid_client
1 parent 762d137 commit 9b6d124

File tree

6 files changed

+39
-29
lines changed

6 files changed

+39
-29
lines changed

lib/src/app/app_extension.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import 'dart:io';
42

53
import 'package:firebase_admin/src/auth/credential.dart';
@@ -8,26 +6,30 @@ import '../../firebase_admin.dart';
86
import '../credential.dart';
97

108
extension GetProjectIdExtension on App {
11-
String? get projectId => _getProjectId(this);
9+
String get projectId => _getProjectId(this);
1210
}
1311

14-
String? _getProjectId(App app) {
12+
String _getProjectId(App app) {
1513
final options = app.options;
1614
if (options.projectId != null && options.projectId!.isNotEmpty) {
17-
return options.projectId;
15+
return options.projectId!;
1816
}
1917

2018
final cert = _tryGetCertificate(options.credential);
2119
if (cert != null && cert.projectId != null && cert.projectId!.isNotEmpty) {
22-
return cert.projectId;
20+
return cert.projectId!;
2321
}
2422

2523
final projectId = Platform.environment['GOOGLE_CLOUD_PROJECT'] ??
2624
Platform.environment['GCLOUD_PROJECT'];
2725
if (projectId != null && projectId.isNotEmpty) {
2826
return projectId;
2927
}
30-
return null;
28+
29+
throw FirebaseAuthError.invalidCredential(
30+
'Must initialize app with a cert credential or set your Firebase project ID as the '
31+
'GOOGLE_CLOUD_PROJECT environment variable.',
32+
);
3133
}
3234

3335
Certificate? _tryGetCertificate(Credential credential) {

lib/src/auth/credential.dart

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

116134
abstract class _OpenIdCredential implements Credential {
117-
final String? clientId;
135+
final String clientId;
118136
final String? clientSecret;
119137

120138
_OpenIdCredential(this.clientId, this.clientSecret);

lib/src/auth/token_verifier.dart

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,14 @@ import '../utils/validator.dart' as validator;
1010
/// This verifies ID tokens and session cookies.
1111
class FirebaseTokenVerifier {
1212
final App app;
13-
final String? projectId;
13+
final String projectId;
1414

15-
final String _verifyApiName = 'verifyIdToken()';
1615
final String _jwtName = 'ID token';
1716

1817
static FirebaseTokenVerifier Function(App app) factory =
1918
(app) => FirebaseTokenVerifier(app);
2019

21-
FirebaseTokenVerifier(this.app) : projectId = app.projectId {
22-
if (projectId == null) {
23-
throw FirebaseAuthError.invalidCredential(
24-
'Must initialize app with a cert credential or set your Firebase project ID as the '
25-
'GOOGLE_CLOUD_PROJECT environment variable to call $_verifyApiName.',
26-
);
27-
}
28-
}
20+
FirebaseTokenVerifier(this.app) : projectId = app.projectId;
2921

3022
/// Verifies the format and signature of a Firebase Auth JWT token.
3123
Future<IdToken> verifyJwt(String jwtToken) async {
@@ -48,7 +40,7 @@ class FirebaseTokenVerifier {
4840

4941
@visibleForTesting
5042
Future<Client> getOpenIdClient() async {
51-
var issuer = await Issuer.discover(Issuer.firebase(projectId!));
43+
var issuer = await Issuer.discover(Issuer.firebase(projectId));
5244
return Client(issuer, projectId);
5345
}
5446
}

lib/src/storage.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import 'package:firebase_admin/firebase_admin.dart';
42
import 'package:firebase_admin/src/utils/api_request.dart';
53
import 'package:gcloud/storage.dart' as gcloud;
@@ -17,7 +15,7 @@ class Storage implements FirebaseService {
1715

1816
Storage(this.app)
1917
: storageClient = gcloud.Storage(
20-
AuthorizedHttpClient(app, Duration(seconds: 25)), app.projectId!);
18+
AuthorizedHttpClient(app, Duration(seconds: 25)), app.projectId);
2119

2220
@override
2321
Future<void> delete() async {}

lib/src/testing.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ServiceAccountMockCredential extends ServiceAccountCredential
2424
});
2525
};
2626
ServiceAccountMockCredential()
27-
: super({
27+
: super.fromJson({
2828
'type': 'service_account',
2929
'project_id': 'project_id',
3030
'private_key_id': 'aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd',

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies:
1111
x509: ^0.2.2
1212
jose: ^0.3.2
1313
dotenv: ^3.0.0
14-
openid_client: ^0.4.1
14+
openid_client: ^0.4.3
1515
firebase_dart: ^1.0.0-dev.45
1616
path: ^1.8.0
1717
meta: ^1.4.0

0 commit comments

Comments
 (0)