OIDTokenResponse+Testing.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
  15. #import "GoogleSignIn/Tests/Unit/OIDAuthorizationRequest+Testing.h"
  16. #import "GoogleSignIn/Tests/Unit/OIDTokenRequest+Testing.h"
  17. #ifdef SWIFT_PACKAGE
  18. @import AppAuth;
  19. #else
  20. #import <AppAuth/OIDScopeUtilities.h>
  21. #import <AppAuth/OIDTokenRequest.h>
  22. #import <AppAuth/OIDTokenResponse.h>
  23. #endif
  24. NSString *const kAccessToken = @"access_token";
  25. NSTimeInterval const kAccessTokenExpiresIn = 3600;
  26. NSString *const kRefreshToken = @"refresh_token";
  27. NSString *const kServerAuthCode = @"server_auth_code";
  28. // ID token constants
  29. NSString *const kAlg = @"RS256";
  30. NSString *const kKid = @"alkjdfas";
  31. NSString *const kTyp = @"JWT";
  32. NSString *const kUserID = @"12345679";
  33. NSString *const kHostedDomain = @"fakehosteddomain.com";
  34. NSString *const kIssuer = @"https://test.com";
  35. NSString *const kAudience = @"audience";
  36. NSTimeInterval const kIDTokenExpires = 1000;
  37. NSTimeInterval const kIssuedAt = 0;
  38. NSString *const kFatNameKey = @"name";
  39. NSString *const kFatGivenNameKey = @"given_name";
  40. NSString *const kFatFamilyNameKey = @"family_name";
  41. NSString *const kFatPictureURLKey = @"picture";
  42. NSString * const kFatName = @"fake username";
  43. NSString * const kFatGivenName = @"fake";
  44. NSString * const kFatFamilyName = @"username";
  45. NSString * const kFatPictureURL = @"fake_user_picture_url";
  46. @implementation OIDTokenResponse (Testing)
  47. + (instancetype)testInstance {
  48. return [self testInstanceWithIDToken:[self idToken]];
  49. }
  50. + (instancetype)testInstanceWithScope:(NSString *)scope {
  51. NSMutableDictionary<NSString *, NSString *> *parameters;
  52. parameters = [[NSMutableDictionary alloc] initWithDictionary:@{
  53. @"access_token" : kAccessToken,
  54. @"expires_in" : @(kAccessTokenExpiresIn),
  55. @"token_type" : @"example_token_type",
  56. @"refresh_token" : kRefreshToken,
  57. @"scope" : scope,
  58. @"server_code" : kServerAuthCode,
  59. }];
  60. parameters[@"id_token"] = [self idToken];
  61. return [[OIDTokenResponse alloc] initWithRequest:[OIDTokenRequest testInstance]
  62. parameters:parameters];
  63. }
  64. + (instancetype)testInstanceWithIDToken:(NSString *)idToken {
  65. return [OIDTokenResponse testInstanceWithIDToken:idToken
  66. accessToken:nil
  67. expiresIn:nil
  68. refreshToken:nil
  69. tokenRequest:nil];
  70. }
  71. + (instancetype)testInstanceWithAccessTokenExpiration:(NSNumber *)expiration {
  72. return [OIDTokenResponse testInstanceWithIDToken:[self idToken]
  73. accessToken:nil
  74. expiresIn:expiration
  75. refreshToken:nil
  76. tokenRequest:nil];
  77. }
  78. + (instancetype)testInstanceWithIDToken:(NSString *)idToken
  79. accessToken:(NSString *)accessToken
  80. expiresIn:(NSNumber *)expiresIn
  81. refreshToken:(NSString *)refreshToken
  82. tokenRequest:(OIDTokenRequest *)tokenRequest {
  83. NSMutableDictionary<NSString *, NSString *> *parameters = [[NSMutableDictionary alloc] initWithDictionary:@{
  84. @"access_token" : accessToken ?: kAccessToken,
  85. @"expires_in" : expiresIn ?: @(kAccessTokenExpiresIn),
  86. @"token_type" : @"example_token_type",
  87. @"refresh_token" : refreshToken ?: kRefreshToken,
  88. @"scope" : [OIDScopeUtilities scopesWithArray:@[ OIDAuthorizationRequestTestingScope2 ]],
  89. @"server_code" : kServerAuthCode,
  90. }];
  91. if (idToken) {
  92. parameters[@"id_token"] = idToken;
  93. }
  94. return [[OIDTokenResponse alloc] initWithRequest:tokenRequest ?: [OIDTokenRequest testInstance]
  95. parameters:parameters];
  96. }
  97. + (NSString *)idToken {
  98. return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:NO];
  99. }
  100. + (NSString *)fatIDToken {
  101. return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:YES];
  102. }
  103. + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp {
  104. return [self idTokenWithSub:sub exp:exp fat:NO];
  105. }
  106. + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat {
  107. NSError *error;
  108. NSDictionary *headerContents = @{
  109. @"alg" : kAlg,
  110. @"kid" : kKid,
  111. @"typ" : kTyp,
  112. };
  113. NSData *headerJson = [NSJSONSerialization dataWithJSONObject:headerContents
  114. options:NSJSONWritingPrettyPrinted
  115. error:&error];
  116. if (error || !headerJson) {
  117. return nil;
  118. }
  119. NSMutableDictionary<NSString *, NSString *> *payloadContents =
  120. [NSMutableDictionary dictionaryWithDictionary:@{
  121. @"sub" : sub,
  122. @"hd" : kHostedDomain,
  123. @"iss" : kIssuer,
  124. @"aud" : kAudience,
  125. @"exp" : exp,
  126. @"iat" : @(kIssuedAt),
  127. }];
  128. if (fat) {
  129. [payloadContents addEntriesFromDictionary:@{
  130. kFatNameKey : kFatName,
  131. kFatGivenNameKey : kFatGivenName,
  132. kFatFamilyNameKey : kFatFamilyName,
  133. kFatPictureURLKey : kFatPictureURL,
  134. }];
  135. }
  136. NSData *payloadJson = [NSJSONSerialization dataWithJSONObject:payloadContents
  137. options:NSJSONWritingPrettyPrinted
  138. error:&error];
  139. if (error || !payloadJson) {
  140. return nil;
  141. }
  142. return [NSString stringWithFormat:@"%@.%@.FakeSignature",
  143. [headerJson base64EncodedStringWithOptions:0],
  144. [payloadJson base64EncodedStringWithOptions:0]];
  145. }
  146. @end