Skip to content

Commit

Permalink
Fix-CI (#40)
Browse files Browse the repository at this point in the history
Also fix warning when using emulator

fixes #38
  • Loading branch information
rrousselGit authored Jul 9, 2024
1 parent 0e5d031 commit 1b8423c
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 3,662 deletions.
1 change: 1 addition & 0 deletions packages/dart_firebase_admin/lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:io';
import 'package:firebaseapis/identitytoolkit/v3.dart' as auth3;
import 'package:googleapis_auth/auth_io.dart' as auth;
import 'package:googleapis_auth/googleapis_auth.dart';
import 'package:http/http.dart';
import 'package:meta/meta.dart';

part 'app/credential.dart';
Expand Down
57 changes: 40 additions & 17 deletions packages/dart_firebase_admin/lib/src/app/credential.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@ part of '../app.dart';
@internal
const envSymbol = #_envSymbol;

class _RequestImpl extends BaseRequest {
_RequestImpl(super.method, super.url, [Stream<List<int>>? stream])
: _stream = stream ?? const Stream.empty();

final Stream<List<int>> _stream;

@override
ByteStream finalize() {
super.finalize();
return ByteStream(_stream);
}
}

/// Will close the underlying `http.Client` depending on a constructor argument.
class _EmulatorClient extends BaseClient {
_EmulatorClient(this.client);

final Client client;

@override
Future<StreamedResponse> send(BaseRequest request) async {
// Make new request object and perform the authenticated request.
final modifiedRequest = _RequestImpl(
request.method,
request.url,
request.finalize(),
);
modifiedRequest.headers.addAll(request.headers);
modifiedRequest.headers['Authorization'] = 'Bearer owner';

return client.send(modifiedRequest);
}

@override
void close() {
client.close();
super.close();
}
}

/// Authentication information for Firebase Admin SDK.
class Credential {
Credential._(
Expand Down Expand Up @@ -72,21 +112,4 @@ class Credential {

@internal
final auth.ServiceAccountCredentials? serviceAccountCredentials;

@internal
late final client = _getClient(
[
auth3.IdentityToolkitApi.cloudPlatformScope,
auth3.IdentityToolkitApi.firebaseScope,
],
);

Future<AutoRefreshingAuthClient> _getClient(List<String> scopes) async {
final serviceAccountCredentials = this.serviceAccountCredentials;
final client = serviceAccountCredentials == null
? await auth.clientViaApplicationDefaultCredentials(scopes: scopes)
: await auth.clientViaServiceAccount(serviceAccountCredentials, scopes);

return client;
}
}
2 changes: 1 addition & 1 deletion packages/dart_firebase_admin/lib/src/app/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ String _platformErrorCodeMessage(String code) {
}

/// Base interface for all Firebase Admin related errors.
abstract class FirebaseAdminException {
abstract class FirebaseAdminException implements Exception {
FirebaseAdminException(this.service, this._code, [this._message]);

final String service;
Expand Down
23 changes: 22 additions & 1 deletion packages/dart_firebase_admin/lib/src/app/firebase_admin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,30 @@ class FirebaseAdminApp {
firestoreApiHost = Uri.http('127.0.0.1:8080', '/');
}

@internal
late final client = _getClient(
[
auth3.IdentityToolkitApi.cloudPlatformScope,
auth3.IdentityToolkitApi.firebaseScope,
],
);

Future<Client> _getClient(List<String> scopes) async {
if (isUsingEmulator) {
return _EmulatorClient(Client());
}

final serviceAccountCredentials = credential.serviceAccountCredentials;
final client = serviceAccountCredentials == null
? await auth.clientViaApplicationDefaultCredentials(scopes: scopes)
: await auth.clientViaServiceAccount(serviceAccountCredentials, scopes);

return client;
}

/// Stops the app and releases any resources associated with it.
Future<void> close() async {
final client = await credential.client;
final client = await this.client;
client.close();
}
}
2 changes: 1 addition & 1 deletion packages/dart_firebase_admin/lib/src/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:firebaseapis/identitytoolkit/v1.dart' as v1;
import 'package:firebaseapis/identitytoolkit/v2.dart' as auth2;
import 'package:firebaseapis/identitytoolkit/v2.dart' as v2;
import 'package:firebaseapis/identitytoolkit/v3.dart' as auth3;
import 'package:googleapis_auth/googleapis_auth.dart';
import 'package:http/http.dart';
import 'package:meta/meta.dart';

import 'app.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,6 @@ class _AuthHttpClient {
_AuthHttpClient(this.app);

// TODO handle tenants
// TODO needs to send "owner" as bearer token when using the emulator
final FirebaseAdminApp app;

String _buildParent() => 'projects/${app.projectId}';
Expand Down Expand Up @@ -1012,9 +1011,9 @@ class _AuthHttpClient {
}

Future<R> _run<R>(
Future<R> Function(AutoRefreshingAuthClient client) fn,
Future<R> Function(Client client) fn,
) {
return _authGuard(() => app.credential.client.then(fn));
return _authGuard(() => app.client.then(fn));
}

Future<R> v1<R>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:firebaseapis/firestore/v1.dart' as firestore1;
import 'package:firebaseapis/firestore/v1beta1.dart' as firestore1beta1;
import 'package:firebaseapis/firestore/v1beta2.dart' as firestore1beta2;
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:googleapis_auth/googleapis_auth.dart';
import 'package:http/http.dart';
import 'package:intl/intl.dart';

import '../app.dart';
Expand Down Expand Up @@ -206,9 +206,9 @@ class _FirestoreHttpClient {
// TODO refactor with auth
// TODO is it fine to use AuthClient?
Future<R> _run<R>(
Future<R> Function(AutoRefreshingAuthClient client) fn,
Future<R> Function(Client client) fn,
) {
return _firestoreGuard(() => app.credential.client.then(fn));
return _firestoreGuard(() => app.client.then(fn));
}

Future<R> v1<R>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class CollectionReference<T> extends Query<T> {
/// final documentRef = collectionRef.parent;
/// print('Parent name: ${documentRef.path}');
/// ```
DocumentReference<T>? get parent {
DocumentReference<DocumentData>? get parent {
if (!_queryOptions.parentPath.isDocument) return null;

return DocumentReference<T>._(
return DocumentReference<DocumentData>._(
firestore: firestore,
path: _queryOptions.parentPath as _QualifiedResourcePath,
converter: _queryOptions.converter,
converter: _jsonConverter,
);
}

Expand Down
1 change: 0 additions & 1 deletion packages/dart_firebase_admin/lib/src/messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:firebaseapis/fcm/v1.dart' as fmc1;
import 'package:googleapis_auth/auth_io.dart';
import 'package:http/http.dart';
import 'package:meta/meta.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class FirebaseMessagingRequestHandler {
final FirebaseAdminApp firebase;

Future<R> _run<R>(
Future<R> Function(AutoRefreshingAuthClient client) fn,
Future<R> Function(Client client) fn,
) {
return _fmcGuard(() => firebase.credential.client.then(fn));
return _fmcGuard(() => firebase.client.then(fn));
}

Future<T> _fmcGuard<T>(
Expand Down Expand Up @@ -45,15 +45,14 @@ class FirebaseMessagingRequestHandler {
Object? requestData,
}) async {
try {
final client = await firebase.credential.client;
final client = await firebase.client;

final response = await client.post(
Uri.https(host, path),
body: jsonEncode(requestData),
headers: {
..._legacyFirebaseMessagingHeaders,
'content-type': 'application/json',
'Authorization': 'Bearer ${client.credentials.accessToken.data}',
},
);

Expand Down
Loading

0 comments on commit 1b8423c

Please sign in to comment.