@@ -24,7 +24,9 @@ - (dispatch_queue_t)methodQueue
24
24
return dispatch_get_main_queue ();
25
25
}
26
26
27
- UIBackgroundTaskIdentifier rnAppAuthTaskId;
27
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
28
+ UIBackgroundTaskIdentifier rnAppAuthTaskId;
29
+ #endif
28
30
29
31
/* ! @brief Number of random bytes generated for the @ state.
30
32
*/
@@ -356,32 +358,51 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
356
358
additionalParameters: additionalParameters];
357
359
358
360
// performs authentication request
361
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
359
362
id <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager> appDelegate = (id <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>)[UIApplication sharedApplication ].delegate ;
363
+ #elif TARGET_OS_OSX
364
+ id <NSApplicationDelegate , RNAppAuthAuthorizationFlowManager> appDelegate = (id <NSApplicationDelegate , RNAppAuthAuthorizationFlowManager>)[NSApplication sharedApplication ].delegate ;
365
+ #endif
360
366
if (![[appDelegate class ] conformsToProtocol: @protocol (RNAppAuthAuthorizationFlowManager)]) {
361
367
[NSException raise: @" RNAppAuth Missing protocol conformance"
362
368
format: @" %@ does not conform to RNAppAuthAuthorizationFlowManager" , appDelegate];
363
369
}
364
370
appDelegate.authorizationFlowManagerDelegate = self;
365
371
__weak typeof (self) weakSelf = self;
366
372
373
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
367
374
rnAppAuthTaskId = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler: ^{
368
375
[UIApplication.sharedApplication endBackgroundTask: rnAppAuthTaskId];
369
376
rnAppAuthTaskId = UIBackgroundTaskInvalid;
370
377
}];
371
378
372
379
UIViewController *presentingViewController = appDelegate.window .rootViewController .view .window ? appDelegate.window .rootViewController : appDelegate.window .rootViewController .presentedViewController ;
380
+ #elif TARGET_OS_OSX
381
+ NSWindow *presentingWindow = [NSApplication sharedApplication ].mainWindow ;
382
+ if (!presentingWindow){
383
+ reject (@" authentication_failed" , @" Unable to get the main window to present an authorization request. Try authenticating again with the main window open." , nil );
384
+ return ;
385
+ }
386
+ #endif
373
387
374
388
#if TARGET_OS_MACCATALYST
375
389
id <OIDExternalUserAgent> externalUserAgent = nil ;
376
390
#elif TARGET_OS_IOS
377
391
id <OIDExternalUserAgent> externalUserAgent = iosCustomBrowser != nil ? [self getCustomBrowser: iosCustomBrowser] : nil ;
392
+ #elif TARGET_OS_OSX
393
+ id <OIDExternalUserAgent> externalUserAgent = nil ;
378
394
#endif
379
395
380
396
OIDAuthorizationCallback callback = ^(OIDAuthorizationResponse *_Nullable authorizationResponse, NSError *_Nullable error) {
381
397
typeof (self) strongSelf = weakSelf;
382
398
strongSelf->_currentSession = nil ;
399
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
383
400
[UIApplication.sharedApplication endBackgroundTask: rnAppAuthTaskId];
384
401
rnAppAuthTaskId = UIBackgroundTaskInvalid;
402
+ #elif TARGET_OS_OSX
403
+ // Brings this app to the foreground.
404
+ [[NSRunningApplication currentApplication ] activateWithOptions: (NSApplicationActivateAllWindows)];
405
+ #endif
385
406
if (authorizationResponse) {
386
407
resolve ([self formatAuthorizationResponse: authorizationResponse withCodeVerifier: codeVerifier]);
387
408
} else {
@@ -397,6 +418,7 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
397
418
externalUserAgent: externalUserAgent
398
419
callback: callback];
399
420
} else {
421
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
400
422
if (@available (iOS 13 , *)) {
401
423
_currentSession = [OIDAuthorizationService presentAuthorizationRequest: request
402
424
presentingViewController: presentingViewController
@@ -407,6 +429,12 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
407
429
presentingViewController: presentingViewController
408
430
callback: callback];
409
431
}
432
+ #elif TARGET_OS_OSX
433
+ _currentSession = [OIDAuthorizationService presentAuthorizationRequest: request
434
+ presentingWindow: presentingWindow
435
+ prefersEphemeralSession: prefersEphemeralSession
436
+ callback: callback];
437
+ #endif
410
438
}
411
439
} else {
412
440
OIDAuthStateAuthorizationCallback callback = ^(
@@ -415,8 +443,13 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
415
443
) {
416
444
typeof (self) strongSelf = weakSelf;
417
445
strongSelf->_currentSession = nil ;
446
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
418
447
[UIApplication.sharedApplication endBackgroundTask: rnAppAuthTaskId];
419
448
rnAppAuthTaskId = UIBackgroundTaskInvalid;
449
+ #elif TARGET_OS_OSX
450
+ // Brings this app to the foreground.
451
+ [[NSRunningApplication currentApplication ] activateWithOptions: (NSApplicationActivateAllWindows)];
452
+ #endif
420
453
if (authState) {
421
454
resolve ([self formatResponse: authState.lastTokenResponse
422
455
withAuthResponse: authState.lastAuthorizationResponse]);
@@ -433,6 +466,7 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
433
466
} else {
434
467
435
468
469
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
436
470
if (@available (iOS 13 , *)) {
437
471
_currentSession = [OIDAuthState authStateByPresentingAuthorizationRequest: request
438
472
presentingViewController: presentingViewController
@@ -443,6 +477,11 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
443
477
presentingViewController: presentingViewController
444
478
callback: callback];
445
479
}
480
+ #elif TARGET_OS_OSX
481
+ _currentSession = [OIDAuthState authStateByPresentingAuthorizationRequest: request
482
+ presentingWindow: presentingWindow
483
+ callback: callback];
484
+ #endif
446
485
}
447
486
}
448
487
}
@@ -499,35 +538,54 @@ - (void)endSessionWithConfiguration: (OIDServiceConfiguration *) configuration
499
538
postLogoutRedirectURL: [NSURL URLWithString: postLogoutRedirectURL]
500
539
additionalParameters: additionalParameters];
501
540
541
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
502
542
id <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager> appDelegate = (id <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>)[UIApplication sharedApplication ].delegate ;
543
+ #elif TARGET_OS_OSX
544
+ id <NSApplicationDelegate , RNAppAuthAuthorizationFlowManager> appDelegate = (id <NSApplicationDelegate , RNAppAuthAuthorizationFlowManager>)[NSApplication sharedApplication ].delegate ;
545
+ #endif
503
546
if (![[appDelegate class ] conformsToProtocol: @protocol (RNAppAuthAuthorizationFlowManager)]) {
504
547
[NSException raise: @" RNAppAuth Missing protocol conformance"
505
548
format: @" %@ does not conform to RNAppAuthAuthorizationFlowManager" , appDelegate];
506
549
}
507
550
appDelegate.authorizationFlowManagerDelegate = self;
508
551
__weak typeof (self) weakSelf = self;
509
552
553
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
510
554
rnAppAuthTaskId = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler: ^{
511
555
[UIApplication.sharedApplication endBackgroundTask: rnAppAuthTaskId];
512
556
rnAppAuthTaskId = UIBackgroundTaskInvalid;
513
557
}];
514
558
515
559
UIViewController *presentingViewController = appDelegate.window .rootViewController .view .window ? appDelegate.window .rootViewController : appDelegate.window .rootViewController .presentedViewController ;
560
+ #elif TARGET_OS_OSX
561
+ NSWindow *presentingWindow = [NSApplication sharedApplication ].mainWindow ;
562
+ if (!presentingWindow){
563
+ reject (@" authentication_failed" , @" Unable to get the main window to present an authorization request. Try authenticating again with the main window open." , nil );
564
+ return ;
565
+ }
566
+ #endif
516
567
517
568
#if TARGET_OS_MACCATALYST
518
569
id <OIDExternalUserAgent> externalUserAgent = nil ;
519
570
#elif TARGET_OS_IOS
520
571
id <OIDExternalUserAgent> externalUserAgent = iosCustomBrowser != nil ? [self getCustomBrowser: iosCustomBrowser] : [self getExternalUserAgentWithPresentingViewController: presentingViewController
521
572
prefersEphemeralSession: prefersEphemeralSession];
573
+ #elif TARGET_OS_OSX
574
+ id <OIDExternalUserAgent> externalUserAgent = nil ;
522
575
#endif
523
576
524
577
_currentSession = [OIDAuthorizationService presentEndSessionRequest: endSessionRequest
525
578
externalUserAgent: externalUserAgent
526
579
callback: ^(OIDEndSessionResponse *_Nullable response, NSError *_Nullable error) {
527
580
typeof (self) strongSelf = weakSelf;
528
581
strongSelf->_currentSession = nil ;
582
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
529
583
[UIApplication.sharedApplication endBackgroundTask: rnAppAuthTaskId];
530
584
rnAppAuthTaskId = UIBackgroundTaskInvalid;
585
+ #elif TARGET_OS_OSX
586
+ // Brings this app to the foreground.
587
+ [[NSRunningApplication currentApplication ] activateWithOptions: (NSApplicationActivateAllWindows)];
588
+ #endif
531
589
if (response) {
532
590
resolve ([self formatEndSessionResponse: response]);
533
591
} else {
@@ -694,10 +752,10 @@ - (NSString*)getErrorCode: (NSError*) error defaultCode: (NSString *) defaultCod
694
752
return defaultCode;
695
753
}
696
754
697
- #if !TARGET_OS_MACCATALYST
755
+ #if TARGET_OS_IOS
698
756
- (id <OIDExternalUserAgent>)getCustomBrowser : (NSString *) browserType {
699
757
typedef id <OIDExternalUserAgent> (^BrowserBlock)(void );
700
-
758
+
701
759
NSDictionary *browsers = @{
702
760
@" safari" :
703
761
^{
@@ -733,8 +791,13 @@ - (NSString*)getErrorMessage: (NSError*) error {
733
791
}
734
792
}
735
793
794
+ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
736
795
- (id <OIDExternalUserAgent>)getExternalUserAgentWithPresentingViewController : (UIViewController *)presentingViewController
737
- prefersEphemeralSession : (BOOL *) prefersEphemeralSession
796
+ prefersEphemeralSession : (BOOL ) prefersEphemeralSession
797
+ #elif TARGET_OS_OSX
798
+ - (id <OIDExternalUserAgent>)getExternalUserAgentWithPresentingWindow : (NSWindow *)presentingWindow
799
+ prefersEphemeralSession : (BOOL ) prefersEphemeralSession
800
+ #endif
738
801
{
739
802
id <OIDExternalUserAgent> externalUserAgent;
740
803
#if TARGET_OS_MACCATALYST
@@ -749,7 +812,11 @@ - (NSString*)getErrorMessage: (NSError*) error {
749
812
presentingViewController];
750
813
}
751
814
#elif TARGET_OS_OSX
752
- externalUserAgent = [[OIDExternalUserAgentMac alloc ] init ];
815
+ if (@available (macOS 10.15 , *)) {
816
+ externalUserAgent = [[OIDExternalUserAgentMac alloc ] initWithPresentingWindow: presentingWindow];
817
+ } else {
818
+ externalUserAgent = [[OIDExternalUserAgentMac alloc ] init ];
819
+ }
753
820
#endif
754
821
return externalUserAgent;
755
822
}
0 commit comments