Skip to content

Commit fa3602e

Browse files
committed
Address review comments and add a test for the SHA-256 hash helper function.
1 parent c16a7d0 commit fa3602e

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

FirebaseAuthUI/FirebaseAuthUITests/FUIAuthTest.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,13 @@ - (void)testUseEmulatorSetsFIRAuthEmulator {
152152
OCMVerify([mockAuth useEmulatorWithHost:@"host" port:12345]);
153153
}
154154

155+
- (void)testStringBySHA256HashingString {
156+
NSString *inputString = @"abc-123.ZYX_987";
157+
NSString *expectedSHA256HashedString = @"d858d78754a50c8ccdc414946f656fe854e6ba76bf09a79a7e7d9ca135e4b58d";
158+
159+
NSString *actualSHA256HashedString = [FUIAuthUtils stringBySHA256HashingString:inputString];
160+
161+
XCTAssertEqualObjects(actualSHA256HashedString, expectedSHA256HashedString);
162+
}
163+
155164
@end

FirebaseAuthUI/Sources/FUIAuthUtils.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ + (nullable UIImage *)imageNamed:(NSString *)name fromBundle:(nullable NSBundle
7777
}
7878

7979
+ (NSString *)randomNonce {
80+
// Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce
8081
NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
8182
NSMutableString *result = [NSMutableString string];
8283
NSInteger remainingLength = 32;
@@ -86,7 +87,10 @@ + (NSString *)randomNonce {
8687
for (NSInteger i = 0; i < 16; i++) {
8788
uint8_t random = 0;
8889
int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random);
89-
NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode);
90+
if (errorCode != errSecSuccess) {
91+
[NSException raise:@"FUIAuthGenerateRandomNonce"
92+
format:@"Unable to generate nonce: OSStatus %i", errorCode];
93+
}
9094

9195
[randoms addObject:@(random)];
9296
}
@@ -107,7 +111,7 @@ + (NSString *)randomNonce {
107111
return result;
108112
}
109113

110-
+ (NSString *)stringBySha256HashingString:(NSString *)input {
114+
+ (NSString *)stringBySHA256HashingString:(NSString *)input {
111115
const char *string = [input UTF8String];
112116
unsigned char result[CC_SHA256_DIGEST_LENGTH];
113117
CC_SHA256(string, (CC_LONG)strlen(string), result);

FirebaseAuthUI/Sources/Public/FirebaseAuthUI/FUIAuthUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ extern NSString *const FUIAuthBundleName;
5252
*/
5353
+ (NSString *)randomNonce;
5454

55-
/** @fn stringBySha256HashingString:
55+
/** @fn stringBySHA256HashingString:
5656
@brief Generates the SHA-256 hash of the input string.
5757
@param input The input string to be hashed.
5858
*/
59-
+ (NSString *)stringBySha256HashingString:(NSString *)input;
59+
+ (NSString *)stringBySHA256HashingString:(NSString *)input;
6060

6161
@end
6262

FirebaseOAuthUI/Sources/FUIOAuth.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ - (void)signInWithDefaultValue:(nullable NSString *)defaultValue
298298
self.currentNonce = nonce;
299299
ASAuthorizationAppleIDRequest *request = [[[ASAuthorizationAppleIDProvider alloc] init] createRequest];
300300
request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
301-
request.nonce = [FUIAuthUtils stringBySha256HashingString:nonce];
301+
request.nonce = [FUIAuthUtils stringBySHA256HashingString:nonce];
302302
ASAuthorizationController* controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
303303
controller.delegate = self;
304304
controller.presentationContextProvider = self;

0 commit comments

Comments
 (0)