-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HttpFetcher component #328
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 <Foundation/Foundation.h> | ||
|
||
@protocol GTMSessionFetcherServiceProtocol; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@protocol GIDHTTPFetcher <NSObject> | ||
|
||
/// Fetches the data from an URL request. | ||
/// | ||
/// @param urlRequest The url request to fetch data. | ||
/// @param fetcherService The object to add authorization to the request. | ||
/// @param comment The comment for logging purpose. | ||
/// @param completion The block that is called on completion asynchronously. | ||
- (void)fetchURLRequest:(NSURLRequest *)urlRequest | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
withFetcherService:(id<GTMSessionFetcherServiceProtocol>)fetcherService | ||
#pragma clang diagnostic pop | ||
withComment:(NSString *)comment | ||
completion:(void (^)(NSData *_Nullable, NSError *_Nullable))completion; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 <Foundation/Foundation.h> | ||
|
||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/// The block which provides the response for the method | ||
/// fetchURLRequest:withAuthorizer:withComment:completion:`. | ||
/// | ||
/// @param data The NSData returned if succeed, | ||
/// @param error The error returned if failed. | ||
typedef void(^GIDHTTPFetcherFakeResponseProviderBlock)(NSData *_Nullable data, | ||
NSError *_Nullable error); | ||
|
||
/// The block to set up data based on the input request for the method | ||
/// fetchURLRequest:withAuthorizer:withComment:completion:`. | ||
/// | ||
/// @param request The request from input. | ||
/// @param responseProvider The block which provides the response. | ||
typedef void (^GIDHTTPFetcherTestBlock)(NSURLRequest *request, | ||
GIDHTTPFetcherFakeResponseProviderBlock responseProvider); | ||
|
||
@interface GIDFakeHTTPFetcher : NSObject <GIDHTTPFetcher> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming |
||
|
||
/// Set the test block which provides the response value. | ||
- (void)setTestBlock:(GIDHTTPFetcherTestBlock)block; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be passed into the initializer instead? |
||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/Fakes/GIDFakeHTTPFetcher.h" | ||
|
||
@interface GIDFakeHTTPFetcher () | ||
|
||
@property(nonatomic) GIDHTTPFetcherTestBlock testBlock; | ||
|
||
@end | ||
|
||
@implementation GIDFakeHTTPFetcher | ||
|
||
- (void)fetchURLRequest:(NSURLRequest *)urlRequest | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
withFetcherService:(id<GTMSessionFetcherServiceProtocol>)fetcherService | ||
#pragma clang diagnostic pop | ||
withComment:(NSString *)comment | ||
completion:(void (^)(NSData *_Nullable, NSError *_Nullable))completion { | ||
NSAssert(self.testBlock != nil, @"Set the test block before invoking this method."); | ||
self.testBlock(urlRequest, ^(NSData *_Nullable data, NSError *_Nullable error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you do anything with |
||
completion(data, error); | ||
}); | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 <Foundation/Foundation.h> | ||
|
||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface GIDHTTPFetcher : NSObject<GIDHTTPFetcher> | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h" | ||
|
||
@import GTMAppAuth; | ||
|
||
#import <GTMSessionFetcher/GTMSessionFetcher.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
// Maximum retry interval in seconds for the fetcher. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this a doc comment with |
||
static const NSTimeInterval kFetcherMaxRetryInterval = 15.0; | ||
|
||
@implementation GIDHTTPFetcher | ||
|
||
- (void)fetchURLRequest:(NSURLRequest *)urlRequest | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
withFetcherService:(id<GTMSessionFetcherServiceProtocol>)fetcherService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align the colon on this line with the others. |
||
#pragma clang diagnostic pop | ||
withComment:(NSString *)comment | ||
completion:(void (^)(NSData *_Nullable, NSError *_Nullable))completion { | ||
GTMSessionFetcher *fetcher; | ||
if (fetcherService) { | ||
fetcher = [fetcherService fetcherWithRequest:urlRequest]; | ||
} else { | ||
fetcher = [GTMSessionFetcher fetcherWithRequest:urlRequest]; | ||
} | ||
fetcher.retryEnabled = YES; | ||
fetcher.maxRetryInterval = kFetcherMaxRetryInterval; | ||
fetcher.comment = comment; | ||
[fetcher beginFetchWithCompletionHandler:completion]; | ||
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h" | ||
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInResult.h" | ||
|
||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h" | ||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h" | ||
#import "GoogleSignIn/Sources/GIDEMMSupport.h" | ||
#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h" | ||
#import "GoogleSignIn/Sources/GIDSignInPreferences.h" | ||
|
@@ -118,9 +120,6 @@ | |
// User preference key to detect fresh install of the app. | ||
static NSString *const kAppHasRunBeforeKey = @"GID_AppHasRunBefore"; | ||
|
||
// Maximum retry interval in seconds for the fetcher. | ||
static const NSTimeInterval kFetcherMaxRetryInterval = 15.0; | ||
|
||
// The delay before the new sign-in flow can be presented after the existing one is cancelled. | ||
static const NSTimeInterval kPresentationDelayAfterCancel = 1.0; | ||
|
||
|
@@ -163,6 +162,8 @@ @implementation GIDSignIn { | |
OIDServiceConfiguration *_appAuthConfiguration; | ||
// AppAuth external user-agent session state. | ||
id<OIDExternalUserAgentSession> _currentAuthorizationFlow; | ||
// The class to fetches data from a url end point. | ||
id<GIDHTTPFetcher> _httpFetcher; | ||
// Flag to indicate that the auth flow is restarting. | ||
BOOL _restarting; | ||
// Keychain manager for GTMAppAuth | ||
|
@@ -421,10 +422,13 @@ - (void)disconnectWithCompletion:(nullable GIDDisconnectCompletion)completion { | |
kEnvironmentLoggingParameter, | ||
GIDEnvironment()]; | ||
NSURL *revokeURL = [NSURL URLWithString:revokeURLString]; | ||
[self startFetchURL:revokeURL | ||
fromAuthState:authState | ||
withComment:@"GIDSignIn: revoke tokens" | ||
withCompletionHandler:^(NSData *data, NSError *error) { | ||
NSMutableURLRequest *revokeRequest = [NSMutableURLRequest requestWithURL:revokeURL]; | ||
GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState]; | ||
id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService; | ||
[self->_httpFetcher fetchURLRequest:revokeRequest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to use the indirect member access operator ( |
||
withFetcherService:fetcherService | ||
withComment:@"GIDSignIn: revoke tokens" | ||
completion:^(NSData *data, NSError *error) { | ||
// Revoking an already revoked token seems always successful, which helps us here. | ||
if (!error) { | ||
[self signOut]; | ||
|
@@ -450,7 +454,8 @@ + (GIDSignIn *)sharedInstance { | |
|
||
#pragma mark - Private methods | ||
|
||
- (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore { | ||
- (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore | ||
httpFetcher:(id<GIDHTTPFetcher>)httpFetcher { | ||
self = [super init]; | ||
if (self) { | ||
// Get the bundle of the current executable. | ||
|
@@ -477,6 +482,7 @@ - (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore { | |
initWithAuthorizationEndpoint:[NSURL URLWithString:authorizationEnpointURL] | ||
tokenEndpoint:[NSURL URLWithString:tokenEndpointURL]]; | ||
_keychainStore = keychainStore; | ||
_httpFetcher = httpFetcher; | ||
|
||
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
// Perform migration of auth state from old (before 5.0) versions of the SDK if needed. | ||
|
@@ -494,7 +500,9 @@ - (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore { | |
- (instancetype)initPrivate { | ||
GTMKeychainStore *keychainStore = | ||
[[GTMKeychainStore alloc] initWithItemName:kGTMAppAuthKeychainName]; | ||
return [self initWithKeychainStore:keychainStore]; | ||
id<GIDHTTPFetcher> httpFetcher = [[GIDHTTPFetcher alloc] init]; | ||
return [self initWithKeychainStore:keychainStore | ||
httpFetcher:httpFetcher]; | ||
} | ||
|
||
// Does sanity check for parameters and then authenticates if necessary. | ||
|
@@ -823,10 +831,13 @@ - (void)addDecodeIdTokenCallback:(GIDAuthFlow *)authFlow { | |
[NSString stringWithFormat:kUserInfoURLTemplate, | ||
[GIDSignInPreferences googleUserInfoServer], | ||
authState.lastTokenResponse.accessToken]]; | ||
[self startFetchURL:infoURL | ||
fromAuthState:authState | ||
withComment:@"GIDSignIn: fetch basic profile info" | ||
withCompletionHandler:^(NSData *data, NSError *error) { | ||
NSMutableURLRequest *infoRequest = [NSMutableURLRequest requestWithURL:infoURL]; | ||
GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState]; | ||
id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService; | ||
[self->_httpFetcher fetchURLRequest:infoRequest | ||
henryhl22321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
withFetcherService:fetcherService | ||
withComment:@"GIDSignIn: fetch basic profile info" | ||
completion:^(NSData *data, NSError *error) { | ||
if (data && !error) { | ||
NSError *jsonDeserializationError; | ||
NSDictionary<NSString *, NSString *> *profileDict = | ||
|
@@ -876,24 +887,24 @@ - (void)addCompletionCallback:(GIDAuthFlow *)authFlow { | |
}]; | ||
} | ||
|
||
- (void)startFetchURL:(NSURL *)URL | ||
fromAuthState:(OIDAuthState *)authState | ||
withComment:(NSString *)comment | ||
withCompletionHandler:(void (^)(NSData *, NSError *))handler { | ||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; | ||
GTMSessionFetcher *fetcher; | ||
GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState]; | ||
id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService; | ||
if (fetcherService) { | ||
fetcher = [fetcherService fetcherWithRequest:request]; | ||
} else { | ||
fetcher = [GTMSessionFetcher fetcherWithRequest:request]; | ||
} | ||
fetcher.retryEnabled = YES; | ||
fetcher.maxRetryInterval = kFetcherMaxRetryInterval; | ||
fetcher.comment = comment; | ||
[fetcher beginFetchWithCompletionHandler:handler]; | ||
} | ||
//- (void)startFetchURL:(NSURL *)URL | ||
// fromAuthState:(OIDAuthState *)authState | ||
// withComment:(NSString *)comment | ||
// withCompletionHandler:(void (^)(NSData *, NSError *))handler { | ||
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; | ||
// GTMSessionFetcher *fetcher; | ||
// GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState]; | ||
// id<GTMSessionFetcherServiceProtocol> fetcherService = authorization.fetcherService; | ||
// if (fetcherService) { | ||
// fetcher = [fetcherService fetcherWithRequest:request]; | ||
// } else { | ||
// fetcher = [GTMSessionFetcher fetcherWithRequest:request]; | ||
// } | ||
// fetcher.retryEnabled = YES; | ||
// fetcher.maxRetryInterval = kFetcherMaxRetryInterval; | ||
// fetcher.comment = comment; | ||
// [fetcher beginFetchWithCompletionHandler:handler]; | ||
//} | ||
henryhl22321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Parse incoming URL from the Google Device Policy app. | ||
- (BOOL)handleDevicePolicyAppURL:(NSURL *)url { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent one space.