Skip to content

Commit c3a08ca

Browse files
committed
Added a unit test for testing idToken with claims. Also updated OIDTokenResponse + Testing
1 parent 5a409da commit c3a08ca

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

GoogleSignIn/Tests/Unit/GIDSignInTest.m

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,32 @@ - (void)testAddScopes {
852852
[profile stopMocking];
853853
}
854854

855+
- (void)testOAuthLogin_TokenClaims_Passes {
856+
GIDTokenClaim *authTimeClaim = [GIDTokenClaim authTimeClaim];
857+
OCMStub([_keychainStore saveAuthSession:OCMOCK_ANY error:OCMArg.anyObjectRef]
858+
).andDo(^(NSInvocation *invocation){
859+
self->_keychainSaved = self->_saveAuthorizationReturnValue;
860+
});
861+
862+
[self OAuthLoginWithAddScopesFlow:NO
863+
authError:nil
864+
tokenError:nil
865+
emmPasscodeInfoRequired:NO
866+
keychainError:NO
867+
tokenClaimsError:NO
868+
restoredSignIn:NO
869+
oldAccessToken:NO
870+
modalCancel:NO
871+
useAdditionalScopes:NO
872+
additionalScopes:nil
873+
manualNonce:nil
874+
tokenClaims:[NSSet setWithObject:authTimeClaim]];
875+
876+
OCMVerifyAll(_user);
877+
OCMVerifyAll(_authState);
878+
}
879+
880+
855881
- (void)testOpenIDRealm {
856882
_signIn.configuration = [[GIDConfiguration alloc] initWithClientID:kClientId
857883
serverClientID:nil
@@ -1553,12 +1579,20 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
15531579
nonce:nonce
15541580
errorString:authError];
15551581

1556-
OIDTokenResponse *tokenResponse =
1557-
[OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken]
1558-
accessToken:restoredSignIn ? kAccessToken : nil
1559-
expiresIn:oldAccessToken ? @(300) : nil
1560-
refreshToken:kRefreshToken
1561-
tokenRequest:nil];
1582+
OIDTokenResponse *tokenResponse;
1583+
if (tokenClaims) {
1584+
tokenResponse = [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse iDTokenWithAuthTime]
1585+
accessToken:restoredSignIn ? kAccessToken : nil
1586+
expiresIn:oldAccessToken ? @(300) : nil
1587+
refreshToken:kRefreshToken
1588+
tokenRequest:nil];
1589+
} else {
1590+
tokenResponse = [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken]
1591+
accessToken:restoredSignIn ? kAccessToken : nil
1592+
expiresIn:oldAccessToken ? @(300) : nil
1593+
refreshToken:kRefreshToken
1594+
tokenRequest:nil];
1595+
}
15621596

15631597
OIDTokenRequest *tokenRequest = [[OIDTokenRequest alloc]
15641598
initWithConfiguration:authResponse.request.configuration
@@ -1749,6 +1783,12 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
17491783
} else {
17501784
// Simulate token endpoint response.
17511785
_savedTokenCallback(tokenResponse, nil);
1786+
if (tokenClaims) {
1787+
XCTAssertEqualObjects(tokenResponse.idToken,
1788+
[OIDTokenResponse iDTokenWithAuthTime],
1789+
@"ID Token string should contain authTime");
1790+
}
1791+
17521792
}
17531793

