Skip to content

Commit 96afce5

Browse files
authored
Add support for prefersEphemeralSession on iOS. Refs FormidableLabs#771 (FormidableLabs#787)
1 parent f8c9162 commit 96afce5

File tree

8 files changed

+77
-20
lines changed

8 files changed

+77
-20
lines changed

Example/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const App = () => {
7171
const newAuthState = await authorize({
7272
...config,
7373
connectionTimeoutSeconds: 5,
74+
iosPrefersEphemeralSession: true
7475
});
7576

7677
setAuthState({

Example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ PODS:
192192
- React-jsi (= 0.63.2)
193193
- React-jsinspector (0.63.2)
194194
- react-native-app-auth (6.4.3):
195-
- AppAuth (~> 1.4)
195+
- AppAuth (~> 1.6)
196196
- React-Core
197197
- React-RCTActionSheet (0.63.2):
198198
- React-Core/RCTActionSheetHeaders (= 0.63.2)
@@ -366,7 +366,7 @@ SPEC CHECKSUMS:
366366
React-jsi: 54245e1d5f4b690dec614a73a3795964eeef13a8
367367
React-jsiexecutor: 8ca588cc921e70590820ce72b8789b02c67cce38
368368
React-jsinspector: b14e62ebe7a66e9231e9581279909f2fc3db6606
369-
react-native-app-auth: e5b48009fda193a6a6808ec8f3d2cf159d9cbba4
369+
react-native-app-auth: 0aaa426c98f354afa487f1d792bd711d83495a22
370370
React-RCTActionSheet: 910163b6b09685a35c4ebbc52b66d1bfbbe39fc5
371371
React-RCTAnimation: 9a883bbe1e9d2e158d4fb53765ed64c8dc2200c6
372372
React-RCTBlob: 39cf0ece1927996c4466510e25d2105f67010e13

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ with optional overrides.
137137
- **usePKCE** - (`boolean`) (default: true) optionally allows not sending the code_challenge parameter and skipping PKCE code verification, to support non-compliant providers.
138138
- **skipCodeExchange** - (`boolean`) (default: false) just return the authorization response, instead of automatically exchanging the authorization code. This is useful if this exchange needs to be done manually (not client-side)
139139
- **iosCustomBrowser** - (`string`) (default: undefined) _IOS_ override the used browser for authorization, used to open an external browser. If no value is provided, the `SFAuthenticationSession` or `SFSafariViewController` are used.
140+
- **iosPrefersEphemeralSession** - (`boolean`) (default: `false`) _IOS_ indicates whether the session should ask the browser for a private authentication session.
140141
- **androidAllowCustomBrowsers** - (`string[]`) (default: undefined) _ANDROID_ override the used browser for authorization. If no value is provided, all browsers are allowed.
141142
- **connectionTimeoutSeconds** - (`number`) configure the request timeout interval in seconds. This must be a positive number. The default values are 60 seconds on iOS and 15 seconds on Android.
142143

index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,21 @@ export type AuthConfiguration = BaseAuthConfiguration & {
8080
warmAndPrefetchChrome?: boolean;
8181
skipCodeExchange?: boolean;
8282
iosCustomBrowser?: 'safari' | 'chrome' | 'opera' | 'firefox';
83-
androidAllowCustomBrowsers?: ('chrome' | 'chromeCustomTab' | 'firefox' | 'firefoxCustomTab' | 'samsung' | 'samsungCustomTab')[]
83+
androidAllowCustomBrowsers?: (
84+
| 'chrome'
85+
| 'chromeCustomTab'
86+
| 'firefox'
87+
| 'firefoxCustomTab'
88+
| 'samsung'
89+
| 'samsungCustomTab'
90+
)[];
91+
iosPrefersEphemeralSession?: boolean;
8492
};
8593

8694
export type EndSessionConfiguration = BaseAuthConfiguration & {
8795
additionalParameters?: { [name: string]: string };
8896
dangerouslyAllowInsecureHttpRequests?: boolean;
97+
iosPrefersEphemeralSession?: boolean;
8998
};
9099

91100
export interface AuthorizeResult {

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export const authorize = ({
210210
iosCustomBrowser = null,
211211
androidAllowCustomBrowsers = null,
212212
connectionTimeoutSeconds,
213+
iosPrefersEphemeralSession = false,
213214
}) => {
214215
validateIssuerOrServiceConfigurationEndpoints(issuer, serviceConfiguration);
215216
validateClientId(clientId);
@@ -245,6 +246,7 @@ export const authorize = ({
245246
nativeMethodArguments.push(useNonce);
246247
nativeMethodArguments.push(usePKCE);
247248
nativeMethodArguments.push(iosCustomBrowser);
249+
nativeMethodArguments.push(iosPrefersEphemeralSession);
248250
}
249251

250252
return RNAppAuth.authorize(...nativeMethodArguments);
@@ -356,6 +358,7 @@ export const logout = (
356358
additionalParameters,
357359
dangerouslyAllowInsecureHttpRequests = false,
358360
iosCustomBrowser = null,
361+
iosPrefersEphemeralSession = false,
359362
androidAllowCustomBrowsers = null,
360363
},
361364
{ idToken, postLogoutRedirectUrl }
@@ -379,6 +382,7 @@ export const logout = (
379382

380383
if (Platform.OS === 'ios') {
381384
nativeMethodArguments.push(iosCustomBrowser);
385+
nativeMethodArguments.push(iosPrefersEphemeralSession);
382386
}
383387

384388
return RNAppAuth.logout(...nativeMethodArguments);

index.spec.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('AppAuth', () => {
6161
connectionTimeoutSeconds: TIMEOUT_SEC,
6262
skipCodeExchange: false,
6363
iosCustomBrowser: 'safari',
64+
iosPrefersEphemeralSession: true,
6465
androidAllowCustomBrowsers: ['chrome'],
6566
};
6667

@@ -533,7 +534,8 @@ describe('AppAuth', () => {
533534
config.additionalHeaders,
534535
config.useNonce,
535536
config.usePKCE,
536-
config.iosCustomBrowser
537+
config.iosCustomBrowser,
538+
config.iosPrefersEphemeralSession
537539
);
538540
});
539541

@@ -562,7 +564,8 @@ describe('AppAuth', () => {
562564
null,
563565
true,
564566
true,
565-
null
567+
null,
568+
false
566569
);
567570
});
568571

@@ -587,7 +590,8 @@ describe('AppAuth', () => {
587590
config.additionalHeaders,
588591
config.useNonce,
589592
config.usePKCE,
590-
config.iosCustomBrowser
593+
config.iosCustomBrowser,
594+
config.iosPrefersEphemeralSession
591595
);
592596
});
593597
});
@@ -609,7 +613,8 @@ describe('AppAuth', () => {
609613
additionalHeaders,
610614
config.useNonce,
611615
config.usePKCE,
612-
config.iosCustomBrowser
616+
config.iosCustomBrowser,
617+
config.iosPrefersEphemeralSession
613618
);
614619
});
615620

@@ -641,7 +646,8 @@ describe('AppAuth', () => {
641646
config.additionalHeaders,
642647
true,
643648
true,
644-
config.iosCustomBrowser
649+
config.iosCustomBrowser,
650+
config.iosPrefersEphemeralSession
645651
);
646652
});
647653

@@ -660,7 +666,8 @@ describe('AppAuth', () => {
660666
config.additionalHeaders,
661667
false,
662668
true,
663-
config.iosCustomBrowser
669+
config.iosCustomBrowser,
670+
config.iosPrefersEphemeralSession
664671
);
665672
});
666673
});
@@ -681,7 +688,8 @@ describe('AppAuth', () => {
681688
config.additionalHeaders,
682689
config.useNonce,
683690
true,
684-
config.iosCustomBrowser
691+
config.iosCustomBrowser,
692+
config.iosPrefersEphemeralSession
685693
);
686694
});
687695

