diff --git a/AppAuth.xcodeproj/project.pbxproj b/AppAuth.xcodeproj/project.pbxproj index 1c61157b5..02a940914 100644 --- a/AppAuth.xcodeproj/project.pbxproj +++ b/AppAuth.xcodeproj/project.pbxproj @@ -2959,6 +2959,7 @@ INFOPLIST_FILE = UnitTests/UnitTestsInfo.plist; IPHONEOS_DEPLOYMENT_TARGET = 15.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + OTHER_LDFLAGS = "-ObjC"; PRODUCT_BUNDLE_IDENTIFIER = net.openid.appauth.AppAuthTests; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "UnitTests/OIDAppAuthTests-Bridging-Header.h"; @@ -2976,6 +2977,7 @@ INFOPLIST_FILE = UnitTests/UnitTestsInfo.plist; IPHONEOS_DEPLOYMENT_TARGET = 15.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + OTHER_LDFLAGS = "-ObjC"; PRODUCT_BUNDLE_IDENTIFIER = net.openid.appauth.AppAuthTests; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "UnitTests/OIDAppAuthTests-Bridging-Header.h"; diff --git a/CHANGELOG.md b/CHANGELOG.md index fa17ca1c2..0fd6bb026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Fix a crash (`NSInvalidArgumentException`) when a token, registration or device authorization endpoint answers with a JSON array instead of an object. The callback now gets an error. Addresses issue #987. - Added instance-method counterparts of the `OIDAuthorizationService` class methods, and made `-init` public, so the service can be injected and faked in tests. - Deprecated `OIDAuthorizationService.configuration`. It is never set and is always `nil`; use the request's `configuration` instead. It will be removed in 4.0. +- Added instance-method counterparts of the iOS and macOS `OIDAuthorizationService` convenience methods, and read-only `presentingViewController` / `presentingWindow` properties on the external user agents. # 3.0.0 - BREAKING: Updates made to support Xcode 27. ([#972](https://github.com/openid/AppAuth-iOS/pull/972), [#973](https://github.com/openid/AppAuth-iOS/pull/973)) diff --git a/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.h b/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.h index c7c685d28..86ae01988 100644 --- a/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.h +++ b/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.h @@ -60,6 +60,31 @@ NS_ASSUME_NONNULL_BEGIN prefersEphemeralSession:(BOOL)prefersEphemeralSession callback:(OIDAuthorizationCallback)callback API_AVAILABLE(ios(13)); +/*! @brief Instance counterpart of + @c +presentAuthorizationRequest:presentingViewController:callback:. + @param request The authorization request. + @param presentingViewController The view controller from which to present authentication UI. + @param callback The method called when the request has completed or failed. + */ +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + callback:(OIDAuthorizationCallback)callback; + +/*! @brief Instance counterpart of + @c +presentAuthorizationRequest:presentingViewController:prefersEphemeralSession:callback:. + @param request The authorization request. + @param presentingViewController The view controller from which to present authentication UI. + @param prefersEphemeralSession Whether the caller prefers to use a private authentication + session. See @c ASWebAuthenticationSession.prefersEphemeralWebBrowserSession for more. + @param callback The method called when the request has completed or failed. + */ +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + prefersEphemeralSession:(BOOL)prefersEphemeralSession + callback:(OIDAuthorizationCallback)callback; + @end NS_ASSUME_NONNULL_END diff --git a/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.m b/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.m index 4ca07c55e..7c81bbda6 100644 --- a/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.m +++ b/Sources/AppAuth/iOS/OIDAuthorizationService+IOS.m @@ -26,35 +26,81 @@ NS_ASSUME_NONNULL_BEGIN -@implementation OIDAuthorizationService (IOS) - -+ (id) presentAuthorizationRequest:(OIDAuthorizationRequest *)request - presentingViewController:(UIViewController *)presentingViewController - callback:(OIDAuthorizationCallback)callback { - id externalUserAgent; +/*! @brief The platform's external user agent, presenting from @c presentingViewController. + */ +static id OIDExternalUserAgentForViewController( + UIViewController *presentingViewController) { #if TARGET_OS_MACCATALYST - externalUserAgent = [[OIDExternalUserAgentCatalyst alloc] + return [[OIDExternalUserAgentCatalyst alloc] initWithPresentingViewController:presentingViewController]; #else // TARGET_OS_MACCATALYST - externalUserAgent = [[OIDExternalUserAgentIOS alloc] initWithPresentingViewController:presentingViewController]; + return [[OIDExternalUserAgentIOS alloc] + initWithPresentingViewController:presentingViewController]; #endif // TARGET_OS_MACCATALYST - return [self presentAuthorizationRequest:request externalUserAgent:externalUserAgent callback:callback]; } -+ (id) presentAuthorizationRequest:(OIDAuthorizationRequest *)request - presentingViewController:(UIViewController *)presentingViewController - prefersEphemeralSession:(BOOL)prefersEphemeralSession - callback:(OIDAuthorizationCallback)callback { - id externalUserAgent; +/*! @brief The platform's external user agent, presenting from @c presentingViewController, with + the given ephemeral session preference. + */ +static id OIDEphemeralCapableExternalUserAgentForViewController( + UIViewController *presentingViewController, BOOL prefersEphemeralSession) { #if TARGET_OS_MACCATALYST - externalUserAgent = [[OIDExternalUserAgentCatalyst alloc] + return [[OIDExternalUserAgentCatalyst alloc] initWithPresentingViewController:presentingViewController - prefersEphemeralSession:prefersEphemeralSession]; + prefersEphemeralSession:prefersEphemeralSession]; #else // TARGET_OS_MACCATALYST - externalUserAgent = [[OIDExternalUserAgentIOS alloc] initWithPresentingViewController:presentingViewController + return [[OIDExternalUserAgentIOS alloc] + initWithPresentingViewController:presentingViewController prefersEphemeralSession:prefersEphemeralSession]; #endif // TARGET_OS_MACCATALYST - return [self presentAuthorizationRequest:request externalUserAgent:externalUserAgent callback:callback]; +} + +@implementation OIDAuthorizationService (IOS) + ++ (id) presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + callback:(OIDAuthorizationCallback)callback { + id externalUserAgent = + OIDExternalUserAgentForViewController(presentingViewController); + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + ++ (id) presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + prefersEphemeralSession:(BOOL)prefersEphemeralSession + callback:(OIDAuthorizationCallback)callback { + id externalUserAgent = + OIDEphemeralCapableExternalUserAgentForViewController(presentingViewController, + prefersEphemeralSession); + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + callback:(OIDAuthorizationCallback)callback { + id externalUserAgent = + OIDExternalUserAgentForViewController(presentingViewController); + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + prefersEphemeralSession:(BOOL)prefersEphemeralSession + callback:(OIDAuthorizationCallback)callback { + id externalUserAgent = + OIDEphemeralCapableExternalUserAgentForViewController(presentingViewController, + prefersEphemeralSession); + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; } @end diff --git a/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.h b/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.h index 910d0bb86..e79f3f07f 100644 --- a/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.h +++ b/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.h @@ -32,6 +32,10 @@ NS_ASSUME_NONNULL_BEGIN API_AVAILABLE(macCatalyst(13)) API_UNAVAILABLE(ios) @interface OIDExternalUserAgentCatalyst : NSObject +/*! @brief The view controller from which the authentication UI is presented. + */ +@property(nonatomic, readonly, nullable) UIViewController *presentingViewController; + /*! @internal @brief Unavailable. Please use @c initWithPresentingViewController: */ diff --git a/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.m b/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.m index 4c10c154d..c57db89f7 100644 --- a/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.m +++ b/Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.m @@ -45,6 +45,8 @@ @implementation OIDExternalUserAgentCatalyst { ASWebAuthenticationSession *_webAuthenticationVC; } +@synthesize presentingViewController = _presentingViewController; + - (nullable instancetype)initWithPresentingViewController: (UIViewController *)presentingViewController { self = [super init]; diff --git a/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.h b/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.h index c7916a52e..67b07b5e3 100644 --- a/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.h +++ b/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.h @@ -34,6 +34,10 @@ NS_ASSUME_NONNULL_BEGIN API_UNAVAILABLE(macCatalyst) @interface OIDExternalUserAgentIOS : NSObject +/*! @brief The view controller from which the authentication UI is presented. + */ +@property(nonatomic, readonly, nullable) UIViewController *presentingViewController; + - (null_unspecified instancetype)init API_AVAILABLE(ios(11)) __deprecated_msg("This method will not work on iOS 13, use " "initWithPresentingViewController:presentingViewController"); diff --git a/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.m b/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.m index eac47acf5..7a6879c67 100644 --- a/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.m +++ b/Sources/AppAuth/iOS/OIDExternalUserAgentIOS.m @@ -81,6 +81,8 @@ @implementation OIDExternalUserAgentIOS { #pragma clang diagnostic pop } +@synthesize presentingViewController = _presentingViewController; + - (null_unspecified instancetype)init { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wnonnull" diff --git a/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.h b/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.h index 0a34faf28..973e5f8e0 100644 --- a/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.h +++ b/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.h @@ -71,6 +71,30 @@ NS_ASSUME_NONNULL_BEGIN callback:(OIDAuthorizationCallback)callback __deprecated_msg("For macOS 10.15 and above please use presentAuthorizationRequest:presentingWindow:callback:"); +/*! @brief Instance counterpart of @c +presentAuthorizationRequest:presentingWindow:callback:. + @param request The authorization request. + @param presentingWindow The window to present the authentication flow. + @param callback The method called when the request has completed or failed. + */ +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingWindow:(NSWindow *)presentingWindow + callback:(OIDAuthorizationCallback)callback; + +/*! @brief Instance counterpart of + @c +presentAuthorizationRequest:presentingWindow:prefersEphemeralSession:callback:. + @param request The authorization request. + @param presentingWindow The window to present the authentication flow. + @param prefersEphemeralSession Whether the caller prefers to use a private authentication + session. See @c ASWebAuthenticationSession.prefersEphemeralWebBrowserSession for more. + @param callback The method called when the request has completed or failed. + */ +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingWindow:(NSWindow *)presentingWindow + prefersEphemeralSession:(BOOL)prefersEphemeralSession + callback:(OIDAuthorizationCallback)callback; + @end NS_ASSUME_NONNULL_END diff --git a/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.m b/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.m index 4d3d9a038..19763614a 100644 --- a/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.m +++ b/Sources/AppAuth/macOS/OIDAuthorizationService+Mac.m @@ -57,6 +57,30 @@ @implementation OIDAuthorizationService (Mac) callback:callback]; } +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingWindow:(NSWindow *)presentingWindow + callback:(OIDAuthorizationCallback)callback { + OIDExternalUserAgentMac *externalUserAgent = + [[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow]; + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingWindow:(NSWindow *)presentingWindow + prefersEphemeralSession:(BOOL)prefersEphemeralSession + callback:(OIDAuthorizationCallback)callback { + OIDExternalUserAgentMac *externalUserAgent = + [[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow + prefersEphemeralSession:prefersEphemeralSession]; + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + @end NS_ASSUME_NONNULL_END diff --git a/Sources/AppAuth/macOS/OIDExternalUserAgentMac.h b/Sources/AppAuth/macOS/OIDExternalUserAgentMac.h index 360c44ad6..d7e3c35a7 100644 --- a/Sources/AppAuth/macOS/OIDExternalUserAgentMac.h +++ b/Sources/AppAuth/macOS/OIDExternalUserAgentMac.h @@ -30,6 +30,10 @@ NS_ASSUME_NONNULL_BEGIN */ @interface OIDExternalUserAgentMac : NSObject +/*! @brief The window from which the authentication UI is presented. + */ +@property(nonatomic, readonly, nullable) NSWindow *presentingWindow; + /*! @brief The designated initializer. @param presentingWindow The window from which to present the @c ASWebAuthenticationSession on macOS 10.15 and above. Older macOS versions use the system browser. diff --git a/Sources/AppAuth/macOS/OIDExternalUserAgentMac.m b/Sources/AppAuth/macOS/OIDExternalUserAgentMac.m index 184767668..edfad58f8 100644 --- a/Sources/AppAuth/macOS/OIDExternalUserAgentMac.m +++ b/Sources/AppAuth/macOS/OIDExternalUserAgentMac.m @@ -47,6 +47,8 @@ @implementation OIDExternalUserAgentMac { #pragma clang diagnostic pop } +@synthesize presentingWindow = _presentingWindow; + - (instancetype)initWithPresentingWindow:(NSWindow *)presentingWindow { self = [super init]; if (self) { diff --git a/UnitTests/OIDExternalUserAgentIOSTests.m b/UnitTests/OIDExternalUserAgentIOSTests.m index 7cc7fd8c6..e01a80080 100644 --- a/UnitTests/OIDExternalUserAgentIOSTests.m +++ b/UnitTests/OIDExternalUserAgentIOSTests.m @@ -26,10 +26,13 @@ #import +#import "Sources/AppAuth/iOS/OIDAuthorizationService+IOS.h" #import "Sources/AppAuth/iOS/OIDExternalUserAgentIOS.h" #import "Sources/AppAuthCore/OIDAuthorizationRequest.h" +#import "Sources/AppAuthCore/OIDAuthorizationService.h" #import "Sources/AppAuthCore/OIDEndSessionRequest.h" #import "Sources/AppAuthCore/OIDExternalUserAgentRequest.h" +#import "Sources/AppAuthCore/OIDExternalUserAgentSession.h" #import "Sources/AppAuthCore/OIDResponseTypes.h" #import "Sources/AppAuthCore/OIDScopes.h" #import "Sources/AppAuthCore/OIDServiceConfiguration.h" @@ -78,6 +81,51 @@ - (NSString *)redirectScheme { @end +/*! @brief An external user agent that refuses to present anything. + */ +@interface OIDRefusingIOSTestUserAgent : NSObject +@end + +@implementation OIDRefusingIOSTestUserAgent + +- (BOOL)presentExternalUserAgentRequest:(id)request + session:(id)session { + return NO; +} + +- (void)dismissExternalUserAgentAnimated:(BOOL)animated completion:(void (^)(void))completion { + if (completion) { + completion(); + } +} + +@end + +/*! @brief Records the external user agent it is handed, then fails the flow with a user agent + that refuses to present. + */ +@interface OIDUserAgentRecordingAuthorizationService : OIDAuthorizationService + +/*! @brief The external user agent passed to the most recent authorization request. + */ +@property(nonatomic, readonly, nullable) id lastExternalUserAgent; + +@end + +@implementation OIDUserAgentRecordingAuthorizationService + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + externalUserAgent:(id)externalUserAgent + callback:(OIDAuthorizationCallback)callback { + _lastExternalUserAgent = externalUserAgent; + return [super presentAuthorizationRequest:request + externalUserAgent:[[OIDRefusingIOSTestUserAgent alloc] init] + callback:callback]; +} + +@end + /*! @brief Unit tests for the iOS external user agent's HTTPS redirect callback support. */ @interface OIDExternalUserAgentIOSTests : XCTestCase @@ -189,6 +237,51 @@ - (void)testNoHTTPSCallbackForUnsupportedRequestType { } } +/*! @brief The presenting view controller reads back as the one passed to the initializer. + */ +- (void)testPresentingViewControllerIsTheOneItWasCreatedWith { + UIViewController *presentingViewController = [[UIViewController alloc] init]; + OIDExternalUserAgentIOS *userAgent = [[OIDExternalUserAgentIOS alloc] + initWithPresentingViewController:presentingViewController]; + XCTAssertEqual(userAgent.presentingViewController, presentingViewController); +} + +/*! @brief The instance convenience method hands an iOS user agent for the given view controller to + the instance core method. + */ +- (void)testInstancePresentAuthorizationRequestBuildsIOSUserAgent { + OIDUserAgentRecordingAuthorizationService *service = + [[OIDUserAgentRecordingAuthorizationService alloc] init]; + UIViewController *presentingViewController = [[UIViewController alloc] init]; + OIDAuthorizationRequest *request = [self authorizationRequestWithRedirectURL: + [NSURL URLWithString:@"com.example.app:/oauth2redirect"]]; + [service presentAuthorizationRequest:request + presentingViewController:presentingViewController + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) {}]; + XCTAssertTrue([service.lastExternalUserAgent isKindOfClass:[OIDExternalUserAgentIOS class]]); + OIDExternalUserAgentIOS *userAgent = (OIDExternalUserAgentIOS *)service.lastExternalUserAgent; + XCTAssertEqual(userAgent.presentingViewController, presentingViewController); +} + +/*! @brief The ephemeral instance convenience method does the same. + */ +- (void)testInstancePresentAuthorizationRequestWithEphemeralSessionBuildsIOSUserAgent { + OIDUserAgentRecordingAuthorizationService *service = + [[OIDUserAgentRecordingAuthorizationService alloc] init]; + UIViewController *presentingViewController = [[UIViewController alloc] init]; + OIDAuthorizationRequest *request = [self authorizationRequestWithRedirectURL: + [NSURL URLWithString:@"com.example.app:/oauth2redirect"]]; + [service presentAuthorizationRequest:request + presentingViewController:presentingViewController + prefersEphemeralSession:YES + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) {}]; + XCTAssertTrue([service.lastExternalUserAgent isKindOfClass:[OIDExternalUserAgentIOS class]]); + OIDExternalUserAgentIOS *userAgent = (OIDExternalUserAgentIOS *)service.lastExternalUserAgent; + XCTAssertEqual(userAgent.presentingViewController, presentingViewController); +} + @end #pragma GCC diagnostic pop