-
Notifications
You must be signed in to change notification settings - Fork 4k
/
method_channel_firebase_app_check.dart
124 lines (107 loc) · 3.72 KB
/
method_channel_firebase_app_check.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import '../../firebase_app_check_platform_interface.dart';
import 'utils/exception.dart';
class MethodChannelFirebaseAppCheck extends FirebaseAppCheckPlatform {
/// Create an instance of [MethodChannelFirebaseAppCheck].
MethodChannelFirebaseAppCheck({required FirebaseApp app})
: super(appInstance: app) {
_tokenChangesListeners[app.name] = StreamController<String?>.broadcast();
channel.invokeMethod<String>('FirebaseAppCheck#registerTokenListener', {
'appName': app.name,
}).then((channelName) {
final events = EventChannel(channelName!, channel.codec);
events.receiveBroadcastStream().listen(
(arguments) {
// ignore: close_sinks
StreamController<String?> controller =
_tokenChangesListeners[app.name]!;
Map<dynamic, dynamic> result = arguments;
controller.add(result['token'] as String?);
},
);
});
}
static final Map<String, StreamController<String?>> _tokenChangesListeners =
{};
static Map<String, MethodChannelFirebaseAppCheck>
_methodChannelFirebaseAppCheckInstances =
<String, MethodChannelFirebaseAppCheck>{};
/// The [MethodChannel] used to communicate with the native plugin
static MethodChannel channel = const MethodChannel(
'plugins.flutter.io/firebase_app_check',
);
/// Returns a stub instance to allow the platform interface to access
/// the class instance statically.
static MethodChannelFirebaseAppCheck get instance {
return MethodChannelFirebaseAppCheck._();
}
/// Internal stub class initializer.
///
/// When the user code calls an auth method, the real instance is
/// then initialized via the [delegateFor] method.
MethodChannelFirebaseAppCheck._() : super(appInstance: null);
@override
FirebaseAppCheckPlatform delegateFor({required FirebaseApp app}) {
return _methodChannelFirebaseAppCheckInstances.putIfAbsent(app.name, () {
return MethodChannelFirebaseAppCheck(app: app);
});
}
@override
MethodChannelFirebaseAppCheck setInitialValues() {
return this;
}
@override
Future<void> activate({
String? webRecaptchaSiteKey,
bool? androidDebugProvider,
}) async {
try {
await channel.invokeMethod<void>('FirebaseAppCheck#activate', {
'appName': app.name,
if (Platform.isAndroid || kDebugMode)
'androidDebugProvider': androidDebugProvider,
});
} on PlatformException catch (e, s) {
throw platformExceptionToFirebaseException(e, s);
}
}
@override
Future<String?> getToken(bool forceRefresh) async {
try {
final result = await channel.invokeMapMethod(
'FirebaseAppCheck#getToken',
{'appName': app.name, 'forceRefresh': forceRefresh},
);
return result!['token'];
} on PlatformException catch (e, s) {
throw platformExceptionToFirebaseException(e, s);
}
}
@override
Future<void> setTokenAutoRefreshEnabled(
bool isTokenAutoRefreshEnabled,
) async {
try {
await channel.invokeMethod(
'FirebaseAppCheck#setTokenAutoRefreshEnabled',
{
'appName': app.name,
'isTokenAutoRefreshEnabled': isTokenAutoRefreshEnabled
},
);
} on PlatformException catch (e, s) {
throw platformExceptionToFirebaseException(e, s);
}
}
@override
Stream<String?> get onTokenChange {
return _tokenChangesListeners[app.name]!.stream;
}
}