/
FlutterFirebaseAppCheckPlugin.java
252 lines (210 loc) 路 8.68 KB
/
FlutterFirebaseAppCheckPlugin.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 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.
package io.flutter.plugins.firebase.appcheck;
import static io.flutter.plugins.firebase.core.FlutterFirebasePluginRegistry.registerPlugin;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.appcheck.AppCheckTokenResult;
import com.google.firebase.appcheck.FirebaseAppCheck;
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory;
import com.google.firebase.appcheck.safetynet.SafetyNetAppCheckProviderFactory;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugins.firebase.core.FlutterFirebasePlugin;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class FlutterFirebaseAppCheckPlugin
implements FlutterFirebasePlugin, FlutterPlugin, MethodCallHandler {
private static final String METHOD_CHANNEL_NAME = "plugins.flutter.io/firebase_app_check";
private final Map<EventChannel, TokenChannelStreamHandler> streamHandlers = new HashMap<>();
@Nullable private BinaryMessenger messenger;
private MethodChannel channel;
private void initInstance(BinaryMessenger messenger) {
registerPlugin(METHOD_CHANNEL_NAME, this);
channel = new MethodChannel(messenger, METHOD_CHANNEL_NAME);
channel.setMethodCallHandler(this);
this.messenger = messenger;
}
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
initInstance(binding.getBinaryMessenger());
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
channel = null;
messenger = null;
removeEventListeners();
}
private FirebaseAppCheck getAppCheck(Map<String, Object> arguments) {
String appName = (String) Objects.requireNonNull(arguments.get("appName"));
FirebaseApp app = FirebaseApp.getInstance(appName);
return FirebaseAppCheck.getInstance(app);
}
private Map<String, Object> tokenResultToMap(AppCheckTokenResult result) {
Map<String, Object> output = new HashMap<>();
output.put("token", result.getToken());
return output;
}
private Task<Void> activate(Map<String, Object> arguments) {
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
cachedThreadPool.execute(
() -> {
try {
Boolean debug = (Boolean) arguments.get("androidDebugProvider");
if (Boolean.TRUE.equals(debug)) {
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance());
} else {
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
firebaseAppCheck.installAppCheckProviderFactory(
SafetyNetAppCheckProviderFactory.getInstance());
}
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});
return taskCompletionSource.getTask();
}
private Task<Map<String, Object>> getToken(Map<String, Object> arguments) {
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();
cachedThreadPool.execute(
() -> {
try {
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
Boolean forceRefresh = (Boolean) Objects.requireNonNull(arguments.get("forceRefresh"));
AppCheckTokenResult tokenResult = Tasks.await(firebaseAppCheck.getToken(forceRefresh));
taskCompletionSource.setResult(tokenResultToMap(tokenResult));
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});
return taskCompletionSource.getTask();
}
private Task<Void> setTokenAutoRefreshEnabled(Map<String, Object> arguments) {
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
cachedThreadPool.execute(
() -> {
try {
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
Boolean isTokenAutoRefreshEnabled =
(Boolean) Objects.requireNonNull(arguments.get("isTokenAutoRefreshEnabled"));
firebaseAppCheck.setTokenAutoRefreshEnabled(isTokenAutoRefreshEnabled);
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});
return taskCompletionSource.getTask();
}
private Task<String> registerTokenListener(Map<String, Object> arguments) {
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
cachedThreadPool.execute(
() -> {
try {
String appName = (String) Objects.requireNonNull(arguments.get("appName"));
FirebaseAppCheck firebaseAppCheck = getAppCheck(arguments);
final TokenChannelStreamHandler handler =
new TokenChannelStreamHandler(firebaseAppCheck);
final String name = METHOD_CHANNEL_NAME + "/token/" + appName;
final EventChannel channel = new EventChannel(messenger, name);
channel.setStreamHandler(handler);
streamHandlers.put(channel, handler);
taskCompletionSource.setResult(name);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});
return taskCompletionSource.getTask();
}
@Override
public void onMethodCall(MethodCall call, @NonNull final Result result) {
Task<?> methodCallTask;
switch (call.method) {
case "FirebaseAppCheck#activate":
methodCallTask = activate(call.arguments());
break;
case "FirebaseAppCheck#getToken":
methodCallTask = getToken(call.arguments());
break;
case "FirebaseAppCheck#setTokenAutoRefreshEnabled":
methodCallTask = setTokenAutoRefreshEnabled(call.arguments());
break;
case "FirebaseAppCheck#registerTokenListener":
methodCallTask = registerTokenListener(call.arguments());
break;
default:
result.notImplemented();
return;
}
methodCallTask.addOnCompleteListener(
task -> {
if (task.isSuccessful()) {
result.success(task.getResult());
} else {
Exception exception = task.getException();
result.error(
"firebase_app_check",
exception != null ? exception.getMessage() : null,
getExceptionDetails(exception));
}
});
}
private Map<String, Object> getExceptionDetails(@Nullable Exception exception) {
Map<String, Object> details = new HashMap<>();
details.put("code", "unknown");
if (exception != null) {
details.put("message", exception.getMessage());
} else {
details.put("message", "An unknown error has occurred.");
}
return details;
}
@Override
public Task<Map<String, Object>> getPluginConstantsForFirebaseApp(FirebaseApp firebaseApp) {
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();
cachedThreadPool.execute(
() -> {
try {
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});
return taskCompletionSource.getTask();
}
@Override
public Task<Void> didReinitializeFirebaseCore() {
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
cachedThreadPool.execute(
() -> {
try {
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});
return taskCompletionSource.getTask();
}
private void removeEventListeners() {
for (EventChannel eventChannel : streamHandlers.keySet()) {
EventChannel.StreamHandler streamHandler = streamHandlers.get(eventChannel);
streamHandler.onCancel(null);
eventChannel.setStreamHandler(null);
}
streamHandlers.clear();
}
}