|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +@import CommonCrypto; |
| 18 | + |
17 | 19 | #import "MainViewController+OAuth.h"
|
18 | 20 |
|
19 | 21 | #import <AuthenticationServices/AuthenticationServices.h>
|
@@ -321,11 +323,57 @@ - (void)signInYahooHeadfulLite {
|
321 | 323 | - (ASAuthorizationAppleIDRequest *)appleIDRequestWithState:(NSString *)state API_AVAILABLE(ios(13.0)) {
|
322 | 324 | ASAuthorizationAppleIDRequest *request = [[[ASAuthorizationAppleIDProvider alloc] init] createRequest];
|
323 | 325 | 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]; |
325 | 329 | request.state = state;
|
326 | 330 | return request;
|
327 | 331 | }
|
328 | 332 |
|
| 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 | + |
329 | 377 | - (void)signInWithApple {
|
330 | 378 | if (@available(iOS 13, *)) {
|
331 | 379 | ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState:@"signIn"];
|
@@ -364,7 +412,7 @@ - (void)authorizationController:(ASAuthorizationController *)controller didCompl
|
364 | 412 | NSString *IDToken = [NSString stringWithUTF8String:[appleIDCredential.identityToken bytes]];
|
365 | 413 | FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"apple.com"
|
366 | 414 | IDToken:IDToken
|
367 |
| - rawNonce:@"REPLACE_ME_WITH_YOUR_RAW_NONCE" |
| 415 | + rawNonce:self.appleRawNonce |
368 | 416 | accessToken:nil];
|
369 | 417 |
|
370 | 418 | if ([appleIDCredential.state isEqualToString:@"signIn"]) {
|
|
0 commit comments