From 066661372cd8931432b0f9918c5d2de5641e97af Mon Sep 17 00:00:00 2001 From: Juzer Ali Date: Thu, 30 Sep 2021 23:21:46 +0530 Subject: [PATCH] [code-review] move crypto fuctions to a separate file --- .../lib/src/authentication_repository.dart | 26 +++---------------- .../lib/src/crypto.dart | 24 +++++++++++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 examples/flutter_firebase_login/packages/authentication_repository/lib/src/crypto.dart diff --git a/examples/flutter_firebase_login/packages/authentication_repository/lib/src/authentication_repository.dart b/examples/flutter_firebase_login/packages/authentication_repository/lib/src/authentication_repository.dart index a0d0cd909a3..c4bf0f7e75d 100644 --- a/examples/flutter_firebase_login/packages/authentication_repository/lib/src/authentication_repository.dart +++ b/examples/flutter_firebase_login/packages/authentication_repository/lib/src/authentication_repository.dart @@ -1,15 +1,13 @@ import 'dart:async'; -import 'dart:convert'; -import 'dart:math'; import 'package:authentication_repository/authentication_repository.dart'; +import 'package:authentication_repository/src/crypto.dart'; import 'package:cache/cache.dart'; import 'package:firebase_auth/firebase_auth.dart' as firebase_auth; import 'package:flutter/foundation.dart' show kIsWeb; import 'package:google_sign_in/google_sign_in.dart'; import 'package:sign_in_with_apple/sign_in_with_apple.dart'; import 'package:meta/meta.dart'; -import 'package:crypto/crypto.dart'; /// Thrown if during the sign up process if a failure occurs. class SignUpFailure implements Exception {} @@ -42,6 +40,7 @@ class AuthenticationRepository { final CacheClient _cache; final firebase_auth.FirebaseAuth _firebaseAuth; final GoogleSignIn _googleSignIn; + final Crypto _crypto = Crypto(); /// Whether or not the current environment is web /// Should only be overriden for testing purposes. Otherwise, @@ -121,8 +120,8 @@ class AuthenticationRepository { // include a nonce in the credential request. When signing in with // Firebase, the nonce in the id token returned by Apple, is expected to // match the sha256 hash of `rawNonce`. - final rawNonce = generateNonce(); - final nonce = sha256ofString(rawNonce); + final rawNonce = _crypto.generateNonce(); + final nonce = _crypto.sha256ofString(rawNonce); try { final appleCredential = await SignInWithApple.getAppleIDCredential( @@ -145,23 +144,6 @@ class AuthenticationRepository { } } - /// Generates a cryptographically secure random nonce, to be included in a - /// credential request. - String generateNonce([int length = 32]) { - final charset = - '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._'; - const random = Random.secure(); - return List.generate(length, (_) => charset[random.nextInt(charset.length)]) - .join(); - } - - /// Returns the sha256 hash of [input] in hex notation. - String sha256ofString(String input) { - final bytes = utf8.encode(input); - final digest = sha256.convert(bytes); - return digest.toString(); - } - /// Signs in with the provided [email] and [password]. /// /// Throws a [LogInWithEmailAndPasswordFailure] if an exception occurs. diff --git a/examples/flutter_firebase_login/packages/authentication_repository/lib/src/crypto.dart b/examples/flutter_firebase_login/packages/authentication_repository/lib/src/crypto.dart new file mode 100644 index 00000000000..935dc702e23 --- /dev/null +++ b/examples/flutter_firebase_login/packages/authentication_repository/lib/src/crypto.dart @@ -0,0 +1,24 @@ +import 'dart:math'; +import 'dart:convert'; + +import 'package:crypto/crypto.dart'; + +class Crypto { + + /// Generates a cryptographically secure random nonce, to be included in a + /// credential request. + String generateNonce([int length = 32]) { + const charset = + '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._'; + var random = Random.secure(); + return List.generate(length, (_) => charset[random.nextInt(charset.length)]) + .join(); + } + + /// Returns the sha256 hash of [input] in hex notation. + String sha256ofString(String input) { + final bytes = utf8.encode(input); + final digest = sha256.convert(bytes); + return digest.toString(); + } +} \ No newline at end of file