Skip to content

Commit 48889ae

Browse files
committed
Add emphemeral browser option to macOS user agent.
Adds BOOL arguments to macOS-related methods which allow setting the ASWebAuthenticationSession property prefersEphemeralWebBrowserSession.
1 parent 6d5e581 commit 48889ae

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

Source/AppAuth/macOS/OIDAuthState+Mac.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,39 @@ NS_ASSUME_NONNULL_BEGIN
4141
@return A @c OIDExternalUserAgentSession instance which will terminate when it
4242
receives a @c OIDExternalUserAgentSession.cancel message, or after processing a
4343
@c OIDExternalUserAgentSession.resumeExternalUserAgentFlowWithURL: message.
44-
@discussion This method adopts ASWebAuthenticationSession for macOS 10.15 and above or the default browser otherwise.
44+
@discussion This method adopts ASWebAuthenticationSession for macOS 10.15 and above or the
45+
default browser otherwise.
4546
*/
4647
+ (id<OIDExternalUserAgentSession>)
4748
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
4849
presentingWindow:(NSWindow *)presentingWindow
4950
callback:(OIDAuthStateAuthorizationCallback)callback;
5051

52+
/*! @brief Convenience method to create a @c OIDAuthState by presenting an authorization request
53+
(optionally using an emphemeral browser session that shares no cookies or data with the
54+
normal browser session) and performing the authorization code exchange in the case of code
55+
flow requests. For the hybrid flow, the caller should validate the id_token and c_hash, then
56+
perform the token request (@c OIDAuthorizationService.performTokenRequest:callback:)
57+
and update the OIDAuthState with the results using
58+
@c OIDAuthState.updateWithTokenResponse:error:.
59+
@param authorizationRequest The authorization request to present.
60+
@param presentingWindow The window to present the authentication flow.
61+
@param prefersEphemeralSession Whether the caller prefers to use a private authentication
62+
session. See @c ASWebAuthenticationSession.prefersEphemeralWebBrowserSession for more.
63+
@param callback The method called when the request has completed or failed.
64+
@return A @c OIDExternalUserAgentSession instance which will terminate when it
65+
receives a @c OIDExternalUserAgentSession.cancel message, or after processing a
66+
@c OIDExternalUserAgentSession.resumeExternalUserAgentFlowWithURL: message.
67+
@discussion This method adopts ASWebAuthenticationSession for macOS 10.15 and above or the
68+
default browser otherwise.
69+
*/
70+
+ (id<OIDExternalUserAgentSession>)
71+
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
72+
presentingWindow:(NSWindow *)presentingWindow
73+
prefersEphemeralSession:(BOOL)prefersEphemeralSession
74+
callback:(OIDAuthStateAuthorizationCallback)callback
75+
API_AVAILABLE(macos(10.15));
76+
5177
/*! @param authorizationRequest The authorization request to present.
5278
@param callback The method called when the request has completed or failed.
5379
@return A @c OIDExternalUserAgentSession instance which will terminate when it

Source/AppAuth/macOS/OIDAuthState+Mac.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ @implementation OIDAuthState (Mac)
3535
externalUserAgent:externalUserAgent
3636
callback:callback];
3737
}
38+
+ (id<OIDExternalUserAgentSession>)
39+
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
40+
presentingWindow:(NSWindow *)presentingWindow
41+
prefersEphemeralSession:(BOOL)prefersEphemeralSession
42+
callback:(OIDAuthStateAuthorizationCallback)callback {
43+
OIDExternalUserAgentMac *externalUserAgent =
44+
[[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow
45+
prefersEphemeralSession:prefersEphemeralSession];
46+
return [self authStateByPresentingAuthorizationRequest:authorizationRequest
47+
externalUserAgent:externalUserAgent
48+
callback:callback];
49+
}
3850

3951
+ (id<OIDExternalUserAgentSession>)
4052
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest

Source/AppAuth/macOS/OIDAuthorizationService+Mac.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ NS_ASSUME_NONNULL_BEGIN
4242
presentingWindow:(NSWindow *)presentingWindow
4343
callback:(OIDAuthorizationCallback)callback;
4444

45+
/*! @brief Perform an authorization flow using the @c ASWebAuthenticationSession optionally using an
46+
emphemeral browser session that shares no cookies or data with the normal browser session.
47+
@param request The authorization request.
48+
@param presentingWindow The window to present the authentication flow.
49+
@param prefersEphemeralSession Whether the caller prefers to use a private authentication
50+
session. See @c ASWebAuthenticationSession.prefersEphemeralWebBrowserSession for more.
51+
@param callback The method called when the request has completed or failed.
52+
@return A @c OIDExternalUserAgentSession instance which will terminate when it
53+
receives a @c OIDExternalUserAgentSession.cancel message, or after processing a
54+
@c OIDExternalUserAgentSession.resumeExternalUserAgentFlowWithURL: message.
55+
@discussion This method adopts ASWebAuthenticationSession for macOS 10.15 and above or the default browser otherwise.
56+
*/
57+
+ (id<OIDExternalUserAgentSession>) presentAuthorizationRequest:(OIDAuthorizationRequest *)request
58+
presentingWindow:(NSWindow *)presentingWindow
59+
prefersEphemeralSession:(BOOL)prefersEphemeralSession
60+
callback:(OIDAuthorizationCallback)callback
61+
API_AVAILABLE(macos(10.15));
62+
4563
/*! @brief Perform an authorization flow using the default browser.
4664
@param request The authorization request.
4765
@param callback The method called when the request has completed or failed.

Source/AppAuth/macOS/OIDAuthorizationService+Mac.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ @implementation OIDAuthorizationService (Mac)
3737
callback:callback];
3838
}
3939

40+
+ (id<OIDExternalUserAgentSession>) presentAuthorizationRequest:(OIDAuthorizationRequest *)request
41+
presentingWindow:(NSWindow *)presentingWindow
42+
prefersEphemeralSession:(BOOL)prefersEphemeralSession
43+
callback:(OIDAuthorizationCallback)callback {
44+
OIDExternalUserAgentMac *externalUserAgent =
45+
[[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow
46+
prefersEphemeralSession:prefersEphemeralSession];
47+
return [self presentAuthorizationRequest:request
48+
externalUserAgent:externalUserAgent
49+
callback:callback];
50+
}
51+
4052
+ (id<OIDExternalUserAgentSession>) presentAuthorizationRequest:(OIDAuthorizationRequest *)request
4153
callback:(OIDAuthorizationCallback)callback {
4254
OIDExternalUserAgentMac *externalUserAgent = [[OIDExternalUserAgentMac alloc] init];

Source/AppAuth/macOS/OIDExternalUserAgentMac.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ NS_ASSUME_NONNULL_BEGIN
3535
*/
3636
- (instancetype)initWithPresentingWindow:(NSWindow *)presentingWindow NS_DESIGNATED_INITIALIZER;
3737

