Skip to content

Commit

Permalink
Making the FlowSessions and UICoordinators generic, so they can be us…
Browse files Browse the repository at this point in the history
…ed for more than just authorization purposes. Relates to issue #195
  • Loading branch information
Lucas Farris committed Jan 24, 2018
1 parent d27db29 commit 67c291a
Show file tree
Hide file tree
Showing 27 changed files with 354 additions and 266 deletions.
80 changes: 50 additions & 30 deletions AppAuth.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Source/AppAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#import "OIDAuthorizationRequest.h"
#import "OIDAuthorizationResponse.h"
#import "OIDAuthorizationService.h"
#import "OIDAuthorizationUICoordinator.h"
#import "OIDUserAgentUICoordinator.h"
#import "OIDError.h"
#import "OIDErrorUtilities.h"
#import "OIDGrantTypes.h"
Expand All @@ -43,11 +43,11 @@
#elif TARGET_OS_IOS
#import "OIDAuthState+IOS.h"
#import "OIDAuthorizationService+IOS.h"
#import "OIDAuthorizationUICoordinatorIOS.h"
#import "OIDUserAgentUICoordinatorIOS.h"
#elif TARGET_OS_MAC
#import "OIDAuthState+Mac.h"
#import "OIDAuthorizationService+Mac.h"
#import "OIDAuthorizationUICoordinatorMac.h"
#import "OIDUserAgentUICoordinatorMac.h"
#import "OIDRedirectHTTPHandler.h"
#else
#error "Platform Undefined"
Expand Down
6 changes: 3 additions & 3 deletions Source/Framework/AppAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FOUNDATION_EXPORT const unsigned char AppAuthVersionString[];
#import <AppAuth/OIDAuthorizationRequest.h>
#import <AppAuth/OIDAuthorizationResponse.h>
#import <AppAuth/OIDAuthorizationService.h>
#import <AppAuth/OIDAuthorizationUICoordinator.h>
#import <AppAuth/OIDUserAgentUICoordinator.h>
#import <AppAuth/OIDError.h>
#import <AppAuth/OIDErrorUtilities.h>
#import <AppAuth/OIDGrantTypes.h>
Expand All @@ -50,11 +50,11 @@ FOUNDATION_EXPORT const unsigned char AppAuthVersionString[];
#elif TARGET_OS_IOS
#import <AppAuth/OIDAuthState+IOS.h>
#import <AppAuth/OIDAuthorizationService+IOS.h>
#import <AppAuth/OIDAuthorizationUICoordinatorIOS.h>
#import <AppAuth/OIDUserAgentUICoordinatorIOS.h>
#elif TARGET_OS_MAC
#import <AppAuth/OIDAuthState+Mac.h>
#import <AppAuth/OIDAuthorizationService+Mac.h>
#import <AppAuth/OIDAuthorizationUICoordinatorMac.h>
#import <AppAuth/OIDUserAgentUICoordinatorMac.h>
#import <AppAuth/OIDRedirectHTTPHandler.h>
#else
#error "Platform Undefined"
Expand Down
17 changes: 8 additions & 9 deletions Source/OIDAuthState.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
@class OIDRegistrationResponse;
@class OIDTokenResponse;
@class OIDTokenRequest;
@protocol OIDAuthorizationFlowSession;
@protocol OIDAuthorizationUICoordinator;
@protocol OIDAuthStateChangeDelegate;
@protocol OIDAuthStateErrorDelegate;
@protocol OIDUserAgentUICoordinator;
@protocol OIDUserAgentFlowSession;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -142,16 +142,15 @@ typedef void (^OIDAuthStateAuthorizationCallback)(OIDAuthState *_Nullable authSt
/*! @brief Convenience method to create a @c OIDAuthState by presenting an authorization request
and performing the authorization code exchange in the case of code flow requests.
@param authorizationRequest The authorization request to present.
@param UICoordinator Generic authorization UI coordinator that can present an authorization
request.
@param UICoordinator Generic UI coordinator that can present an user-agent request.
@param callback The method called when the request has completed or failed.
@return A @c OIDAuthorizationFlowSession instance which will terminate when it
receives a @c OIDAuthorizationFlowSession.cancel message, or after processing a
@c OIDAuthorizationFlowSession.resumeAuthorizationFlowWithURL: message.
@return A @c OIDUserAgentFlowSession instance which will terminate when it
receives a @c OIDUserAgentFlowSession.cancel message, or after processing a
@c OIDUserAgentFlowSession.resumeUserAgentFlowWithURL: message.
*/
+ (id<OIDAuthorizationFlowSession>)
+ (id<OIDUserAgentFlowSession>)
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
UICoordinator:(id<OIDAuthorizationUICoordinator>)UICoordinator
UICoordinator:(id<OIDUserAgentUICoordinator>)UICoordinator
callback:(OIDAuthStateAuthorizationCallback)callback;

/*! @internal
Expand Down
10 changes: 5 additions & 5 deletions Source/OIDAuthState.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ @implementation OIDAuthState

#pragma mark - Convenience initializers

+ (id<OIDAuthorizationFlowSession>)
+ (id<OIDUserAgentFlowSession>)
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
UICoordinator:(id<OIDAuthorizationUICoordinator>)UICoordinator
UICoordinator:(id<OIDUserAgentUICoordinator>)UICoordinator
callback:(OIDAuthStateAuthorizationCallback)callback {
// presents the authorization request
id<OIDAuthorizationFlowSession> authFlowSession = [OIDAuthorizationService
id<OIDUserAgentFlowSession> authFlowSession = [OIDAuthorizationService
presentAuthorizationRequest:authorizationRequest
UICoordinator:UICoordinator
callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse,
Expand Down Expand Up @@ -157,15 +157,15 @@ - (nonnull instancetype)init
OID_UNAVAILABLE_USE_INITIALIZER(@selector(initWithAuthorizationResponse:tokenResponse:));

/*! @brief Creates an auth state from an authorization response.
@param response The authorization response.
@param authorizationResponse The authorization response.
*/
- (instancetype)initWithAuthorizationResponse:(OIDAuthorizationResponse *)authorizationResponse {
return [self initWithAuthorizationResponse:authorizationResponse tokenResponse:nil];
}


/*! @brief Designated initializer.
@param response The authorization response.
@param authorizationResponse The authorization response.
@discussion Creates an auth state from an authorization response and token response.
*/
- (instancetype)initWithAuthorizationResponse:(OIDAuthorizationResponse *)authorizationResponse
Expand Down
3 changes: 2 additions & 1 deletion Source/OIDAuthorizationRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// so they are imported here for convenience.
#import "OIDResponseTypes.h"
#import "OIDScopes.h"
#import "OIDUserAgentRequest.h"

@class OIDServiceConfiguration;

Expand All @@ -37,7 +38,7 @@ extern NSString *const OIDOAuthorizationRequestCodeChallengeMethodS256;
@see https://tools.ietf.org/html/rfc6749#section-4
@see https://tools.ietf.org/html/rfc6749#section-4.1.1
*/
@interface OIDAuthorizationRequest : NSObject <NSCopying, NSSecureCoding> {
@interface OIDAuthorizationRequest : NSObject <NSCopying, NSSecureCoding, OIDUserAgentRequest> {
// property variables
OIDServiceConfiguration *_configuration;
NSString *_responseType;
Expand Down
10 changes: 10 additions & 0 deletions Source/OIDAuthorizationRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,14 @@ - (NSURL *)authorizationRequestURL {
return [query URLByReplacingQueryInURL:_configuration.authorizationEndpoint];
}

#pragma mark - OIDUserAgentRequest

- (NSURL *)userAgentRequestURL {
return [self authorizationRequestURL];
}

- (NSString *)redirectScheme {
return [[self redirectURL] scheme];
}

@end
48 changes: 7 additions & 41 deletions Source/OIDAuthorizationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
@class OIDServiceConfiguration;
@class OIDTokenRequest;
@class OIDTokenResponse;
@protocol OIDAuthorizationFlowSession;
@protocol OIDAuthorizationUICoordinator;
@protocol OIDUserAgentFlowSession;
@protocol OIDUserAgentUICoordinator;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -115,13 +115,13 @@ typedef void (^OIDRegistrationCompletion)(OIDRegistrationResponse *_Nullable reg
@param UICoordinator Generic authorization UI coordinator that can present an authorization
request.
@param callback The method called when the request has completed or failed.
@return A @c OIDAuthorizationFlowSession instance which will terminate when it
receives a @c OIDAuthorizationFlowSession.cancel message, or after processing a
@c OIDAuthorizationFlowSession.resumeAuthorizationFlowWithURL: message.
@return A @c OIDUserAgentFlowSession instance which will terminate when it
receives a @c OIDUserAgentFlowSession.cancel message, or after processing a
@c OIDUserAgentFlowSession.resumeUserAgentFlowWithURL: message.
*/
+ (id<OIDAuthorizationFlowSession>)
+ (id<OIDUserAgentFlowSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
UICoordinator:(id<OIDAuthorizationUICoordinator>)UICoordinator
UICoordinator:(id<OIDUserAgentUICoordinator>)UICoordinator
callback:(OIDAuthorizationCallback)callback;

/*! @brief Performs a token request.
Expand All @@ -139,38 +139,4 @@ typedef void (^OIDRegistrationCompletion)(OIDRegistrationResponse *_Nullable reg

@end

/*! @brief Represents an in-flight authorization flow session.
*/
@protocol OIDAuthorizationFlowSession <NSObject>

/*! @brief Cancels the code flow session, invoking the request's callback with a cancelled error.
@remarks Has no effect if called more than once, or after a
@c OIDAuthorizationFlowSession.resumeAuthorizationFlowWithURL: message was received. Will
cause an error with code: @c ::OIDErrorCodeProgramCanceledAuthorizationFlow to be passed to
the @c callback block passed to
@c OIDAuthorizationService.presentAuthorizationRequest:presentingViewController:callback:
*/
- (void)cancel;

/*! @brief Clients should call this method with the result of the authorization code flow if it
becomes available.
@param URL The redirect URL invoked by the authorization server.
@discussion When the URL represented a valid authorization response, implementations
should clean up any left-over UI state from the authorization, for example by
closing the \SFSafariViewController or looback HTTP listener if those were used.
The completion block of the pending authorization request should then be invoked.
@remarks Has no effect if called more than once, or after a @c cancel message was received.
@return YES if the passed URL matches the expected redirect URL and was consumed, NO otherwise.
*/
- (BOOL)resumeAuthorizationFlowWithURL:(NSURL *)URL;

/*! @brief @c OIDAuthorizationUICoordinator or clients should call this method when the
authorization flow failed with a non-OAuth error.
@param error The error that is the reason for the failure of this authorization flow.
@remarks Has no effect if called more than once, or after a @c cancel message was received.
*/
- (void)failAuthorizationFlowWithError:(NSError *)error;

@end

NS_ASSUME_NONNULL_END
33 changes: 17 additions & 16 deletions Source/OIDAuthorizationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

#import "OIDAuthorizationRequest.h"
#import "OIDAuthorizationResponse.h"
#import "OIDAuthorizationUICoordinator.h"
#import "OIDUserAgentUICoordinator.h"
#import "OIDUserAgentFlowSession.h"
#import "OIDDefines.h"
#import "OIDErrorUtilities.h"
#import "OIDRegistrationRequest.h"
Expand All @@ -40,10 +41,10 @@

NS_ASSUME_NONNULL_BEGIN

@interface OIDAuthorizationFlowSessionImplementation : NSObject<OIDAuthorizationFlowSession> {
@interface OIDAuthorizationFlowSessionImplementation : NSObject<OIDUserAgentFlowSession> {
// private variables
OIDAuthorizationRequest *_request;
id<OIDAuthorizationUICoordinator> _UICoordinator;
id<OIDUserAgentUICoordinator> _UICoordinator;
OIDAuthorizationCallback _pendingauthorizationFlowCallback;
}

Expand All @@ -64,12 +65,12 @@ - (instancetype)initWithRequest:(OIDAuthorizationRequest *)request {
return self;
}

- (void)presentAuthorizationWithCoordinator:(id<OIDAuthorizationUICoordinator>)UICoordinator
- (void)presentAuthorizationWithCoordinator:(id<OIDUserAgentUICoordinator>)UICoordinator
callback:(OIDAuthorizationCallback)authorizationFlowCallback {
_UICoordinator = UICoordinator;
_pendingauthorizationFlowCallback = authorizationFlowCallback;
BOOL authorizationFlowStarted =
[_UICoordinator presentAuthorizationRequest:_request session:self];
[_UICoordinator presentUserAgentRequest:_request session:self];
if (!authorizationFlowStarted) {
NSError *safariError = [OIDErrorUtilities errorWithCode:OIDErrorCodeSafariOpenError
underlyingError:nil
Expand All @@ -79,9 +80,9 @@ - (void)presentAuthorizationWithCoordinator:(id<OIDAuthorizationUICoordinator>)U
}

- (void)cancel {
[_UICoordinator dismissAuthorizationAnimated:YES
completion:^{
NSError *error = [OIDErrorUtilities
[_UICoordinator dismissUserAgentUIAnimated:YES
completion:^{
NSError *error = [OIDErrorUtilities
errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
underlyingError:nil
description:nil];
Expand All @@ -101,7 +102,7 @@ - (BOOL)shouldHandleURL:(NSURL *)URL {
OIDIsEqualIncludingNil(standardizedURL.path, standardizedRedirectURL.path);
}

- (BOOL)resumeAuthorizationFlowWithURL:(NSURL *)URL {
- (BOOL)resumeUserAgentFlowWithURL:(NSURL *)URL {
// rejects URLs that don't match redirect (these may be completely unrelated to the authorization)
if (![self shouldHandleURL:URL]) {
return NO;
Expand Down Expand Up @@ -145,15 +146,15 @@ - (BOOL)resumeAuthorizationFlowWithURL:(NSURL *)URL {
}
}

[_UICoordinator dismissAuthorizationAnimated:YES
completion:^{
[self didFinishWithResponse:response error:error];
}];
[_UICoordinator dismissUserAgentUIAnimated:YES
completion:^{
[self didFinishWithResponse:response error:error];
}];

return YES;
}

- (void)failAuthorizationFlowWithError:(NSError *)error {
- (void)failUserAgentFlowWithError:(NSError *)error {
[self didFinishWithResponse:nil error:error];
}

Expand Down Expand Up @@ -245,9 +246,9 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL

#pragma mark - Authorization Endpoint

+ (id<OIDAuthorizationFlowSession>)
+ (id<OIDUserAgentFlowSession>)
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
UICoordinator:(id<OIDAuthorizationUICoordinator>)UICoordinator
UICoordinator:(id<OIDUserAgentUICoordinator>)UICoordinator
callback:(OIDAuthorizationCallback)callback {
OIDAuthorizationFlowSessionImplementation *flowSession =
[[OIDAuthorizationFlowSessionImplementation alloc] initWithRequest:request];
Expand Down
53 changes: 0 additions & 53 deletions Source/OIDAuthorizationUICoordinator.h

This file was deleted.

Loading

0 comments on commit 67c291a

Please sign in to comment.