Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AppAuth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
25 changes: 25 additions & 0 deletions Sources/AppAuth/iOS/OIDAuthorizationService+IOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<OIDExternalUserAgentSession>)
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<OIDExternalUserAgentSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingViewController:(UIViewController *)presentingViewController
prefersEphemeralSession:(BOOL)prefersEphemeralSession
callback:(OIDAuthorizationCallback)callback;

@end

NS_ASSUME_NONNULL_END
Expand Down
82 changes: 64 additions & 18 deletions Sources/AppAuth/iOS/OIDAuthorizationService+IOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,81 @@

NS_ASSUME_NONNULL_BEGIN

@implementation OIDAuthorizationService (IOS)

+ (id<OIDExternalUserAgentSession>) presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingViewController:(UIViewController *)presentingViewController
callback:(OIDAuthorizationCallback)callback {
id<OIDExternalUserAgent> externalUserAgent;
/*! @brief The platform's external user agent, presenting from @c presentingViewController.
*/
static id<OIDExternalUserAgent> 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<OIDExternalUserAgentSession>) presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingViewController:(UIViewController *)presentingViewController
prefersEphemeralSession:(BOOL)prefersEphemeralSession
callback:(OIDAuthorizationCallback)callback {
id<OIDExternalUserAgent> externalUserAgent;
/*! @brief The platform's external user agent, presenting from @c presentingViewController, with
the given ephemeral session preference.
*/
static id<OIDExternalUserAgent> 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<OIDExternalUserAgentSession>) presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingViewController:(UIViewController *)presentingViewController
callback:(OIDAuthorizationCallback)callback {
id<OIDExternalUserAgent> externalUserAgent =
OIDExternalUserAgentForViewController(presentingViewController);
return [self presentAuthorizationRequest:request
externalUserAgent:externalUserAgent
callback:callback];
}

+ (id<OIDExternalUserAgentSession>) presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingViewController:(UIViewController *)presentingViewController
prefersEphemeralSession:(BOOL)prefersEphemeralSession
callback:(OIDAuthorizationCallback)callback {
id<OIDExternalUserAgent> externalUserAgent =
OIDEphemeralCapableExternalUserAgentForViewController(presentingViewController,
prefersEphemeralSession);
return [self presentAuthorizationRequest:request
externalUserAgent:externalUserAgent
callback:callback];
}

- (id<OIDExternalUserAgentSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingViewController:(UIViewController *)presentingViewController
callback:(OIDAuthorizationCallback)callback {
id<OIDExternalUserAgent> externalUserAgent =
OIDExternalUserAgentForViewController(presentingViewController);
return [self presentAuthorizationRequest:request
externalUserAgent:externalUserAgent
callback:callback];
}

- (id<OIDExternalUserAgentSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingViewController:(UIViewController *)presentingViewController
prefersEphemeralSession:(BOOL)prefersEphemeralSession
callback:(OIDAuthorizationCallback)callback {
id<OIDExternalUserAgent> externalUserAgent =
OIDEphemeralCapableExternalUserAgentForViewController(presentingViewController,
prefersEphemeralSession);
return [self presentAuthorizationRequest:request
externalUserAgent:externalUserAgent
callback:callback];
}

@end
Expand Down
4 changes: 4 additions & 0 deletions Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ NS_ASSUME_NONNULL_BEGIN
API_AVAILABLE(macCatalyst(13)) API_UNAVAILABLE(ios)
@interface OIDExternalUserAgentCatalyst : NSObject<OIDExternalUserAgent>

/*! @brief The view controller from which the authentication UI is presented.
*/
@property(nonatomic, readonly, nullable) UIViewController *presentingViewController;

/*! @internal
@brief Unavailable. Please use @c initWithPresentingViewController:
*/
Expand Down
2 changes: 2 additions & 0 deletions Sources/AppAuth/iOS/OIDExternalUserAgentCatalyst.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ @implementation OIDExternalUserAgentCatalyst {
ASWebAuthenticationSession *_webAuthenticationVC;
}

@synthesize presentingViewController = _presentingViewController;

- (nullable instancetype)initWithPresentingViewController:
(UIViewController *)presentingViewController {
self = [super init];
Expand Down
4 changes: 4 additions & 0 deletions Sources/AppAuth/iOS/OIDExternalUserAgentIOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ NS_ASSUME_NONNULL_BEGIN
API_UNAVAILABLE(macCatalyst)
@interface OIDExternalUserAgentIOS : NSObject<OIDExternalUserAgent>

/*! @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");
Expand Down
2 changes: 2 additions & 0 deletions Sources/AppAuth/iOS/OIDExternalUserAgentIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions Sources/AppAuth/macOS/OIDAuthorizationService+Mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<OIDExternalUserAgentSession>)
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<OIDExternalUserAgentSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingWindow:(NSWindow *)presentingWindow
prefersEphemeralSession:(BOOL)prefersEphemeralSession
callback:(OIDAuthorizationCallback)callback;

@end

NS_ASSUME_NONNULL_END
Expand Down
24 changes: 24 additions & 0 deletions Sources/AppAuth/macOS/OIDAuthorizationService+Mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ @implementation OIDAuthorizationService (Mac)
callback:callback];
}

- (id<OIDExternalUserAgentSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
presentingWindow:(NSWindow *)presentingWindow
callback:(OIDAuthorizationCallback)callback {
OIDExternalUserAgentMac *externalUserAgent =
[[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow];
return [self presentAuthorizationRequest:request
externalUserAgent:externalUserAgent
callback:callback];
}

- (id<OIDExternalUserAgentSession>)
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
Expand Down
4 changes: 4 additions & 0 deletions Sources/AppAuth/macOS/OIDExternalUserAgentMac.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ NS_ASSUME_NONNULL_BEGIN
*/
@interface OIDExternalUserAgentMac : NSObject <OIDExternalUserAgent>

/*! @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.
Expand Down
2 changes: 2 additions & 0 deletions Sources/AppAuth/macOS/OIDExternalUserAgentMac.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ @implementation OIDExternalUserAgentMac {
#pragma clang diagnostic pop
}

@synthesize presentingWindow = _presentingWindow;

- (instancetype)initWithPresentingWindow:(NSWindow *)presentingWindow {
self = [super init];
if (self) {
Expand Down
93 changes: 93 additions & 0 deletions UnitTests/OIDExternalUserAgentIOSTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@

#import <AuthenticationServices/AuthenticationServices.h>

#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"
Expand Down Expand Up @@ -78,6 +81,51 @@ - (NSString *)redirectScheme {

@end

/*! @brief An external user agent that refuses to present anything.
*/
@interface OIDRefusingIOSTestUserAgent : NSObject <OIDExternalUserAgent>
@end

@implementation OIDRefusingIOSTestUserAgent

- (BOOL)presentExternalUserAgentRequest:(id<OIDExternalUserAgentRequest>)request
session:(id<OIDExternalUserAgentSession>)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<OIDExternalUserAgent> lastExternalUserAgent;

@end

@implementation OIDUserAgentRecordingAuthorizationService

- (id<OIDExternalUserAgentSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
externalUserAgent:(id<OIDExternalUserAgent>)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
Expand Down Expand Up @@ -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
Expand Down
Loading