|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h" |
| 18 | + |
| 19 | +#import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h" |
| 20 | +#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h" |
| 21 | +#import "GoogleSignIn/Sources/GIDProfileData_Private.h" |
| 22 | +#import "GoogleSignIn/Sources/GIDSignInPreferences.h" |
| 23 | + |
| 24 | +#ifdef SWIFT_PACKAGE |
| 25 | +@import AppAuth; |
| 26 | +@import GTMAppAuth; |
| 27 | +#else |
| 28 | +#import <AppAuth/AppAuth.h> |
| 29 | +#import <GTMAppAuth/GTMAppAuth.h> |
| 30 | +#endif |
| 31 | + |
| 32 | +NS_ASSUME_NONNULL_BEGIN |
| 33 | + |
| 34 | +// The URL template for the URL to get user info. |
| 35 | +static NSString *const kUserInfoURLTemplate = @"https://%@/oauth2/v3/userinfo"; |
| 36 | + |
| 37 | +// Basic profile (Fat ID Token / userinfo endpoint) keys |
| 38 | +static NSString *const kBasicProfileEmailKey = @"email"; |
| 39 | +static NSString *const kBasicProfilePictureKey = @"picture"; |
| 40 | +static NSString *const kBasicProfileNameKey = @"name"; |
| 41 | +static NSString *const kBasicProfileGivenNameKey = @"given_name"; |
| 42 | +static NSString *const kBasicProfileFamilyNameKey = @"family_name"; |
| 43 | + |
| 44 | +@implementation GIDProfileDataFetcher { |
| 45 | + id<GIDHTTPFetcher> _httpFetcher; |
| 46 | +} |
| 47 | + |
| 48 | +- (instancetype)init { |
| 49 | + GIDHTTPFetcher *httpFetcher = [[GIDHTTPFetcher alloc] init]; |
| 50 | + return [self initWithHTTPFetcher:httpFetcher]; |
| 51 | +} |
| 52 | + |
| 53 | +- (instancetype)initWithHTTPFetcher:(id<GIDHTTPFetcher>)httpFetcher { |
| 54 | + self = [super init]; |
| 55 | + if (self) { |
| 56 | + _httpFetcher = httpFetcher; |
| 57 | + } |
| 58 | + return self; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState |
| 62 | + completion:(void (^)(GIDProfileData *_Nullable profileData, |
| 63 | + NSError *_Nullable error))completion { |
| 64 | + OIDIDToken *idToken = |
| 65 | + [[OIDIDToken alloc] initWithIDTokenString:authState.lastTokenResponse.idToken]; |
| 66 | + if (idToken) { |
| 67 | + // If we have an ID token, try to extract profile data from it. |
| 68 | + GIDProfileData *profileData = [self fetchProfileDataWithIDToken:idToken]; |
| 69 | + if (profileData) { |
| 70 | + completion(profileData, nil); |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // If we can't retrieve profile data from the ID token, make a UserInfo endpoint request to |
| 76 | + // fetch it. |
| 77 | + NSString *infoString = [NSString stringWithFormat:kUserInfoURLTemplate, |
| 78 | + [GIDSignInPreferences googleUserInfoServer]]; |
| 79 | + NSURL *infoURL = [NSURL URLWithString:infoString]; |
| 80 | + NSMutableURLRequest *infoRequest = [NSMutableURLRequest requestWithURL:infoURL]; |
| 81 | + GTMAppAuthFetcherAuthorization *authorization = |
| 82 | + [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState]; |
| 83 | + |
| 84 | + [_httpFetcher fetchURLRequest:infoRequest |
| 85 | + withAuthorizer:authorization |
| 86 | + withComment:@"GIDSignIn: fetch basic profile info" |
| 87 | + completion:^(NSData *data, NSError *error) { |
| 88 | + if (error) { |
| 89 | + completion(nil, error); |
| 90 | + return; |
| 91 | + } |
| 92 | + NSError *jsonDeserializationError; |
| 93 | + NSDictionary<NSString *, NSString *> *profileDict = |
| 94 | + [NSJSONSerialization JSONObjectWithData:data |
| 95 | + options:NSJSONReadingMutableContainers |
| 96 | + error:&jsonDeserializationError]; |
| 97 | + if (jsonDeserializationError) { |
| 98 | + completion(nil, jsonDeserializationError); |
| 99 | + return; |
| 100 | + } |
| 101 | + GIDProfileData *profileData = [[GIDProfileData alloc] |
| 102 | + initWithEmail:idToken.claims[kBasicProfileEmailKey] |
| 103 | + name:profileDict[kBasicProfileNameKey] |
| 104 | + givenName:profileDict[kBasicProfileGivenNameKey] |
| 105 | + familyName:profileDict[kBasicProfileFamilyNameKey] |
| 106 | + imageURL:[NSURL URLWithString:profileDict[kBasicProfilePictureKey]]]; |
| 107 | + completion(profileData, nil); |
| 108 | + }]; |
| 109 | +} |
| 110 | + |
| 111 | +- (nullable GIDProfileData*)fetchProfileDataWithIDToken:(OIDIDToken *)idToken { |
| 112 | + if (!idToken || |
| 113 | + !idToken.claims[kBasicProfilePictureKey] || |
| 114 | + !idToken.claims[kBasicProfileNameKey] || |
| 115 | + !idToken.claims[kBasicProfileGivenNameKey] || |
| 116 | + !idToken.claims[kBasicProfileFamilyNameKey]) { |
| 117 | + return nil; |
| 118 | + } |
| 119 | + |
| 120 | + return [[GIDProfileData alloc] |
| 121 | + initWithEmail:idToken.claims[kBasicProfileEmailKey] |
| 122 | + name:idToken.claims[kBasicProfileNameKey] |
| 123 | + givenName:idToken.claims[kBasicProfileGivenNameKey] |
| 124 | + familyName:idToken.claims[kBasicProfileFamilyNameKey] |
| 125 | + imageURL:[NSURL URLWithString:idToken.claims[kBasicProfilePictureKey]]]; |
| 126 | +} |
| 127 | + |
| 128 | +@end |
| 129 | + |
| 130 | +NS_ASSUME_NONNULL_END |
0 commit comments