41 lines
1.1 KiB
Dart
41 lines
1.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class FirebaseAuthVerifier {
|
|
FirebaseAuthVerifier(this._webApiKey);
|
|
|
|
final String _webApiKey;
|
|
|
|
Future<String?> verifyBearerToken(String? authorization) async {
|
|
if (authorization == null || !authorization.startsWith('Bearer ')) {
|
|
return null;
|
|
}
|
|
final String idToken = authorization.substring('Bearer '.length).trim();
|
|
if (idToken.isEmpty) {
|
|
return null;
|
|
}
|
|
|
|
final Uri uri = Uri.parse(
|
|
'https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=$_webApiKey',
|
|
);
|
|
final http.Response response = await http.post(
|
|
uri,
|
|
headers: const <String, String>{'Content-Type': 'application/json'},
|
|
body: jsonEncode(<String, dynamic>{'idToken': idToken}),
|
|
);
|
|
|
|
if (response.statusCode != 200) {
|
|
return null;
|
|
}
|
|
|
|
final Map<String, dynamic> data =
|
|
jsonDecode(response.body) as Map<String, dynamic>;
|
|
final List<dynamic> users = data['users'] as List<dynamic>? ?? <dynamic>[];
|
|
if (users.isEmpty) {
|
|
return null;
|
|
}
|
|
return users.first['localId'] as String?;
|
|
}
|
|
}
|