Skip to content

Commit dac3ee1

Browse files
committed
Address review comment to use shared utility method for nonce and hashing.
1 parent 4ff100f commit dac3ee1

File tree

2 files changed

+12
-54
lines changed

2 files changed

+12
-54
lines changed

FirebaseFacebookAuthUI/Sources/FUIFacebookAuth.m

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
#import <FirebaseAuth/FirebaseAuth.h>
2121

2222
#if SWIFT_PACKAGE
23-
@import CommonCrypto;
2423
@import FBSDKCoreKit;
2524
@import FBSDKLoginKit;
2625
#else
2726
#import <FBSDKCoreKit/FBSDKCoreKit.h>
2827
#import <FBSDKLoginKit/FBSDKLoginKit.h>
29-
#import <CommonCrypto/CommonCrypto.h>
3028
#endif // SWIFT_PACKAGE
3129

3230
/** @var kTableName
@@ -70,6 +68,11 @@ @interface FUIFacebookAuth () <FUIAuthProvider>
7068
*/
7169
@property(nonatomic, strong) FIROAuthProvider *providerForEmulator;
7270

71+
/** @property currentNonce
72+
@brief The nonce for the current Facebook Limited Login session, if any.
73+
*/
74+
@property(nonatomic, copy, nullable) NSString *currentNonce;
75+
7376
@end
7477

7578
@implementation FUIFacebookAuth {
@@ -88,11 +91,6 @@ @implementation FUIFacebookAuth {
8891
@brief The email address associated with this account.
8992
*/
9093
NSString *_email;
91-
92-
/** @var _currentNonce
93-
@brief The current nonce for a Facebook Limited Login sign-in attempt.
94-
*/
95-
NSString *_currentNonce;
9694
}
9795

9896
+ (NSBundle *)bundle {
@@ -200,12 +198,12 @@ - (void)signInWithDefaultValue:(nullable NSString *)defaultValue
200198

201199
if (self.useLimitedLogin) {
202200
// Facebook Limited Login
203-
NSString *nonce = [self randomNonce];
204-
self->_currentNonce = nonce;
201+
NSString *nonce = [FUIAuthUtils randomNonce];
202+
self.currentNonce = nonce;
205203
FBSDKLoginConfiguration *configuration =
206204
[[FBSDKLoginConfiguration alloc] initWithPermissions:_scopes
207205
tracking:FBSDKLoginTrackingLimited
208-
nonce:[self stringBySha256HashingString:nonce]];
206+
nonce:[FUIAuthUtils stringBySHA256HashingString:nonce]];
209207
[_loginManager logInFromViewController:presentingViewController
210208
configuration:configuration
211209
completion:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
@@ -322,7 +320,7 @@ - (void)completeSignInFlowWithAccessToken:(nullable NSString *)accessToken
322320
}
323321
FIRAuthCredential *credential;
324322
if (idToken) {
325-
NSString *rawNonce = self->_currentNonce;
323+
NSString *rawNonce = self.currentNonce;
326324
credential = [FIROAuthProvider credentialWithProviderID:FIRFacebookAuthProviderID
327325
IDToken:idToken
328326
rawNonce:rawNonce];
@@ -394,47 +392,4 @@ - (FBSDKLoginManager *)createLoginManager {
394392
return [[FBSDKLoginManager alloc] init];
395393
}
396394

397-
- (NSString *)randomNonce {
398-
NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
399-
NSMutableString *result = [NSMutableString string];
400-
NSInteger remainingLength = 32;
401-
402-
while (remainingLength > 0) {
403-
NSMutableArray *randoms = [NSMutableArray arrayWithCapacity:16];
404-
for (NSInteger i = 0; i < 16; i++) {
405-
uint8_t random = 0;
406-
int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random);
407-
NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode);
408-
409-
[randoms addObject:@(random)];
410-
}
411-
412-
for (NSNumber *random in randoms) {
413-
if (remainingLength == 0) {
414-
break;
415-
}
416-
417-
if (random.unsignedIntValue < characterSet.length) {
418-
unichar character = [characterSet characterAtIndex:random.unsignedIntValue];
419-
[result appendFormat:@"%C", character];
420-
remainingLength--;
421-
}
422-
}
423-
}
424-
425-
return result;
426-
}
427-
428-
- (NSString *)stringBySha256HashingString:(NSString *)input {
429-
const char *string = [input UTF8String];
430-
unsigned char result[CC_SHA256_DIGEST_LENGTH];
431-
CC_SHA256(string, (CC_LONG)strlen(string), result);
432-
433-
NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
434-
for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
435-
[hashed appendFormat:@"%02x", result[i]];
436-
}
437-
return hashed;
438-
}
439-
440395
@end

FirebaseOAuthUI/Sources/FUIOAuth.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ @interface FUIOAuth () <ASAuthorizationControllerDelegate, ASAuthorizationContro
9999
*/
100100
@property(nonatomic, copy, nullable) NSString *loginHintKey;
101101

102+
/** @property currentNonce
103+
@brief The nonce for the current Sign in with Apple session, if any.
104+
*/
102105
@property(nonatomic, copy, nullable) NSString *currentNonce;
103106

104107
/** @property provider

0 commit comments

Comments
 (0)