17541794
if (keychainError) {

GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern NSString *const kUserID;
3333
extern NSString *const kHostedDomain;
3434
extern NSString *const kIssuer;
3535
extern NSString *const kAudience;
36+
extern NSString *const kAuthTime;
3637
extern NSTimeInterval const kIDTokenExpires;
3738
extern NSTimeInterval const kIssuedAt;
3839

@@ -59,10 +60,19 @@ extern NSString * const kFatPictureURL;
5960
refreshToken:(NSString *)refreshToken
6061
tokenRequest:(OIDTokenRequest *)tokenRequest;
6162

63+
+ (instancetype)testInstanceWithIDToken:(NSString *)idToken
64+
accessToken:(NSString *)accessToken
65+
expiresIn:(NSNumber *)expiresIn
66+
refreshToken:(NSString *)refreshToken
67+
authTime:(NSString *)authTime
68+
tokenRequest:(OIDTokenRequest *)tokenRequest;
69+
6270
+ (NSString *)idToken;
6371

6472
+ (NSString *)fatIDToken;
6573

74+
+ (NSString *)iDTokenWithAuthTime;
75+
6676
/**
6777
* @sub The subject of the ID token.
6878
* @exp The interval between 00:00:00 UTC on 1 January 1970 and the expiration date of the ID token.
@@ -71,4 +81,6 @@ extern NSString * const kFatPictureURL;
7181

7282
+ (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat;
7383

84+
+ (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat authTime:(NSString *)authTime;
85+
7486
@end

GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.m

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
NSString *const kHostedDomain = @"fakehosteddomain.com";
3939
NSString *const kIssuer = @"https://test.com";
4040
NSString *const kAudience = @"audience";
41+
NSString *const kAuthTime = @"123333";
4142
NSTimeInterval const kIDTokenExpires = 1000;
4243
NSTimeInterval const kIssuedAt = 0;
4344

@@ -70,6 +71,21 @@ + (instancetype)testInstanceWithIDToken:(NSString *)idToken
7071
expiresIn:(NSNumber *)expiresIn
7172
refreshToken:(NSString *)refreshToken
7273
tokenRequest:(OIDTokenRequest *)tokenRequest {
74+
return [OIDTokenResponse testInstanceWithIDToken:idToken
75+
accessToken:accessToken
76+
expiresIn:expiresIn
77+
refreshToken:refreshToken
78+
authTime:nil
79+
tokenRequest:tokenRequest];
80+
}
81+
82+
+ (instancetype)testInstanceWithIDToken:(NSString *)idToken
83+
accessToken:(NSString *)accessToken
84+
expiresIn:(NSNumber *)expiresIn
85+
refreshToken:(NSString *)refreshToken
86+
authTime:(NSString *)authTime
87+
tokenRequest:(OIDTokenRequest *)tokenRequest {
88+
7389
NSMutableDictionary<NSString *, NSString *> *parameters = [[NSMutableDictionary alloc] initWithDictionary:@{
7490
@"access_token" : accessToken ?: kAccessToken,
7591
@"expires_in" : expiresIn ?: @(kAccessTokenExpiresIn),
@@ -93,11 +109,24 @@ + (NSString *)fatIDToken {
93109
return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:YES];
94110
}
95111

112+
+ (NSString *)iDTokenWithAuthTime {
113+
return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:YES authTime:kAuthTime];
114+
}
115+
96116
+ (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp {
97117
return [self idTokenWithSub:sub exp:exp fat:NO];
98118
}
99119

100-
+ (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat {
120+
+ (NSString *)idTokenWithSub:(NSString *)sub
121+
exp:(NSNumber *)exp
122+
fat:(BOOL)fat {
123+
return [self idTokenWithSub:kUserID exp:exp fat:fat authTime:nil];
124+
}
125+
126+
+ (NSString *)idTokenWithSub:(NSString *)sub
127+
exp:(NSNumber *)exp
128+
fat:(BOOL)fat
129+
authTime:(NSString *)authTime{
101130
NSError *error;
102131
NSDictionary *headerContents = @{
103132
@"alg" : kAlg,
@@ -110,7 +139,7 @@ + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat {
110139
if (error || !headerJson) {
111140
return nil;
112141
}
113-
NSMutableDictionary<NSString *, NSString *> *payloadContents =
142+
NSMutableDictionary<NSString *, id> *payloadContents =
114143
[NSMutableDictionary dictionaryWithDictionary:@{
115144
@"sub" : sub,
116145
@"hd" : kHostedDomain,
@@ -127,6 +156,11 @@ + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat {
127156
kFatPictureURLKey : kFatPictureURL,
128157
}];
129158
}
159+
if (authTime) {
160+
[payloadContents addEntriesFromDictionary:@{
161+
@"auth_time": kAuthTime,
162+
}];
163+
}
130164
NSData *payloadJson = [NSJSONSerialization dataWithJSONObject:payloadContents
131165
options:NSJSONWritingPrettyPrinted
132166
error:&error];

0 commit comments

Comments
 (0)