OIDTokenResponse+Testing.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #import <AppAuth/OIDScopeUtilities.h>
  18. #import <AppAuth/OIDTokenRequest.h>
  19. #import <AppAuth/OIDTokenResponse.h>
  20. NSString *const kAccessToken = @"access_token";
  21. NSTimeInterval const kAccessTokenExpiresIn = 3600;
  22. NSString *const kRefreshToken = @"refresh_token";
  23. NSString *const kServerAuthCode = @"server_auth_code";
  24. // ID token constants
  25. NSString *const kAlg = @"RS256";
  26. NSString *const kKid = @"alkjdfas";
  27. NSString *const kTyp = @"JWT";
  28. NSString *const kUserID = @"12345679";
  29. NSString *const kHostedDomain = @"fakehosteddomain.com";
  30. NSString *const kIssuer = @"https://test.com";
  31. NSString *const kAudience = @"audience";
  32. NSTimeInterval const kIDTokenExpires = 1000;
  33. NSTimeInterval const kIssuedAt = 0;
  34. NSString *const kFatNameKey = @"name";
  35. NSString *const kFatGivenNameKey = @"given_name";
  36. NSString *const kFatFamilyNameKey = @"family_name";
  37. NSString *const kFatPictureURLKey = @"picture";
  38. NSString * const kFatName = @"fake username";
  39. NSString * const kFatGivenName = @"fake";
  40. NSString * const kFatFamilyName = @"username";
  41. NSString * const kFatPictureURL = @"fake_user_picture_url";
  42. @implementation OIDTokenResponse (Testing)
  43. + (instancetype)testInstance {
  44. return [self testInstanceWithIDToken:[self idToken]];
  45. }
  46. + (instancetype)testInstanceWithIDToken:(NSString *)idToken {
  47. return [OIDTokenResponse testInstanceWithIDToken:idToken
  48. accessToken:nil
  49. expiresIn:nil
  50. tokenRequest:nil];
  51. }
  52. + (instancetype)testInstanceWithIDToken:(NSString *)idToken
  53. accessToken:(NSString *)accessToken
  54. expiresIn:(NSNumber *)expiresIn
  55. tokenRequest:(OIDTokenRequest *)tokenRequest {
  56. NSMutableDictionary<NSString *, NSString *> *parameters;
  57. parameters = [[NSMutableDictionary alloc] initWithDictionary:@{
  58. @"access_token" : accessToken ?: kAccessToken,
  59. @"expires_in" : expiresIn ?: @(kAccessTokenExpiresIn),
  60. @"token_type" : @"example_token_type",
  61. @"refresh_token" : kRefreshToken,
  62. @"scope" : [OIDScopeUtilities scopesWithArray:@[ OIDAuthorizationRequestTestingScope2 ]],
  63. @"server_code" : kServerAuthCode,
  64. }];
  65. if (idToken) {
  66. parameters[@"id_token"] = idToken;
  67. }
  68. return [[OIDTokenResponse alloc] initWithRequest:tokenRequest ?: [OIDTokenRequest testInstance]
  69. parameters:parameters];
  70. }
  71. + (NSString *)idToken {
  72. return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:NO];
  73. }
  74. + (NSString *)fatIDToken {
  75. return [self idTokenWithSub:kUserID exp:@(kIDTokenExpires) fat:YES];
  76. }
  77. + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp {
  78. return [self idTokenWithSub:sub exp:exp fat:NO];
  79. }
  80. + (NSString *)idTokenWithSub:(NSString *)sub exp:(NSNumber *)exp fat:(BOOL)fat {
  81. NSError *error;
  82. NSDictionary *headerContents = @{
  83. @"alg" : kAlg,
  84. @"kid" : kKid,
  85. @"typ" : kTyp,
  86. };
  87. NSData *headerJson = [NSJSONSerialization dataWithJSONObject:headerContents
  88. options:NSJSONWritingPrettyPrinted
  89. error:&error];
  90. if (error || !headerJson) {
  91. return nil;
  92. }
  93. NSMutableDictionary<NSString *, NSString *> *payloadContents =
  94. [NSMutableDictionary dictionaryWithDictionary:@{
  95. @"sub" : sub,
  96. @"hd" : kHostedDomain,
  97. @"iss" : kIssuer,
  98. @"aud" : kAudience,
  99. @"exp" : exp,
  100. @"iat" : @(kIssuedAt),
  101. }];
  102. if (fat) {
  103. [payloadContents addEntriesFromDictionary:@{
  104. kFatNameKey : kFatName,
  105. kFatGivenNameKey : kFatGivenName,
  106. kFatFamilyNameKey : kFatFamilyName,
  107. kFatPictureURLKey : kFatPictureURL,
  108. }];
  109. }
  110. NSData *payloadJson = [NSJSONSerialization dataWithJSONObject:payloadContents
  111. options:NSJSONWritingPrettyPrinted
  112. error:&error];
  113. if (error || !payloadJson) {
  114. return nil;
  115. }
  116. return [NSString stringWithFormat:@"%@.%@.FakeSignature",
  117. [headerJson base64EncodedStringWithOptions:0],
  118. [payloadJson base64EncodedStringWithOptions:0]];
  119. }
  120. @end