Skip to content

Commit

Permalink
[code-review] move crypto fuctions to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
juzerali committed Oct 4, 2021
1 parent c7eab10 commit 0666613
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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 {}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 0666613

Please sign in to comment.