OIDTokenResponse+Testing.m 4.9 KB

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