Skip to content

Commit c9a8502

Browse files
authored
Update the sample app to use a random nonce in the Sign in with Apple flow (#5981)
* Update the sample app to use a random nonce in the Sign in with Apple flow. * Address feedback.
1 parent 019131e commit c9a8502

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

FirebaseAuth/Tests/Sample/Sample/MainViewController+Internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ typedef void (^TestAutomationCallback)(NSError *_Nullable error);
5151

5252
@property(nonatomic) NSURL *actionCodeContinueURL;
5353

54+
@property(nonatomic, copy) NSString *appleRawNonce;
55+
5456
@property(nonatomic) FIROAuthProvider *googleOAuthProvider;
5557
@property(nonatomic) FIROAuthProvider *microsoftOAuthProvider;
5658
@property(nonatomic) FIROAuthProvider *twitterOAuthProvider;

FirebaseAuth/Tests/Sample/Sample/MainViewController+OAuth.m

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
@import CommonCrypto;
18+
1719
#import "MainViewController+OAuth.h"
1820

1921
#import <AuthenticationServices/AuthenticationServices.h>
@@ -321,11 +323,57 @@ - (void)signInYahooHeadfulLite {
321323
- (ASAuthorizationAppleIDRequest *)appleIDRequestWithState:(NSString *)state API_AVAILABLE(ios(13.0)) {
322324
ASAuthorizationAppleIDRequest *request = [[[ASAuthorizationAppleIDProvider alloc] init] createRequest];
323325
request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
324-
request.nonce = @"REPLACE_ME_WITH_YOUR_NONCE";
326+
NSString *rawNonce = [self randomNonce:32];
327+
self.appleRawNonce = rawNonce;
328+
request.nonce = [self stringBySha256HashingString:rawNonce];
325329
request.state = state;
326330
return request;
327331
}
328332

333+
- (NSString *)randomNonce:(NSInteger)length {
334+
NSAssert(length > 0, @"Expected nonce to have positive length");
335+
NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
336+
NSMutableString *result = [NSMutableString string];
337+
NSInteger remainingLength = length;
338+
339+
while (remainingLength > 0) {
340+
NSMutableArray *randoms = [NSMutableArray arrayWithCapacity:16];
341+
for (NSInteger i = 0; i < 16; i++) {
342+
uint8_t random = 0;
343+
int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random);
344+
NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode);
345+
346+
[randoms addObject:@(random)];
347+
}
348+
349+
for (NSNumber *random in randoms) {
350+
if (remainingLength == 0) {
351+
break;
352+
}
353+
354+
if (random.unsignedIntValue < characterSet.length) {
355+
unichar character = [characterSet characterAtIndex:random.unsignedIntValue];
356+
[result appendFormat:@"%C", character];
357+
remainingLength--;
358+
}
359+
}
360+
}
361+
362+
return result;
363+
}
364+
365+
- (NSString *)stringBySha256HashingString:(NSString *)input {
366+
const char *string = [input UTF8String];
367+
unsigned char result[CC_SHA256_DIGEST_LENGTH];
368+
CC_SHA256(string, (CC_LONG)strlen(string), result);
369+
370+
NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
371+
for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
372+
[hashed appendFormat:@"%02x", result[i]];
373+
}
374+
return hashed;
375+
}
376+
329377
- (void)signInWithApple {
330378
if (@available(iOS 13, *)) {
331379
ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState:@"signIn"];
@@ -364,7 +412,7 @@ - (void)authorizationController:(ASAuthorizationController *)controller didCompl
364412
NSString *IDToken = [NSString stringWithUTF8String:[appleIDCredential.identityToken bytes]];
365413
FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"apple.com"
366414
IDToken:IDToken
367-
rawNonce:@"REPLACE_ME_WITH_YOUR_RAW_NONCE"
415+
rawNonce:self.appleRawNonce
368416
accessToken:nil];
369417

370418
if ([appleIDCredential.state isEqualToString:@"signIn"]) {

0 commit comments

Comments
 (0)