Skip to content

Commit 0ff9492

Browse files
committed
Add the property GIDConfiguration to GIDGoogleUser
- Add a GIDConfiguration to replace hostedDomain, serverClientID, openRealm. - Remove the property serverAuthCode.
1 parent 05339d6 commit 0ff9492

File tree

7 files changed

+59
-63
lines changed

7 files changed

+59
-63
lines changed

GoogleSignIn/Sources/GIDGoogleUser.m

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import "GoogleSignIn/Sources/GIDAuthentication_Private.h"
1818
#import "GoogleSignIn/Sources/GIDProfileData_Private.h"
19+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
20+
1921

2022
#ifdef SWIFT_PACKAGE
2123
@import AppAuth;
@@ -53,30 +55,6 @@ - (nullable NSString *)userID {
5355
return nil;
5456
}
5557

56-
- (nullable NSString *)hostedDomain {
57-
NSString *idToken = [self idToken];
58-
if (idToken) {
59-
OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
60-
if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
61-
return [idTokenDecoded.claims[kHostedDomainIDTokenClaimKey] copy];
62-
}
63-
}
64-
65-
return nil;
66-
}
67-
68-
- (nullable NSString *)serverAuthCode {
69-
return [_authState.lastTokenResponse.additionalParameters[@"server_code"] copy];
70-
}
71-
72-
- (nullable NSString *)serverClientID {
73-
return [_authState.lastTokenResponse.request.additionalParameters[kAudienceParameter] copy];
74-
}
75-
76-
- (nullable NSString *)openIDRealm {
77-
return [_authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter] copy];
78-
}
79-
8058
- (nullable NSArray<NSString *> *)grantedScopes {
8159
NSArray<NSString *> *grantedScopes;
8260
NSString *grantedScopeString = _authState.lastTokenResponse.scope;
@@ -111,14 +89,41 @@ - (void)updateAuthState:(OIDAuthState *)authState
11189
_authState = authState;
11290
_authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
11391
_profile = profileData;
92+
_configuration = [[GIDConfiguration alloc] initWithClientID:[self clientID]
93+
serverClientID:[self serverClientID]
94+
hostedDomain:[self hostedDomain]
95+
openIDRealm:[self openIDRealm]];
11496
}
11597

11698
#pragma mark - Helpers
11799

100+
- (NSString *)clientID {
101+
return _authState.lastAuthorizationResponse.request.clientID;
102+
}
103+
104+
- (nullable NSString *)hostedDomain {
105+
NSString *idToken = [self idToken];
106+
if (idToken) {
107+
OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
108+
if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
109+
return [idTokenDecoded.claims[kHostedDomainIDTokenClaimKey] copy];
110+
}
111+
}
112+
return nil;
113+
}
114+
118115
- (NSString *)idToken {
119116
return _authState ? _authState.lastTokenResponse.idToken : nil;
120117
}
121118

119+
- (nullable NSString *)serverClientID {
120+
return [_authState.lastTokenResponse.request.additionalParameters[kAudienceParameter] copy];
121+
}
122+
123+
- (nullable NSString *)openIDRealm {
124+
return [_authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter] copy];
125+
}
126+
122127
#pragma mark - NSSecureCoding
123128

124129
+ (BOOL)supportsSecureCoding {
@@ -137,6 +142,10 @@ - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
137142
_authState = authentication.authState;
138143
}
139144
_authentication = [[GIDAuthentication alloc] initWithAuthState:_authState];
145+
_configuration = [[GIDConfiguration alloc] initWithClientID:[self clientID]
146+
serverClientID:[self serverClientID]
147+
hostedDomain:[self hostedDomain]
148+
openIDRealm:[self openIDRealm]];
140149
}
141150
return self;
142151
}

GoogleSignIn/Sources/GIDSignIn.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
278278
return;
279279
}
280280

281-
GIDConfiguration *configuration =
282-
[[GIDConfiguration alloc] initWithClientID:self.currentUser.authentication.clientID
283-
serverClientID:self.currentUser.serverClientID
284-
hostedDomain:self.currentUser.hostedDomain
285-
openIDRealm:self.currentUser.openIDRealm];
281+
GIDConfiguration *configuration = self.currentUser.configuration;
286282
GIDSignInInternalOptions *options =
287283
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
288284
presentingViewController:presentingViewController
@@ -371,11 +367,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
371367
return;
372368
}
373369

374-
GIDConfiguration *configuration =
375-
[[GIDConfiguration alloc] initWithClientID:self.currentUser.authentication.clientID
376-
serverClientID:self.currentUser.serverClientID
377-
hostedDomain:self.currentUser.hostedDomain
378-
openIDRealm:self.currentUser.openIDRealm];
370+
GIDConfiguration *configuration = self.currentUser.configuration;
379371
GIDSignInInternalOptions *options =
380372
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
381373
presentingWindow:presentingWindow

GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
NS_ASSUME_NONNULL_BEGIN
2020

2121
@class GIDAuthentication;
22+
@class GIDConfiguration;
2223
@class GIDProfileData;
2324

2425
/// This class represents a user account.
@@ -36,18 +37,8 @@ NS_ASSUME_NONNULL_BEGIN
3637
/// The API scopes granted to the app in an array of `NSString`.
3738
@property(nonatomic, readonly, nullable) NSArray<NSString *> *grantedScopes;
3839

39-
/// For Google Apps hosted accounts, the domain of the user.
40-
@property(nonatomic, readonly, nullable) NSString *hostedDomain;
41-
42-
/// The client ID of the home server.
43-
@property(nonatomic, readonly, nullable) NSString *serverClientID;
44-
45-
/// An OAuth2 authorization code for the home server.
46-
@property(nonatomic, readonly, nullable) NSString *serverAuthCode;
47-
48-
/// The OpenID2 realm of the home server.
49-
@property(nonatomic, readonly, nullable) NSString *openIDRealm;
50-
40+
/// The client configuration.
41+
@property(nonatomic, readonly) GIDConfiguration *configuration;
5142

