From e16b8aacf7ca252d30084869cf1e64a6a7750f3b Mon Sep 17 00:00:00 2001 From: Harsh Date: Sun, 29 Oct 2017 22:45:11 -0700 Subject: [PATCH] Making NSURLSession extensible --- .DS_Store | Bin 0 -> 6148 bytes AppAuth.xcodeproj/project.pbxproj | 10 ++++++ Examples/Example-iOS_ObjC/Podfile.lock | 16 +++++++++ Source/.DS_Store | Bin 0 -> 6148 bytes Source/AppAuth.h | 1 + Source/OIDAuthorizationService.m | 7 ++-- Source/OIDURLSessionProvider.h | 37 +++++++++++++++++++++ Source/OIDURLSessionProvider.m | 39 ++++++++++++++++++++++ UnitTests/OIDURLSessionProviderTests.m | 43 +++++++++++++++++++++++++ 9 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 Examples/Example-iOS_ObjC/Podfile.lock create mode 100644 Source/.DS_Store create mode 100644 Source/OIDURLSessionProvider.h create mode 100644 Source/OIDURLSessionProvider.m create mode 100644 UnitTests/OIDURLSessionProviderTests.m diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..87aca64c8e5623c9ee03e5172bdb35a2f5b240e3 GIT binary patch literal 6148 zcmeHKOHRWu5Pfb7s>G)3SajhNAoT`m6;6-~Ad=DwHF5-v#G;FwviINyys-zV5m{6$ z5TY}Y{l?=Ne_j+j1Hi4?Y5~juOj!h-C<7wq!LE@yUlCm#qs1Ou+~XbPz(9Y|Bx}FL z5)GdvPS+n$VNL5R0$z5TMNx*jiCY+b%+HIeL!MXdo?IN~n?sB}W3D<>KZt6!Co3F# znd}+`*WPjmCDzZ?Tlo~ijxWl@7%&Em0b}5@8PKa>dUM${*zS!1V_<**IUhWWV5Zn5 zj7JBnm;w+dG)JK>xrD@c#Z0kF$R3JIp~MvGG{tZ!oOZ8%nPQhPg~Ms`;dJJ&A)P^kBQ0@#pqTj3V|JPP89^NQhO&z%MZH3AI)}tpET3 literal 0 HcmV?d00001 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/Examples/Example-iOS_ObjC/Podfile.lock b/Examples/Example-iOS_ObjC/Podfile.lock new file mode 100644 index 000000000..fb06a02fb --- /dev/null +++ b/Examples/Example-iOS_ObjC/Podfile.lock @@ -0,0 +1,16 @@ +PODS: + - AppAuth (0.90.0) + +DEPENDENCIES: + - AppAuth (from `../../`) + +EXTERNAL SOURCES: + AppAuth: + :path: ../../ + +SPEC CHECKSUMS: + AppAuth: 7de40c8b357ff84ad359c405acbf0be89a6b4c79 + +PODFILE CHECKSUM: abe079692b9a7fd43182b3039b95489b56d257e1 + +COCOAPODS: 1.3.1 diff --git a/Source/.DS_Store b/Source/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a4b0f8060048a5fbc54756f1ec5c91b02ebb51c2 GIT binary patch literal 6148 zcmeH~I|@QU3`G+^AlO)1PU8aH!5~^&xB-7yDHdWsOZVo3;L}<}ZXmfcnM8S~c=IA6 zy0{-Fqay{xqwIDX^xJdY_PKmsH{0wh2J zKSaRpZP;iW%18nvKmtz!_I@aE)0)~s{ndfsBLH-Ov>VnwOF)Yypf$CHA_LQCg+{CT z7-D&EhnB3XsVy|xMRWMj{AaZ(2By(2TCjm>bzvX@66g@<$9`h>{|f$T{_nJKO9CYD zX9RSx+$9?$aI%-Xuap + +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..976116ffa --- /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