diff --git a/packages/wyatt_authentication_bloc/lib/src/data/data.dart b/packages/wyatt_authentication_bloc/lib/src/data/data.dart new file mode 100644 index 00000000..42218c55 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/data.dart @@ -0,0 +1,19 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +export 'data_sources/data_sources.dart'; +export 'models/models.dart'; +export 'repositories/repositories.dart'; diff --git a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/data_sources.dart b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/data_sources.dart new file mode 100644 index 00000000..346c8fa7 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/data_sources.dart @@ -0,0 +1,18 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +export 'local/authentication_cache_data_source_impl.dart'; +export 'remote/authentication_firebase_data_source_impl.dart'; diff --git a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/local/authentication_cache_data_source_impl.dart b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/local/authentication_cache_data_source_impl.dart new file mode 100644 index 00000000..363ee51d --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/local/authentication_cache_data_source_impl.dart @@ -0,0 +1,60 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_architecture/wyatt_architecture.dart'; +import 'package:wyatt_authentication_bloc/src/domain/data_sources/local/authentication_local_data_source.dart'; +import 'package:wyatt_authentication_bloc/src/domain/entities/account.dart'; +import 'package:wyatt_type_utils/wyatt_type_utils.dart'; + +class AuthenticationCacheDataSourceImpl + extends AuthenticationLocalDataSource { + Account? _account; + T? _data; + + AuthenticationCacheDataSourceImpl(); + + @override + void storeAccount(Account? account) { + _account = account; + } + + @override + void storeData(T? data) { + _data = data; + } + + @override + Account loadAccount() { + if (_account.isNotNull) { + return _account!; + } + throw ClientException('Cached account is invalid'); + } + + @override + T loadData() { + if (_data.isNotNull) { + return _data!; + } + throw ClientException('Cached data is invalid'); + } + + @override + void destroy() { + _data = null; + _account = null; + } +} diff --git a/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_firebase_data_source_impl.dart b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_firebase_data_source_impl.dart new file mode 100644 index 00000000..f0c55b8d --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/data_sources/remote/authentication_firebase_data_source_impl.dart @@ -0,0 +1,110 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:wyatt_architecture/wyatt_architecture.dart'; +import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart'; +import 'package:wyatt_type_utils/wyatt_type_utils.dart'; + +class AuthenticationFirebaseDataSourceImpl + extends AuthenticationRemoteDataSource { + final FirebaseAuth _firebaseAuth; + + AuthenticationFirebaseDataSourceImpl({FirebaseAuth? firebaseAuth}) + : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance; + + Account _mapper(User user) => AccountModel( + uid: user.uid, + email: user.email, + ); + + @override + Future signInWithEmailAndPassword({ + required String email, + required String password, + }) async { + try { + final userCredential = await _firebaseAuth.signInWithEmailAndPassword( + email: email, + password: password, + ); + final user = userCredential.user; + if (user.isNotNull) { + return _mapper(user!); + } else { + throw Exception(); // Get caught just after. + } + } on FirebaseAuthException catch (e) { + throw SignInWithEmailAndPasswordFailureFirebase.fromCode(e.code); + } catch (_) { + throw SignInWithEmailAndPasswordFailureFirebase(); + } + } + + @override + Future signUp({ + required String email, + required String password, + }) async { + try { + final userCredential = await _firebaseAuth.createUserWithEmailAndPassword( + email: email, + password: password, + ); + final user = userCredential.user; + if (user.isNotNull) { + return _mapper(user!); + } else { + throw Exception(); // Get caught just after. + } + } on FirebaseAuthException catch (e) { + throw SignUpWithEmailAndPasswordFailureFirebase.fromCode(e.code); + } catch (_) { + throw SignUpWithEmailAndPasswordFailureFirebase(); + } + } + + @override + Future signOut() async { + try { + await _firebaseAuth.signOut(); + } catch (_) { + throw SignOutFailureFirebase(); + } + } + + @override + Future getIdentityToken() async { + try { + final token = await _firebaseAuth.currentUser?.getIdToken(); + if (token.isNotNull) { + return token!; + } else { + throw Exception(); // Get caught just after. + } + } catch (_) { + // TODO(hpcl): implement a non ambiguous exception for this case + throw ServerException(); + } + } + + @override + Stream streamAccount() => + _firebaseAuth.userChanges().map((user) { + final Account? account = (user.isNotNull) ? _mapper(user!) : null; + return account; + }); +} diff --git a/packages/wyatt_authentication_bloc/lib/src/data/models/account_model.dart b/packages/wyatt_authentication_bloc/lib/src/data/models/account_model.dart new file mode 100644 index 00000000..0d1d6b65 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/models/account_model.dart @@ -0,0 +1,27 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_authentication_bloc/src/domain/entities/account.dart'; + +class AccountModel implements Account { + @override + final String uid; + + @override + final String? email; + + AccountModel({required this.uid, required this.email}); +} diff --git a/packages/wyatt_authentication_bloc/lib/src/data/models/account_wrapper_model.dart b/packages/wyatt_authentication_bloc/lib/src/data/models/account_wrapper_model.dart new file mode 100644 index 00000000..69bffd98 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/models/account_wrapper_model.dart @@ -0,0 +1,27 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_authentication_bloc/src/domain/entities/account.dart'; +import 'package:wyatt_authentication_bloc/src/domain/entities/account_wrapper.dart'; + +class AccountWrapperModel extends AccountWrapper { + @override + final Account? account; + @override + final T? data; + + AccountWrapperModel(this.account, this.data); +} diff --git a/packages/wyatt_authentication_bloc/lib/src/data/models/models.dart b/packages/wyatt_authentication_bloc/lib/src/data/models/models.dart new file mode 100644 index 00000000..09cc76bf --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/data/models/models.dart @@ -0,0 +1,18 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +export 'account_model.dart'; +export 'account_wrapper_model.dart'; diff --git a/packages/wyatt_authentication_bloc/lib/src/data/models/user_firebase.dart b/packages/wyatt_authentication_bloc/lib/src/data/models/user_firebase.dart deleted file mode 100644 index 31139cf3..00000000 --- a/packages/wyatt_authentication_bloc/lib/src/data/models/user_firebase.dart +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (C) 2022 WYATT GROUP -// Please see the AUTHORS file for details. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -import 'package:firebase_auth/firebase_auth.dart'; -import 'package:wyatt_authentication_bloc/src/domain/entities/user.dart' as wyatt; - -class UserFirebase implements wyatt.User { - final User? _user; - - const UserFirebase(User user) : _user = user; - - User? get inner => _user; - - @override - const UserFirebase.empty() : _user = null; - - @override - DateTime? get creationTime => _user?.metadata.creationTime; - - @override - String? get displayName => _user?.displayName; - - @override - String? get email => _user?.email; - - @override - bool get emailVerified => _user?.emailVerified ?? false; - - @override - bool get isAnonymous => _user?.isAnonymous ?? false; - - @override - bool get isEmpty => _user == null; - - @override - bool get isNotEmpty => _user != null; - - @override - DateTime? get lastSignInTime => _user?.metadata.lastSignInTime; - - @override - String? get phoneNumber => _user?.phoneNumber; - - @override - String? get photoURL => _user?.photoURL; - - @override - String? get refreshToken => _user?.refreshToken; - - @override - String get uid => _user?.uid ?? ''; - - @override - String? get providerId => _user?.providerData.first.providerId; - - @override - bool? get isNewUser { - if (_user?.metadata.lastSignInTime == null || - _user?.metadata.creationTime == null) { - return null; - } else { - return _user?.metadata.lastSignInTime == _user?.metadata.creationTime; - } - } - - @override - // ignore: lines_longer_than_80_chars - String toString() => 'UserFirebase(creationTime: $creationTime, displayName: $displayName, email: $email, emailVerified: $emailVerified, isAnonymous: $isAnonymous, lastSignInTime: $lastSignInTime, phoneNumber: $phoneNumber, photoURL: $photoURL, refreshToken: $refreshToken, uid: $uid)'; -} diff --git a/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/data_sources.dart b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/data_sources.dart new file mode 100644 index 00000000..1e726386 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/data_sources.dart @@ -0,0 +1,19 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +export 'local/authentication_biometrics_data_source.dart'; +export 'local/authentication_local_data_source.dart'; +export 'remote/authentication_remote_data_source.dart'; diff --git a/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/local/authentication_biometrics_data_source.dart b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/local/authentication_biometrics_data_source.dart new file mode 100644 index 00000000..489c9919 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/local/authentication_biometrics_data_source.dart @@ -0,0 +1,19 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_architecture/wyatt_architecture.dart'; + +abstract class AuthenticationBiometricsDataSource extends BaseLocalDataSource {} diff --git a/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/local/authentication_local_data_source.dart b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/local/authentication_local_data_source.dart new file mode 100644 index 00000000..cd47d32e --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/local/authentication_local_data_source.dart @@ -0,0 +1,27 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_architecture/wyatt_architecture.dart'; +import 'package:wyatt_authentication_bloc/src/domain/entities/account.dart'; + +abstract class AuthenticationLocalDataSource + extends BaseLocalDataSource { + void storeAccount(Account? account); + void storeData(T? data); + Account loadAccount(); + T loadData(); + void destroy(); +} diff --git a/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/remote/authentication_remote_data_source.dart b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/remote/authentication_remote_data_source.dart new file mode 100644 index 00000000..3bd615e1 --- /dev/null +++ b/packages/wyatt_authentication_bloc/lib/src/domain/data_sources/remote/authentication_remote_data_source.dart @@ -0,0 +1,36 @@ +// Copyright (C) 2022 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:wyatt_architecture/wyatt_architecture.dart'; +import 'package:wyatt_authentication_bloc/src/domain/entities/account.dart'; + +abstract class AuthenticationRemoteDataSource extends BaseRemoteDataSource { + Future signUp({ + required String email, + required String password, + }); + + Future signInWithEmailAndPassword({ + required String email, + required String password, + }); + + Future signOut(); + + Stream streamAccount(); + + Future getIdentityToken(); +}