5243
@end
5344

GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#import "GoogleSignIn/Tests/Unit/GIDGoogleUser+Testing.h"
1616

17+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
1718
#import "GoogleSignIn/Tests/Unit/GIDAuthentication+Testing.h"
1819
#import "GoogleSignIn/Tests/Unit/GIDProfileData+Testing.h"
1920

@@ -32,15 +33,14 @@ - (BOOL)isEqual:(id)object {
3233
- (BOOL)isEqualToGoogleUser:(GIDGoogleUser *)other {
3334
return [self.authentication isEqual:other.authentication] &&
3435
[self.userID isEqual:other.userID] &&
35-
[self.serverAuthCode isEqual:other.serverAuthCode] &&
3636
[self.profile isEqual:other.profile] &&
37-
[self.hostedDomain isEqual:other.hostedDomain];
37+
[self.configuration isEqual:other.configuration];
3838
}
3939

4040
// Not the hash implemention you want to use on prod, but just to match |isEqual:| here.
4141
- (NSUInteger)hash {
42-
return [self.authentication hash] ^ [self.userID hash] ^ [self.serverAuthCode hash] ^
43-
[self.profile hash] ^ [self.hostedDomain hash];
42+
return [self.authentication hash] ^ [self.userID hash] ^ [self.configuration hash] ^
43+
[self.profile hash] ;
4444
}
4545

4646
@end

GoogleSignIn/Tests/Unit/GIDGoogleUserTest.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <XCTest/XCTest.h>
1818

1919
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDAuthentication.h"
20+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
2021
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
2122

2223
#import "GoogleSignIn/Sources/GIDAuthentication_Private.h"
@@ -49,8 +50,8 @@ - (void)testInitWithAuthState {
4950
XCTAssertEqualObjects(user.authentication, authentication);
5051
XCTAssertEqualObjects(user.grantedScopes, @[ OIDAuthorizationRequestTestingScope2 ]);
5152
XCTAssertEqualObjects(user.userID, kUserID);
52-
XCTAssertEqualObjects(user.hostedDomain, kHostedDomain);
53-
XCTAssertEqualObjects(user.serverAuthCode, kServerAuthCode);
53+
XCTAssertEqualObjects(user.configuration.hostedDomain, kHostedDomain);
54+
XCTAssertEqualObjects(user.configuration.clientID, OIDAuthorizationRequestTestingClientID);
5455
XCTAssertEqualObjects(user.profile, [GIDProfileData testInstance]);
5556
}
5657

GoogleSignIn/Tests/Unit/GIDSignInTest.m

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,21 @@ - (void)testShareInstance {
397397
- (void)testRestorePreviousSignInNoRefresh_hasPreviousUser {
398398
[[[_authorization expect] andReturn:_authState] authState];
399399
OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
400-
OCMStub([_tokenResponse scope]).andReturn(nil);
401-
OCMStub([_tokenResponse additionalParameters]).andReturn(nil);
402-
OCMStub([_tokenResponse idToken]).andReturn(kFakeIDToken);
403-
OCMStub([_tokenResponse request]).andReturn(_tokenRequest);
404-
OCMStub([_tokenRequest additionalParameters]).andReturn(nil);
405400

406401
id idTokenDecoded = OCMClassMock([OIDIDToken class]);
407402
OCMStub([idTokenDecoded alloc]).andReturn(idTokenDecoded);
408403
OCMStub([idTokenDecoded initWithIDTokenString:OCMOCK_ANY]).andReturn(idTokenDecoded);
409404
OCMStub([idTokenDecoded subject]).andReturn(kFakeGaiaID);
405+
406+
// Mock generating a GIDConfiguration when initializing GIDGoogleUser.
407+
OIDAuthorizationResponse *authResponse =
408+
[OIDAuthorizationResponse testInstanceWithAdditionalParameters:nil
409+
errorString:nil];
410+
411+
OCMStub([_authState lastAuthorizationResponse]).andReturn(authResponse);
412+
OCMStub([_tokenResponse idToken]).andReturn(kFakeIDToken);
413+
OCMStub([_tokenResponse request]).andReturn(_tokenRequest);
414+
OCMStub([_tokenRequest additionalParameters]).andReturn(nil);
410415

411416
[_signIn restorePreviousSignInNoRefresh];
412417

@@ -569,11 +574,9 @@ - (void)testAddScopes {
569574
OCMStub([profile email]).andReturn(kUserEmail);
570575

571576
OCMStub([_user authentication]).andReturn(_authentication);
572-
OCMStub([_authentication clientID]).andReturn(kClientId);
573-
OCMStub([_user serverClientID]).andReturn(nil);
574-
OCMStub([_user hostedDomain]).andReturn(nil);
575-
576-
OCMStub([_user openIDRealm]).andReturn(kOpenIDRealm);
577+
578+
// Mock for the method `addScopes`.
579+
OCMStub([_user configuration]).andReturn(_configuration);
577580
OCMStub([_user profile]).andReturn(profile);
578581
OCMStub([_user grantedScopes]).andReturn(@[kGrantedScope]);
579582

GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat {
112112
NSMutableDictionary<NSString *, NSString *> *payloadContents =
113113
[NSMutableDictionary dictionaryWithDictionary:@{
114114
@"sub" : sub,
115-
@"hd" : kHostedDomain,
115+
@"hd" : kHostedDomain,
116116
@"iss" : kIssuer,
117117
@"aud" : kAudience,
118118
@"exp" : exp,

0 commit comments

Comments
 (0)