OIDTokenResponse+Testing.m 5.1 KB

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