@@ -700,7 +708,8 @@ describe('AppAuth', () => {
700708
config.additionalHeaders,
701709
config.useNonce,
702710
false,
703-
config.iosCustomBrowser
711+
config.iosCustomBrowser,
712+
config.iosPrefersEphemeralSession
704713
);
705714
});
706715
});
@@ -1120,7 +1129,8 @@ describe('AppAuth', () => {
11201129
'_redirect_',
11211130
config.serviceConfiguration,
11221131
config.additionalParameters,
1123-
config.iosCustomBrowser
1132+
config.iosCustomBrowser,
1133+
config.iosPrefersEphemeralSession
11241134
);
11251135
});
11261136
});

ios/RNAppAuth.m

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ - (dispatch_queue_t)methodQueue
3434
*/
3535
static NSUInteger const kCodeVerifierBytes = 32;
3636

37+
38+
3739
RCT_EXPORT_MODULE()
3840

3941
RCT_REMAP_METHOD(register,
@@ -98,6 +100,7 @@ - (dispatch_queue_t)methodQueue
98100
useNonce: (BOOL *) useNonce
99101
usePKCE: (BOOL *) usePKCE
100102
iosCustomBrowser: (NSString *) iosCustomBrowser
103+
prefersEphemeralSession: (BOOL *) prefersEphemeralSession
101104
resolve: (RCTPromiseResolveBlock) resolve
102105
reject: (RCTPromiseRejectBlock) reject)
103106
{
@@ -116,6 +119,7 @@ - (dispatch_queue_t)methodQueue
116119
additionalParameters: additionalParameters
117120
skipCodeExchange: skipCodeExchange
118121
iosCustomBrowser: iosCustomBrowser
122+
prefersEphemeralSession: prefersEphemeralSession
119123
resolve: resolve
120124
reject: reject];
121125
} else {
@@ -136,6 +140,7 @@ - (dispatch_queue_t)methodQueue
136140
additionalParameters: additionalParameters
137141
skipCodeExchange: skipCodeExchange
138142
iosCustomBrowser: iosCustomBrowser
143+
prefersEphemeralSession: prefersEphemeralSession
139144
resolve: resolve
140145
reject: reject];
141146
}];
@@ -199,6 +204,7 @@ - (dispatch_queue_t)methodQueue
199204
serviceConfiguration: (NSDictionary *_Nullable) serviceConfiguration
200205
additionalParameters: (NSDictionary *_Nullable) additionalParameters
201206
iosCustomBrowser: (NSString *) iosCustomBrowser
207+
prefersEphemeralSession: (BOOL *) prefersEphemeralSession
202208
resolve:(RCTPromiseResolveBlock) resolve
203209
reject: (RCTPromiseRejectBlock) reject)
204210
{
@@ -209,6 +215,7 @@ - (dispatch_queue_t)methodQueue
209215
postLogoutRedirectURL: postLogoutRedirectURL
210216
additionalParameters: additionalParameters
211217
iosCustomBrowser: iosCustomBrowser
218+
prefersEphemeralSession: prefersEphemeralSession
212219
resolve: resolve
213220
reject: reject];
214221

