-
Notifications
You must be signed in to change notification settings - Fork 1
/
home_bloc.dart
364 lines (334 loc) · 13.1 KB
/
home_bloc.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
// Copyright © 2022 Nevis Security AG. All rights reserved.
import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:nevis_mobile_authentication_sdk/nevis_mobile_authentication_sdk.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/configuration/configuration_loader.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/configuration/model/app_environment.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/blocs/local_data/local_data_bloc.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/blocs/local_data/local_data_state.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/client_provider/client_provider.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/error/error_handler.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/model/error/error.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/model/operation/operation_type.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/repository/deep_link_repository.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/usecase/authenticators_usecase.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/usecase/change_password_usecase.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/usecase/change_pin_usecase.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/usecase/delete_authenticators_usecase.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/usecase/deregister_all_usecase.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/usecase/oob_process_usecase.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/usecase/registered_accounts_usecase.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/navigation/global_navigation_manager.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/ui/screens/credential/navigation/credential_parameter.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/ui/screens/home/home_event.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/ui/screens/home/home_state.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/ui/screens/select_account/navigation/select_account_parameter.dart';
import 'package:permission_handler/permission_handler.dart';
@injectable
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final DeepLinkRepository _deepLinkRepository;
final ConfigurationLoader _configurationLoader;
final ClientProvider _clientProvider;
final OobProcessUseCase _oobProcessUseCase;
final RegisteredAccountsUseCase _registeredAccountsUseCase;
final DeregisterAllUseCase _deregisterAllUseCase;
final AuthenticatorsUseCase _authenticatorsUseCase;
final ChangePinUseCase _changePinUseCase;
final ChangePasswordUseCase _changePasswordUseCase;
final DeleteAuthenticatorsUseCase _deleteAuthenticatorsUseCase;
final LocalDataBloc _localDataBloc;
final ErrorHandler _errorHandler;
final GlobalNavigationManager _globalNavigationManager;
late StreamSubscription _localDataSubscription;
late StreamSubscription _deepLinkRepositorySubscription;
Set<Account> _registeredAccounts = {};
Set<Authenticator> _authenticators = {};
HomeBloc(
this._deepLinkRepository,
this._configurationLoader,
this._clientProvider,
this._oobProcessUseCase,
this._registeredAccountsUseCase,
this._deregisterAllUseCase,
this._authenticatorsUseCase,
this._changePinUseCase,
this._changePasswordUseCase,
this._deleteAuthenticatorsUseCase,
this._localDataBloc,
this._errorHandler,
this._globalNavigationManager,
) : super(HomeInitialState()) {
// Checking broadcast stream, if deep link was clicked in opened application
_deepLinkRepositorySubscription =
_deepLinkRepository.setDeepLinkReceiver((redirectUri) {
add(OOBRedirectArrivedEvent(redirectUri));
});
_localDataSubscription =
_localDataBloc.stream.listen((LocalDataState state) {
add(LocalDataEvent(state));
});
on<HomeCreatedEvent>(_handleHomeCreated);
on<ClientInitializedEvent>(_handleClientInitialized);
on<OOBRedirectArrivedEvent>(_handleOOBRedirect);
on<ReadQrCodeEvent>(_handleReadQrCode);
on<DeregisterEvent>(_handleDeregister);
on<InBandAuthenticationEvent>(_handleInBandAuthentication);
on<InBandRegisterEvent>(_handleInBandRegister);
on<PinChangeEvent>(_handlePinChange);
on<PasswordChangeEvent>(_handlePasswordChange);
on<ChangeDeviceInformationEvent>(_handleChangeDeviceInformation);
on<AuthCloudApiRegistrationEvent>(_handleAuthCloudApiRegistration);
on<DeleteAuthenticatorsEvent>(_handleDeleteAuthenticators);
on<LocalDataEvent>(_handleLocalData);
}
Future<void> _handleHomeCreated(
HomeEvent event,
Emitter<HomeState> emit,
) async {
await _initClient(emit);
}
Future<void> _handleClientInitialized(
ClientInitializedEvent event,
Emitter<HomeState> emit,
) async {
//Checking application start by deep link
final uri = await _deepLinkRepository.getStartUri();
if (uri?.isNotEmpty ?? false) _onRedirected(emit, uri);
await _loadData();
_yieldBasedOnCurrentState(emit);
}
Future<void> _handleOOBRedirect(
OOBRedirectArrivedEvent event,
Emitter<HomeState> emit,
) async {
_onRedirected(emit, event.redirectUri);
}
_onRedirected(
Emitter<HomeState> emit,
String? uri,
) async {
final dispatchTokenResponse =
Uri.tryParse(uri ?? "")?.queryParameters["dispatchTokenResponse"];
await _oobProcessUseCase.execute(dispatchTokenResponse).catchError((e) {
_errorHandler.handle(e);
});
}
Future<void> _initClient(Emitter<HomeState> emit) async {
final configuration = await _configurationLoader.load();
return await _clientProvider.init(configuration.sdkConfiguration, () async {
add(ClientInitializedEvent());
}).catchError((error) {
_yieldBasedOnCurrentState(emit);
_errorHandler.handle(error);
});
}
Future<void> _loadData() async {
_registeredAccounts =
await _registeredAccountsUseCase.execute().catchError((error) {
_errorHandler.handle(error);
return Set<Account>.identity();
});
_authenticators =
await _authenticatorsUseCase.execute().catchError((error) {
_errorHandler.handle(error);
return Set<Authenticator>.identity();
});
}
void _yieldBasedOnCurrentState(Emitter<HomeState> emit) {
emit(HomeLoadedState(_registeredAccounts.length));
}
Future<void> _handleReadQrCode(
ReadQrCodeEvent event,
Emitter<HomeState> emit,
) async {
if (await Permission.camera.request().isGranted) {
_globalNavigationManager.pushReadQrCode();
} else {
_errorHandler.handle(BusinessException.cameraAccessDenied());
}
}
Future<void> _handleDeregister(
DeregisterEvent event,
Emitter<HomeState> emit,
) async {
if (_configurationLoader.environment == AppEnvironment.identitySuite) {
// In the example app Identity Suite environment the deregistration endpoint is guarded,
// and as such we need to provide a cookie to the deregister call.
// Also on Identity Suite a deregistration has to be authenticated for every user,
// so batch deregistration is not really possible.
await _globalNavigationManager.pushSelectAccount(
SelectAccountParameter(
operationType: OperationType.deregistration,
accounts: _registeredAccounts,
),
);
} else {
if (_registeredAccounts.isEmpty) {
_errorHandler.handle(BusinessException.registeredAccountsNotFound());
} else {
await _deregisterAllUseCase
.execute(
accounts: _registeredAccounts,
authorizationProvider: null,
)
.catchError((error) {
_errorHandler.handle(error);
});
}
}
}
Future<void> _handleInBandAuthentication(
InBandAuthenticationEvent event,
Emitter<HomeState> emit,
) async {
// for in-band authentication a username is needed, that's why the account selector screen will be displayed
if (_registeredAccounts.isEmpty) {
_errorHandler.handle(BusinessException.registeredAccountsNotFound());
} else {
_globalNavigationManager.pushSelectAccount(
SelectAccountParameter(
operationType: OperationType.authentication,
accounts: _registeredAccounts,
),
);
}
}
Future<void> _handleInBandRegister(
InBandRegisterEvent event,
Emitter<HomeState> emit,
) async {
_globalNavigationManager.pushLegacyLogin();
_yieldBasedOnCurrentState(emit);
}
Future<void> _handlePinChange(
PinChangeEvent event,
Emitter<HomeState> emit,
) async {
// we should only pass the accounts to the account selection that already
// have a pin enrollment
Set<Account> eligibleAccounts = {};
for (final account in _registeredAccounts) {
if (_isEnrolled(account.username, Aaid.pin)) {
eligibleAccounts.add(account);
}
}
if (eligibleAccounts.length > 1) {
// in case that there are multiple eligible accounts then we have to show
// the account selection screen
_globalNavigationManager.pushSelectAccount(
SelectAccountParameter(
accounts: eligibleAccounts,
operationType: OperationType.pinChange,
),
);
} else if (eligibleAccounts.length == 1) {
// in case that there is only one account, then we can select it automatically
_startPinChange(eligibleAccounts.first.username);
} else {
_errorHandler.handle(BusinessException.accountsNotFound());
}
}
Future<void> _handlePasswordChange(
PasswordChangeEvent event,
Emitter<HomeState> emit,
) async {
// we should only pass the accounts to the account selection that already
// have a password enrollment
Set<Account> eligibleAccounts = {};
for (final account in _registeredAccounts) {
if (_isEnrolled(account.username, Aaid.password)) {
eligibleAccounts.add(account);
}
}
if (eligibleAccounts.length > 1) {
// in case that there are multiple eligible accounts then we have to show
// the account selection screen
_globalNavigationManager.pushSelectAccount(
SelectAccountParameter(
accounts: eligibleAccounts,
operationType: OperationType.passwordChange,
),
);
} else if (eligibleAccounts.length == 1) {
// in case that there is only one account, then we can select it automatically
_startPasswordChange(eligibleAccounts.first.username);
} else {
_errorHandler.handle(BusinessException.accountsNotFound());
}
}
Future<void> _handleChangeDeviceInformation(
ChangeDeviceInformationEvent event,
Emitter<HomeState> emit,
) async {
_globalNavigationManager.pushChangeDeviceInformation();
}
Future<void> _handleAuthCloudApiRegistration(
AuthCloudApiRegistrationEvent event,
Emitter<HomeState> emit,
) async {
_globalNavigationManager.pushAuthCloudApiRegistration();
}
Future<void> _handleDeleteAuthenticators(
DeleteAuthenticatorsEvent event,
Emitter<HomeState> emit,
) async {
if (_registeredAccounts.isEmpty) {
return _errorHandler.handle(
BusinessException.registeredAccountsNotFound(),
);
}
await _deleteAuthenticatorsUseCase
.execute(accounts: _registeredAccounts)
.catchError((error) {
_errorHandler.handle(error);
});
}
Future<void> _handleLocalData(
LocalDataEvent event,
Emitter<HomeState> emit,
) async {
final state = event.state;
if (state is LocalDataLoaded) {
_registeredAccounts = state.accounts;
_authenticators = state.authenticators;
_yieldBasedOnCurrentState(emit);
}
}
Future<void> _startPinChange(String username) async {
final parameter = CredentialParameter.pinChange(
username: username,
);
_changePinUseCase
.execute(username: username) //
.catchError((error) {
_errorHandler.handle(error);
});
_globalNavigationManager.pushCredential(parameter);
}
Future<void> _startPasswordChange(String username) async {
final parameter = CredentialParameter.passwordChange(
username: username,
);
_changePasswordUseCase
.execute(username: username) //
.catchError((error) {
_errorHandler.handle(error);
});
_globalNavigationManager.pushCredential(parameter);
}
bool _isEnrolled(String username, Aaid aaid) {
final authenticators =
_authenticators.where((element) => element.aaid == aaid.rawValue);
if (authenticators.isEmpty) {
return false;
}
return authenticators.first.userEnrollment.isEnrolled(username);
}
@override
Future<void> close() {
_deepLinkRepositorySubscription.cancel();
_localDataSubscription.cancel();
return super.close();
}
}