OIDTokenResponse+Testing.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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)testInstanceWithIDToken:(NSString *)idToken {
  51. return [OIDTokenResponse testInstanceWithIDToken:idToken
  52. accessToken:nil
  53. expiresIn:nil
  54. refreshToken:nil
  55. tokenRequest:nil];
  56. }
  57. + (instancetype)testInstanceWithIDToken:(NSString *)idToken
  58. accessToken:(NSString *)accessToken
  59. expiresIn:(NSNumber *)expiresIn
  60. refreshToken:(NSString *)refreshToken
  61. tokenRequest:(OIDTokenRequest *)tokenRequest {
  62. NSMutableDictionary<NSString *, NSString *> *parameters;
  63. parameters = [[NSMutableDictionary alloc] initWithDictionary:@{
  64. @"access_token" : accessToken ?: kAccessToken,
  65. @"expires_in" : expiresIn ?: @(kAccessTokenExpiresIn),
  66. @"token_type" : @"example_token_type",
  67. @"refresh_token" : refreshToken ?: kRefreshToken,
  68. @"scope" : [OIDScopeUtilities scopesWithArray:@[ OIDAuthorizationRequestTestingScope2 ]],
  69. @"server_code" : kServerAuthCode,
  70. }];
  71. if (idToken) {
  72. parameters[@"id_token"] = idToken;
  73. }
  74. return [[OIDTokenResponse alloc] initWithRequest:tokenRequest ?: [OIDTokenRequest testInstance]
  75. parameters:parameters];
  76. }
  77. + (NSString *)idToken {
  78. return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:NO];
  79. }
  80. + (NSString *)fatIDToken {
  81. return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:YES];
  82. }
  83. + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp {
  84. return [self idTokenWithSub:sub exp:exp fat:NO];
  85. }
  86. + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat {
  87. NSError *error;
  88. NSDictionary *headerContents = @{
  89. @"alg" : kAlg,
  90. @"kid" : kKid,
  91. @"typ" : kTyp,
  92. };
  93. NSData *headerJson = [NSJSONSerialization dataWithJSONObject:headerContents
  94. options:NSJSONWritingPrettyPrinted
  95. error:&error];
  96. if (error || !headerJson) {
  97. return nil;
  98. }
  99. NSMutableDictionary<NSString *, NSString *> *payloadContents =
  100. [NSMutableDictionary dictionaryWithDictionary:@{
  101. @"sub" : sub,
  102. @"hd" : kHostedDomain,
  103. @"iss" : kIssuer,
  104. @"aud" : kAudience,
  105. @"exp" : exp,
  106. @"iat" : @(kIssuedAt),
  107. }];
  108. if (fat) {
  109. [payloadContents addEntriesFromDictionary:@{
  110. kFatNameKey : kFatName,
  111. kFatGivenNameKey : kFatGivenName,
  112. kFatFamilyNameKey : kFatFamilyName,
  113. kFatPictureURLKey : kFatPictureURL,
  114. }];
  115. }
  116. NSData *payloadJson = [NSJSONSerialization dataWithJSONObject:payloadContents
  117. options:NSJSONWritingPrettyPrinted
  118. error:&error];
  119. if (error || !payloadJson) {
  120. return nil;
  121. }
  122. return [NSString stringWithFormat:@"%@.%@.FakeSignature",
  123. [headerJson base64EncodedStringWithOptions:0],
  124. [payloadJson base64EncodedStringWithOptions:0]];
  125. }
  126. @end