From 69d0b3bf15ad420358a8ebbcdb8407d5fd6dc31c Mon Sep 17 00:00:00 2001 From: Harsh Date: Mon, 30 Oct 2017 21:40:15 -0700 Subject: [PATCH] Making NSURLSession extensible --- AppAuth.xcodeproj/project.pbxproj | 10 ++++++ Source/AppAuth.h | 1 + Source/OIDAuthorizationService.m | 7 +++-- Source/OIDURLSessionProvider.h | 40 ++++++++++++++++++++++++ Source/OIDURLSessionProvider.m | 39 +++++++++++++++++++++++ UnitTests/OIDURLSessionProviderTests.m | 43 ++++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 Source/OIDURLSessionProvider.h create mode 100644 Source/OIDURLSessionProvider.m create mode 100644 UnitTests/OIDURLSessionProviderTests.m diff --git a/AppAuth.xcodeproj/project.pbxproj b/AppAuth.xcodeproj/project.pbxproj index 9ed2c8830..26239dcec 100644 --- a/AppAuth.xcodeproj/project.pbxproj +++ b/AppAuth.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 039697461FA8258D003D1FB2 /* OIDURLSessionProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 039697451FA8258D003D1FB2 /* OIDURLSessionProvider.m */; }; + 0396974D1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0396974C1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m */; }; 340DAE571D5821A100EC285B /* OIDAuthorizationService+Mac.m in Sources */ = {isa = PBXBuildFile; fileRef = 340DAE261D581FE700EC285B /* OIDAuthorizationService+Mac.m */; }; 340DAE581D5821A100EC285B /* OIDAuthorizationUICoordinatorMac.m in Sources */ = {isa = PBXBuildFile; fileRef = 340DAE281D581FE700EC285B /* OIDAuthorizationUICoordinatorMac.m */; }; 340DAE591D5821A100EC285B /* OIDAuthState+Mac.m in Sources */ = {isa = PBXBuildFile; fileRef = 340DAE2A1D581FE700EC285B /* OIDAuthState+Mac.m */; }; @@ -446,6 +448,9 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 039697441FA8258D003D1FB2 /* OIDURLSessionProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OIDURLSessionProvider.h; sourceTree = ""; }; + 039697451FA8258D003D1FB2 /* OIDURLSessionProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OIDURLSessionProvider.m; sourceTree = ""; }; + 0396974C1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OIDURLSessionProviderTests.m; sourceTree = ""; }; 340DAE251D581FE700EC285B /* OIDAuthorizationService+Mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "OIDAuthorizationService+Mac.h"; sourceTree = ""; }; 340DAE261D581FE700EC285B /* OIDAuthorizationService+Mac.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "OIDAuthorizationService+Mac.m"; sourceTree = ""; }; 340DAE271D581FE700EC285B /* OIDAuthorizationUICoordinatorMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OIDAuthorizationUICoordinatorMac.h; sourceTree = ""; }; @@ -762,6 +767,8 @@ 341741D61C5D8243000EF209 /* OIDTokenUtilities.m */, 341741D71C5D8243000EF209 /* OIDURLQueryComponent.h */, 341741D81C5D8243000EF209 /* OIDURLQueryComponent.m */, + 039697441FA8258D003D1FB2 /* OIDURLSessionProvider.h */, + 039697451FA8258D003D1FB2 /* OIDURLSessionProvider.m */, ); path = Source; sourceTree = ""; @@ -796,6 +803,7 @@ 60140F851DE43CC700DA0DC3 /* OIDRegistrationResponseTests.m */, 34D5EC441E6D1AD900814354 /* OIDSwiftTests.swift */, 34D5EC431E6D1AD900814354 /* OIDAppAuthTests-Bridging-Header.h */, + 0396974C1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m */, ); path = UnitTests; sourceTree = ""; @@ -1444,6 +1452,7 @@ 340DAECD1D582DE100EC285B /* OIDAuthorizationUICoordinatorIOS.m in Sources */, 341741EB1C5D8243000EF209 /* OIDURLQueryComponent.m in Sources */, 341741E11C5D8243000EF209 /* OIDFieldMapping.m in Sources */, + 039697461FA8258D003D1FB2 /* OIDURLSessionProvider.m in Sources */, 341741DF1C5D8243000EF209 /* OIDError.m in Sources */, 341741DB1C5D8243000EF209 /* OIDAuthorizationRequest.m in Sources */, 60140F801DE4344200DA0DC3 /* OIDRegistrationResponse.m in Sources */, @@ -1466,6 +1475,7 @@ 3417421F1C5D82D3000EF209 /* OIDTokenRequestTests.m in Sources */, 341742181C5D82D3000EF209 /* OIDAuthorizationResponseTests.m in Sources */, 341742171C5D82D3000EF209 /* OIDAuthorizationRequestTests.m in Sources */, + 0396974D1FA827AD003D1FB2 /* OIDURLSessionProviderTests.m in Sources */, 3417421A1C5D82D3000EF209 /* OIDGrantTypesTests.m in Sources */, 3417421B1C5D82D3000EF209 /* OIDResponseTypesTests.m in Sources */, 60140F831DE43BAF00DA0DC3 /* OIDRegistrationRequestTests.m in Sources */, diff --git a/Source/AppAuth.h b/Source/AppAuth.h index 06ec1e371..cfd3f9c62 100644 --- a/Source/AppAuth.h +++ b/Source/AppAuth.h @@ -36,6 +36,7 @@ #import "OIDTokenRequest.h" #import "OIDTokenResponse.h" #import "OIDTokenUtilities.h" +#import "OIDURLSessionProvider.h" #if TARGET_OS_TV #elif TARGET_OS_WATCH diff --git a/Source/OIDAuthorizationService.m b/Source/OIDAuthorizationService.m index 48f56ab0a..a475e37ed 100644 --- a/Source/OIDAuthorizationService.m +++ b/Source/OIDAuthorizationService.m @@ -30,6 +30,7 @@ #import "OIDTokenRequest.h" #import "OIDTokenResponse.h" #import "OIDURLQueryComponent.h" +#import "OIDURLSessionProvider.h" /*! @brief Path appended to an OpenID Connect issuer for discovery @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig @@ -191,7 +192,7 @@ + (void)discoverServiceConfigurationForIssuer:(NSURL *)issuerURL + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL completion:(OIDDiscoveryCallback)completion { - NSURLSession *session = [NSURLSession sharedSession]; + NSURLSession *session = [OIDURLSessionProvider session]; NSURLSessionDataTask *task = [session dataTaskWithURL:discoveryURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { @@ -261,7 +262,7 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL + (void)performTokenRequest:(OIDTokenRequest *)request callback:(OIDTokenCallback)callback { NSURLRequest *URLRequest = [request URLRequest]; - NSURLSession *session = [NSURLSession sharedSession]; + NSURLSession *session = [OIDURLSessionProvider session]; [[session dataTaskWithRequest:URLRequest completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, @@ -372,7 +373,7 @@ + (void)performRegistrationRequest:(OIDRegistrationRequest *)request return; } - NSURLSession *session = [NSURLSession sharedSession]; + NSURLSession *session = [OIDURLSessionProvider session]; [[session dataTaskWithRequest:URLRequest completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, diff --git a/Source/OIDURLSessionProvider.h b/Source/OIDURLSessionProvider.h new file mode 100644 index 000000000..814c669f9 --- /dev/null +++ b/Source/OIDURLSessionProvider.h @@ -0,0 +1,40 @@ +/*! @file OIDURLSessionProvider.h + @brief AppAuth iOS SDK + @copyright + Copyright 2015 Google Inc. All Rights Reserved. + @copydetails + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import + +NS_ASSUME_NONNULL_BEGIN + +/*! @brief A NSURLSession provider that allows clients to provide custom implementation + for NSURLSession + */ +@interface OIDURLSessionProvider : NSObject + +/*! @brief Obtains the current @c NSURLSession; using the +[NSURLSession sharedSession] if + no custom implementation provided + @return NSURLSession object to be used for making network requests + */ ++ (NSURLSession *)session; + +/*! @brief Allows library consumers to change the @c NSURLSession used to create make + network requests + @param session The @c NSURLSession instance that should be used for making network requests + */ ++ (void)setSession:(NSURLSession *)session; +@end +NS_ASSUME_NONNULL_END diff --git a/Source/OIDURLSessionProvider.m b/Source/OIDURLSessionProvider.m new file mode 100644 index 000000000..f2e569b10 --- /dev/null +++ b/Source/OIDURLSessionProvider.m @@ -0,0 +1,39 @@ +/*! @file OIDURLSessionProvider.m + @brief AppAuth iOS SDK + @copyright + Copyright 2015 Google Inc. All Rights Reserved. + @copydetails + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import "OIDURLSessionProvider.h" + +NS_ASSUME_NONNULL_BEGIN + +static NSURLSession * __nullable gURLSession; + +@implementation OIDURLSessionProvider + ++ (NSURLSession *)session { + if (!gURLSession) { + gURLSession = [NSURLSession sharedSession]; + } + return gURLSession; +} + ++ (void)setSession:(NSURLSession *)session { + NSAssert(session, @"Parameter: |session| must be non-nil."); + gURLSession = session; +} +@end +NS_ASSUME_NONNULL_END diff --git a/UnitTests/OIDURLSessionProviderTests.m b/UnitTests/OIDURLSessionProviderTests.m new file mode 100644 index 000000000..d3d9eeb46 --- /dev/null +++ b/UnitTests/OIDURLSessionProviderTests.m @@ -0,0 +1,43 @@ +/*! @file OIDURLSessionProviderTests.m + @brief AppAuth iOS SDK + @copyright + Copyright 2015 Google Inc. All Rights Reserved. + @copydetails + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import +#import "OIDURLSessionProvider.h" + +@interface OIDURLSessionProviderTests : XCTestCase + +@end + +/*! @brief Unit tests for @c OIDURLSessionProvider + */ +@implementation OIDURLSessionProviderTests + +- (void)tearDown { + // Setting the session back to default sharedSession for future test cases + [OIDURLSessionProvider setSession:[NSURLSession sharedSession]]; +} + +- (void)testCustomSession { + NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration]; + NSURLSession *customSession = [NSURLSession sessionWithConfiguration:config]; + [OIDURLSessionProvider setSession:customSession]; + NSURLSession *session = [OIDURLSessionProvider session]; + XCTAssertEqual(session, customSession); +} + +@end