-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgigya_flutter_plugin_method_channel.dart
450 lines (405 loc) · 13.5 KB
/
gigya_flutter_plugin_method_channel.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import '../models/enums/methods.dart';
import '../models/enums/social_provider.dart';
import '../models/gigya_error.dart';
import '../models/screenset_event.dart';
import '../services/biometric_service/biometric_service.dart';
import '../services/biometric_service/method_channel_biometric_service.dart';
import '../services/interruption_resolver/interruption_resolver.dart';
import '../services/interruption_resolver/method_channel_interruption_resolver.dart';
import '../services/otp_service/method_channel_otp_service.dart';
import '../services/otp_service/otp_service.dart';
import '../services/web_authentication_service/method_channel_web_authentication_service.dart';
import '../services/web_authentication_service/web_authentication_service.dart';
import 'gigya_flutter_plugin_platform_interface.dart';
/// An implementation of [GigyaFlutterPluginPlatform] that uses method channels.
class MethodChannelGigyaFlutterPlugin extends GigyaFlutterPluginPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final MethodChannel methodChannel = const MethodChannel(
'com.sap.gigya_flutter_plugin/methods',
);
/// The event channel that provides the stream of screen set events.
@visibleForTesting
final EventChannel screenSetEvents = const EventChannel(
'com.sap.gigya_flutter_plugin/screenSetEvents',
);
@override
InterruptionResolverFactory get interruptionResolverFactory {
return MethodChannelInterruptionResolverFactory(methodChannel);
}
@override
OtpService get otpService => MethodChannelOtpService(methodChannel);
@override
WebAuthenticationService get webAuthenticationService {
return MethodChannelWebAuthenticationService(methodChannel);
}
@override
BiometricService get biometricService {
return MethodChannelBiometricService(methodChannel);
}
@override
Future<Map<String, dynamic>> addConnection(SocialProvider provider) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.addConnection.methodName,
<String, dynamic>{'provider': provider.name},
).timeout(
Methods.addConnection.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> forgotPassword(String loginId) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.forgotPassword.methodName,
<String, dynamic>{'loginId': loginId},
).timeout(
Methods.forgotPassword.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> getAccount({
bool invalidate = false,
Map<String, dynamic> parameters = const <String, dynamic>{},
}) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.getAccount.methodName,
<String, dynamic>{
'invalidate': invalidate,
'parameters': parameters,
},
).timeout(
Methods.getAccount.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> getSession() async {
try {
final Map<String, dynamic>? result = await methodChannel
.invokeMapMethod<String, dynamic>(Methods.getSession.methodName)
.timeout(
Methods.getSession.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<void> initSdk({
required String apiDomain,
required String apiKey,
String? cname,
bool forceLogout = true,
}) async {
// First, initialize the Gigya SDK.
try {
await methodChannel.invokeMethod<void>(
Methods.initSdk.methodName,
<String, dynamic>{
'apiKey': apiKey,
'apiDomain': apiDomain,
'cname': cname
},
).timeout(
Methods.initSdk.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
// Then logout if requested.
if (forceLogout) {
await logout();
}
}
@override
Future<bool> isLoggedIn() async {
try {
final bool? result = await methodChannel.invokeMethod<bool>(
Methods.isLoggedIn.methodName,
);
return result ?? false;
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> linkToSite({
required String loginId,
required String password,
}) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.linkToSite.methodName,
<String, dynamic>{
'loginId': loginId,
'password': password,
},
).timeout(
Methods.linkToSite.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> login({
required String loginId,
required String password,
Map<String, dynamic> parameters = const <String, dynamic>{},
}) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.loginWithCredentials.methodName,
<String, dynamic>{
'loginId': loginId,
'password': password,
'parameters': parameters,
},
).timeout(
Methods.loginWithCredentials.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<void> logout() async {
try {
await methodChannel.invokeMethod<void>(Methods.logOut.methodName).timeout(
Methods.logOut.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> register({
required String loginId,
required String password,
Map<String, dynamic> parameters = const <String, dynamic>{},
}) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.registerWithCredentials.methodName,
<String, dynamic>{
'email': loginId,
'password': password,
'parameters': parameters,
},
).timeout(
Methods.registerWithCredentials.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> removeConnection(SocialProvider provider) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.removeConnection.methodName,
<String, dynamic>{'provider': provider.name},
).timeout(
Methods.removeConnection.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> send(
String endpoint, {
Map<String, dynamic> parameters = const <String, dynamic>{},
}) async {
try {
final String? json = await methodChannel.invokeMethod<String>(
Methods.sendRequest.methodName,
<String, dynamic>{
'endpoint': endpoint,
'parameters': parameters,
},
).timeout(
Methods.sendRequest.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
if (json == null) {
return const <String, dynamic>{};
}
return jsonDecode(json) as Map<String, dynamic>;
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> setAccount(Map<String, dynamic> account) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.setAccount.methodName,
<String, dynamic>{'account': account},
).timeout(
Methods.setAccount.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<void> setSession({
required int expiresIn,
required String sessionSecret,
required String sessionToken,
}) {
try {
return methodChannel.invokeMethod<void>(
Methods.setSession.methodName,
<String, dynamic>{
'sessionToken': sessionToken,
'sessionSecret': sessionSecret,
'expires_in': expiresIn,
},
).timeout(
Methods.setSession.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Stream<ScreensetEvent> showScreenSet(
String name, {
Map<String, dynamic> parameters = const <String, dynamic>{},
bool isDebug = false,
}) async* {
try {
await methodChannel.invokeMethod<void>(
Methods.showScreenSet.methodName,
<String, dynamic>{
'screenSet': name,
'parameters': parameters,
'isDebug': isDebug,
},
);
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
yield* screenSetEvents.receiveBroadcastStream().map((dynamic event) {
// The binary messenger sends things back as `dynamic`.
// If the event is a `Map`,
// it does not have type information and comes back as `Map<Object?, Object?>`.
// Cast it using `Map.cast()` to at least recover the type of the key.
// The values are still `Object?`, though.
final Map<String, Object?> typedEvent =
(event as Map<Object?, Object?>).cast<String, Object?>();
// Now grab the data of the event,
// using `Map.cast()` to recover the type of the keys.
final Map<String, Object?>? data =
(typedEvent['data'] as Map<Object?, Object?>?)
?.cast<String, Object?>();
return ScreensetEvent(
typedEvent['event'] as String,
data ?? <String, Object?>{},
);
});
}
@override
Future<void> dismissScreenSet() async {
try {
await methodChannel
.invokeMethod<void>(Methods.dismissScreenSet.methodName);
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> socialLogin(
SocialProvider provider, {
Map<String, dynamic> parameters = const <String, dynamic>{},
}) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.socialLogin.methodName,
<String, dynamic>{
'provider': provider.name,
'parameters': parameters,
},
).timeout(
Methods.socialLogin.timeout,
onTimeout: () => throw const GigyaTimeoutError(),
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<Map<String, dynamic>> sso({
Map<String, dynamic> parameters = const <String, dynamic>{},
}) async {
try {
final Map<String, dynamic>? result =
await methodChannel.invokeMapMethod<String, dynamic>(
Methods.sso.methodName,
<String, dynamic>{
'provider': 'sso',
'parameters': parameters,
},
);
return result ?? const <String, dynamic>{};
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
@override
Future<String?> getAuthCode() async {
try {
final String? result = await methodChannel
.invokeMethod<String>(Methods.getAuthCode.methodName);
return result;
} on PlatformException catch (exception) {
throw GigyaError.fromPlatformException(exception);
}
}
}