FIRAuthTokenResult.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseAuth/Sources/Auth/FIRAuthTokenResult_Internal.h"
  17. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. /** @var kExpirationDateKey
  20. @brief The key used to encode the expirationDate property for NSSecureCoding.
  21. */
  22. static NSString *const kExpirationDateKey = @"expiratinDate";
  23. /** @var kTokenKey
  24. @brief The key used to encode the token property for NSSecureCoding.
  25. */
  26. static NSString *const kTokenKey = @"token";
  27. /** @var kAuthDateKey
  28. @brief The key used to encode the authDate property for NSSecureCoding.
  29. */
  30. static NSString *const kAuthDateKey = @"authDate";
  31. /** @var kIssuedDateKey
  32. @brief The key used to encode the issuedDate property for NSSecureCoding.
  33. */
  34. static NSString *const kIssuedDateKey = @"issuedDate";
  35. /** @var kSignInProviderKey
  36. @brief The key used to encode the signInProvider property for NSSecureCoding.
  37. */
  38. static NSString *const kSignInProviderKey = @"signInProvider";
  39. /** @var kSignInSecondFactorKey
  40. @brief The key used to encode the signInSecondFactor property for NSSecureCoding.
  41. */
  42. static NSString *const kSignInSecondFactorKey = @"signInSecondFactor";
  43. /** @var kClaimsKey
  44. @brief The key used to encode the claims property for NSSecureCoding.
  45. */
  46. static NSString *const kClaimsKey = @"claims";
  47. @implementation FIRAuthTokenResult
  48. - (instancetype)initWithToken:(NSString *)token
  49. expirationDate:(NSDate *)expirationDate
  50. authDate:(NSDate *)authDate
  51. issuedAtDate:(NSDate *)issuedAtDate
  52. signInProvider:(NSString *)signInProvider
  53. signInSecondFactor:(NSString *)signInSecondFactor
  54. claims:(NSDictionary *)claims {
  55. self = [super init];
  56. if (self) {
  57. _token = token;
  58. _expirationDate = expirationDate;
  59. _authDate = authDate;
  60. _issuedAtDate = issuedAtDate;
  61. _signInProvider = signInProvider;
  62. _signInSecondFactor = signInSecondFactor;
  63. _claims = claims;
  64. }
  65. return self;
  66. }
  67. + (nullable FIRAuthTokenResult *)tokenResultWithToken:(NSString *)token {
  68. NSArray *tokenStringArray = [token componentsSeparatedByString:@"."];
  69. // The JWT should have three parts, though we only use the second in this method.
  70. if (tokenStringArray.count != 3) {
  71. return nil;
  72. }
  73. // The token payload is always the second index of the array.
  74. NSString *IDToken = tokenStringArray[1];
  75. // Convert the base64URL encoded string to a base64 encoded string.
  76. // Replace "_" with "/"
  77. NSMutableString *tokenPayload = [[IDToken stringByReplacingOccurrencesOfString:@"_"
  78. withString:@"/"] mutableCopy];
  79. // Replace "-" with "+"
  80. [tokenPayload replaceOccurrencesOfString:@"-"
  81. withString:@"+"
  82. options:kNilOptions
  83. range:NSMakeRange(0, tokenPayload.length)];
  84. // Pad the token payload with "=" signs if the payload's length is not a multiple of 4.
  85. while ((tokenPayload.length % 4) != 0) {
  86. [tokenPayload appendFormat:@"="];
  87. }
  88. NSData *decodedTokenPayloadData =
  89. [[NSData alloc] initWithBase64EncodedString:tokenPayload
  90. options:NSDataBase64DecodingIgnoreUnknownCharacters];
  91. if (!decodedTokenPayloadData) {
  92. return nil;
  93. }
  94. NSError *jsonError = nil;
  95. NSJSONReadingOptions options = NSJSONReadingMutableContainers | NSJSONReadingAllowFragments;
  96. NSDictionary *tokenPayloadDictionary =
  97. [NSJSONSerialization JSONObjectWithData:decodedTokenPayloadData
  98. options:options
  99. error:&jsonError];
  100. if (jsonError != nil) {
  101. return nil;
  102. }
  103. if (!tokenPayloadDictionary) {
  104. return nil;
  105. }
  106. // These are dates since 00:00:00 January 1 1970, as described by the Terminology section in
  107. // the JWT spec. https://tools.ietf.org/html/rfc7519
  108. NSDate *expirationDate =
  109. [NSDate dateWithTimeIntervalSince1970:[tokenPayloadDictionary[@"exp"] doubleValue]];
  110. NSDate *authDate =
  111. [NSDate dateWithTimeIntervalSince1970:[tokenPayloadDictionary[@"auth_time"] doubleValue]];
  112. NSDate *issuedAtDate =
  113. [NSDate dateWithTimeIntervalSince1970:[tokenPayloadDictionary[@"iat"] doubleValue]];
  114. NSDictionary *firebaseTokenPayloadDictionary = tokenPayloadDictionary[@"firebase"];
  115. NSString *signInProvider = firebaseTokenPayloadDictionary[@"sign_in_provider"];
  116. NSString *signInSecondFactor = firebaseTokenPayloadDictionary[@"sign_in_second_factor"];
  117. FIRAuthTokenResult *tokenResult =
  118. [[FIRAuthTokenResult alloc] initWithToken:token
  119. expirationDate:expirationDate
  120. authDate:authDate
  121. issuedAtDate:issuedAtDate
  122. signInProvider:signInProvider
  123. signInSecondFactor:signInSecondFactor
  124. claims:tokenPayloadDictionary];
  125. return tokenResult;
  126. }
  127. #pragma mark - NSSecureCoding
  128. + (BOOL)supportsSecureCoding {
  129. return YES;
  130. }
  131. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  132. NSString *token = [aDecoder decodeObjectOfClass:[NSDate class] forKey:kTokenKey];
  133. return [FIRAuthTokenResult tokenResultWithToken:token];
  134. }
  135. - (void)encodeWithCoder:(NSCoder *)aCoder {
  136. [aCoder encodeObject:_token forKey:kTokenKey];
  137. }
  138. @end
  139. NS_ASSUME_NONNULL_END