@@ -224,6 +231,7 @@ - (dispatch_queue_t)methodQueue
224231
postLogoutRedirectURL: postLogoutRedirectURL
225232
additionalParameters: additionalParameters
226233
iosCustomBrowser: iosCustomBrowser
234+
prefersEphemeralSession: prefersEphemeralSession
227235
resolve: resolve
228236
reject: reject];
229237
}];
@@ -322,6 +330,7 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
322330
additionalParameters: (NSDictionary *_Nullable) additionalParameters
323331
skipCodeExchange: (BOOL) skipCodeExchange
324332
iosCustomBrowser: (NSString *) iosCustomBrowser
333+
prefersEphemeralSession: (BOOL *) prefersEphemeralSession
325334
resolve: (RCTPromiseResolveBlock) resolve
326335
reject: (RCTPromiseRejectBlock) reject
327336
{
@@ -383,9 +392,16 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
383392
externalUserAgent:externalUserAgent
384393
callback:callback];
385394
} else {
386-
_currentSession = [OIDAuthorizationService presentAuthorizationRequest:request
395+
if (@available(iOS 13, *)) {
396+
_currentSession = [OIDAuthorizationService presentAuthorizationRequest:request
397+
presentingViewController:presentingViewController
398+
prefersEphemeralSession:prefersEphemeralSession
399+
callback:callback];
400+
} else {
401+
_currentSession = [OIDAuthorizationService presentAuthorizationRequest:request
387402
presentingViewController:presentingViewController
388403
callback:callback];
404+
}
389405
}
390406
} else {
391407

@@ -408,10 +424,16 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
408424
[self getErrorMessage: error], error);
409425
}
410426
};
411-
412-
_currentSession = [OIDAuthState authStateByPresentingAuthorizationRequest:request
413-
presentingViewController:presentingViewController
414-
callback:callback];
427+
if (@available(iOS 13, *)) {
428+
_currentSession = [OIDAuthState authStateByPresentingAuthorizationRequest:request
429+
presentingViewController:presentingViewController
430+
prefersEphemeralSession:prefersEphemeralSession
431+
callback:callback];
432+
} else {
433+
_currentSession = [OIDAuthState authStateByPresentingAuthorizationRequest:request
434+
presentingViewController:presentingViewController
435+
callback:callback];
436+
}
415437
}
416438
}
417439
}
@@ -458,6 +480,7 @@ - (void)endSessionWithConfiguration: (OIDServiceConfiguration *) configuration
458480
postLogoutRedirectURL: (NSString *) postLogoutRedirectURL
459481
additionalParameters: (NSDictionary *_Nullable) additionalParameters
460482
iosCustomBrowser: (NSString *) iosCustomBrowser
483+
prefersEphemeralSession: (BOOL *) prefersEphemeralSession
461484
resolve: (RCTPromiseResolveBlock) resolve
462485
reject: (RCTPromiseRejectBlock) reject {
463486

@@ -482,7 +505,8 @@ - (void)endSessionWithConfiguration: (OIDServiceConfiguration *) configuration
482505

483506
UIViewController *presentingViewController = appDelegate.window.rootViewController.view.window ? appDelegate.window.rootViewController : appDelegate.window.rootViewController.presentedViewController;
484507

485-
id<OIDExternalUserAgent> externalUserAgent = iosCustomBrowser != nil ? [self getCustomBrowser: iosCustomBrowser] : [self getExternalUserAgentWithPresentingViewController:presentingViewController];
508+
id<OIDExternalUserAgent> externalUserAgent = iosCustomBrowser != nil ? [self getCustomBrowser: iosCustomBrowser] : [self getExternalUserAgentWithPresentingViewController:presentingViewController
509+
prefersEphemeralSession:prefersEphemeralSession];
486510

487511
_currentSession = [OIDAuthorizationService presentEndSessionRequest: endSessionRequest
488512
externalUserAgent: externalUserAgent
@@ -695,12 +719,20 @@ - (NSString*)getErrorMessage: (NSError*) error {
695719
}
696720

697721
- (id<OIDExternalUserAgent>)getExternalUserAgentWithPresentingViewController: (UIViewController *)presentingViewController
722+
prefersEphemeralSession: (BOOL *) prefersEphemeralSession
698723
{
699724
id<OIDExternalUserAgent> externalUserAgent;
700725
#if TARGET_OS_MACCATALYST
701726
externalUserAgent = [[OIDExternalUserAgentCatalyst alloc] initWithPresentingViewController:presentingViewController];
702727
#elif TARGET_OS_IOS
703-
externalUserAgent = [[OIDExternalUserAgentIOS alloc] initWithPresentingViewController:presentingViewController];
728+
if (@available(iOS 13, *)) {
729+
externalUserAgent = [[OIDExternalUserAgentIOS alloc] initWithPresentingViewController:
730+
presentingViewController
731+
prefersEphemeralSession:prefersEphemeralSession];
732+
} else {
733+
externalUserAgent = [[OIDExternalUserAgentIOS alloc] initWithPresentingViewController:
734+
presentingViewController];
735+
}
704736
#elif TARGET_OS_OSX
705737
externalUserAgent = [[OIDExternalUserAgentMac alloc] init];
706738
#endif

react-native-app-auth.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ Pod::Spec.new do |s|
1414
s.source_files = 'ios/**/*.{h,m}'
1515
s.requires_arc = true
1616
s.dependency 'React-Core'
17-
s.dependency 'AppAuth', '~> 1.4'
17+
s.dependency 'AppAuth', '~> 1.6'
1818
end

0 commit comments

Comments
 (0)