-
Notifications
You must be signed in to change notification settings - Fork 246
Add GIDProfileDataFetcher #278
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4c2172b
Add GIDProfileDataFetcher API, implementation and test.
Alex-4-Git 4d13ed2
Style improvement
Alex-4-Git 9996646
FIx the NS_DESIGNATED_INITIALIZER warning.
Alex-4-Git cb17e5f
Use better name and fix style.
Alex-4-Git 8566e79
Improve wording.
Alex-4-Git 4ec4c44
Improve documentation.
Alex-4-Git 4920dfc
Merge branch 'refactorGSI' into pin-GIDProfileDataFetcher-new
Alex-4-Git 4ac5a72
Remove unused const.
Alex-4-Git 3c9e883
Move the new GIDProfileData initializer to GIDProfileDataFetcher
Alex-4-Git 6b04566
Improve style and documentation.
Alex-4-Git 20719d5
Add more unit tests
Alex-4-Git File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
GoogleSignIn/Sources/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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> | ||
|
|
||
| @class OIDAuthState; | ||
| @class OIDIDToken; | ||
| @class GIDProfileData; | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| @protocol GIDProfileDataFetcher <NSObject> | ||
|
|
||
| /// Fetches the latest `GIDProfileData` object. | ||
| /// | ||
| /// This method either extracts profile data from `OIDIDToken` in `OIDAuthState` or fetches it | ||
| /// from the UserInfo endpoint. | ||
| /// | ||
| /// @param authState The state of the current OAuth session. | ||
| /// @param completion The block that is called on completion asynchronously. | ||
| - (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState | ||
| completion:(void (^)(GIDProfileData *_Nullable profileData, | ||
| NSError *_Nullable error))completion; | ||
|
|
||
| /// Fetches a `GIDProfileData` object from an ID token. | ||
| /// | ||
| /// This method returns a `GIDProfileData` object if the ID token is a fat one. Otherwise, returns | ||
| /// nil. | ||
| /// | ||
| /// @param idToken The ID token. | ||
| - (nullable GIDProfileData *)fetchProfileDataWithIDToken:(OIDIDToken *)idToken; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
35 changes: 35 additions & 0 deletions
35
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * 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/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h" | ||
|
|
||
| @protocol GIDHTTPFetcher; | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| @interface GIDProfileDataFetcher : NSObject <GIDProfileDataFetcher> | ||
|
|
||
| /// The convenience initializer. | ||
| - (instancetype)init; | ||
|
|
||
| /// The initializer for unit test. | ||
| - (instancetype)initWithHTTPFetcher:(id<GIDHTTPFetcher>)httpFetcher NS_DESIGNATED_INITIALIZER; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
130 changes: 130 additions & 0 deletions
130
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * 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 "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h" | ||
Alex-4-Git marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h" | ||
| #import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h" | ||
| #import "GoogleSignIn/Sources/GIDProfileData_Private.h" | ||
| #import "GoogleSignIn/Sources/GIDSignInPreferences.h" | ||
|
|
||
| #ifdef SWIFT_PACKAGE | ||
| @import AppAuth; | ||
| @import GTMAppAuth; | ||
| #else | ||
| #import <AppAuth/AppAuth.h> | ||
| #import <GTMAppAuth/GTMAppAuth.h> | ||
| #endif | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| // The URL template for the URL to get user info. | ||
| static NSString *const kUserInfoURLTemplate = @"https://%@/oauth2/v3/userinfo"; | ||
|
|
||
| // Basic profile (Fat ID Token / userinfo endpoint) keys | ||
| static NSString *const kBasicProfileEmailKey = @"email"; | ||
| static NSString *const kBasicProfilePictureKey = @"picture"; | ||
| static NSString *const kBasicProfileNameKey = @"name"; | ||
| static NSString *const kBasicProfileGivenNameKey = @"given_name"; | ||
| static NSString *const kBasicProfileFamilyNameKey = @"family_name"; | ||
|
|
||
| @implementation GIDProfileDataFetcher { | ||
| id<GIDHTTPFetcher> _httpFetcher; | ||
| } | ||
|
|
||
| - (instancetype)init { | ||
| GIDHTTPFetcher *httpFetcher = [[GIDHTTPFetcher alloc] init]; | ||
| return [self initWithHTTPFetcher:httpFetcher]; | ||
| } | ||
|
|
||
| - (instancetype)initWithHTTPFetcher:(id<GIDHTTPFetcher>)httpFetcher { | ||
| self = [super init]; | ||
| if (self) { | ||
| _httpFetcher = httpFetcher; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState | ||
| completion:(void (^)(GIDProfileData *_Nullable profileData, | ||
| NSError *_Nullable error))completion { | ||
| OIDIDToken *idToken = | ||
| [[OIDIDToken alloc] initWithIDTokenString:authState.lastTokenResponse.idToken]; | ||
| if (idToken) { | ||
| // If we have an ID token, try to extract profile data from it. | ||
| GIDProfileData *profileData = [self fetchProfileDataWithIDToken:idToken]; | ||
| if (profileData) { | ||
| completion(profileData, nil); | ||
| return; | ||
| } | ||
| } | ||
Alex-4-Git marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // If we can't retrieve profile data from the ID token, make a UserInfo endpoint request to | ||
| // fetch it. | ||
| NSString *infoString = [NSString stringWithFormat:kUserInfoURLTemplate, | ||
| [GIDSignInPreferences googleUserInfoServer]]; | ||
| NSURL *infoURL = [NSURL URLWithString:infoString]; | ||
| NSMutableURLRequest *infoRequest = [NSMutableURLRequest requestWithURL:infoURL]; | ||
| GTMAppAuthFetcherAuthorization *authorization = | ||
| [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState]; | ||
|
|
||
| [_httpFetcher fetchURLRequest:infoRequest | ||
| withAuthorizer:authorization | ||
| withComment:@"GIDSignIn: fetch basic profile info" | ||
| completion:^(NSData *data, NSError *error) { | ||
| if (error) { | ||
| completion(nil, error); | ||
| return; | ||
| } | ||
| NSError *jsonDeserializationError; | ||
| NSDictionary<NSString *, NSString *> *profileDict = | ||
| [NSJSONSerialization JSONObjectWithData:data | ||
| options:NSJSONReadingMutableContainers | ||
| error:&jsonDeserializationError]; | ||
Alex-4-Git marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (jsonDeserializationError) { | ||
| completion(nil, jsonDeserializationError); | ||
| return; | ||
| } | ||
| GIDProfileData *profileData = [[GIDProfileData alloc] | ||
| initWithEmail:idToken.claims[kBasicProfileEmailKey] | ||
| name:profileDict[kBasicProfileNameKey] | ||
| givenName:profileDict[kBasicProfileGivenNameKey] | ||
| familyName:profileDict[kBasicProfileFamilyNameKey] | ||
| imageURL:[NSURL URLWithString:profileDict[kBasicProfilePictureKey]]]; | ||
| completion(profileData, nil); | ||
| }]; | ||
| } | ||
|
|
||
| - (nullable GIDProfileData*)fetchProfileDataWithIDToken:(OIDIDToken *)idToken { | ||
| if (!idToken || | ||
| !idToken.claims[kBasicProfilePictureKey] || | ||
| !idToken.claims[kBasicProfileNameKey] || | ||
| !idToken.claims[kBasicProfileGivenNameKey] || | ||
| !idToken.claims[kBasicProfileFamilyNameKey]) { | ||
| return nil; | ||
| } | ||
|
|
||
| return [[GIDProfileData alloc] | ||
| initWithEmail:idToken.claims[kBasicProfileEmailKey] | ||
| name:idToken.claims[kBasicProfileNameKey] | ||
| givenName:idToken.claims[kBasicProfileGivenNameKey] | ||
| familyName:idToken.claims[kBasicProfileFamilyNameKey] | ||
| imageURL:[NSURL URLWithString:idToken.claims[kBasicProfilePictureKey]]]; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.