38+
/*! @brief Create an iOS user-agent which optionally uses a private authentication session.
39+
@param presentingWindow The window from which to present the ASWebAuthenticationSession.
40+
@param prefersEphemeralSession Whether the caller prefers to use a private authentication
41+
session. See @c ASWebAuthenticationSession.prefersEphemeralWebBrowserSession for more.
42+
*/
43+
- (nullable instancetype)initWithPresentingWindow:(NSWindow *)presentingWindow
44+
prefersEphemeralSession:(BOOL)prefersEphemeralSession
45+
API_AVAILABLE(macos(10.15));
46+
3847
- (instancetype)init __deprecated_msg("Use initWithPresentingWindow for macOS 10.15 and above.");
3948

4049
@end

Source/AppAuth/macOS/OIDExternalUserAgentMac.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ @interface OIDExternalUserAgentMac ()<ASWebAuthenticationPresentationContextProv
3838
@implementation OIDExternalUserAgentMac {
3939
BOOL _externalUserAgentFlowInProgress;
4040
__weak id<OIDExternalUserAgentSession> _session;
41+
BOOL _prefersEphemeralSession;
4142

4243
NSWindow *_presentingWindow;
4344
#pragma clang diagnostic push
@@ -54,6 +55,15 @@ - (instancetype)initWithPresentingWindow:(NSWindow *)presentingWindow {
5455
return self;
5556
}
5657

58+
- (nullable instancetype)initWithPresentingWindow:(NSWindow *)presentingWindow
59+
prefersEphemeralSession:(BOOL)prefersEphemeralSession {
60+
self = [self initWithPresentingWindow:presentingWindow];
61+
if (self) {
62+
_prefersEphemeralSession = prefersEphemeralSession;
63+
}
64+
return self;
65+
}
66+
5767
- (instancetype)init {
5868
#pragma clang diagnostic push
5969
#pragma clang diagnostic ignored "-Wnonnull"
@@ -100,6 +110,7 @@ - (BOOL)presentExternalUserAgentRequest:(id<OIDExternalUserAgentRequest>)request
100110
authenticationSession.presentationContextProvider = self;
101111

102112
_webAuthenticationSession = authenticationSession;
113+
_webAuthenticationSession.prefersEphemeralWebBrowserSession = _prefersEphemeralSession;
103114
return [authenticationSession start];
104115
}
105116
}

0 commit comments

Comments
 (0)