GIDToken.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2022 Google LLC
  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 "GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h"
  17. #import "GoogleSignIn/Sources/GIDToken_Private.h"
  18. // Key constants used for encode and decode.
  19. static NSString *const kTokenStringKey = @"tokenString";
  20. static NSString *const kExpirationDateKey = @"expirationDate";
  21. NS_ASSUME_NONNULL_BEGIN
  22. @implementation GIDToken
  23. - (instancetype)initWithTokenString:(NSString *)tokenString
  24. expirationDate:(nullable NSDate *)expirationDate {
  25. self = [super init];
  26. if (self) {
  27. _tokenString = [tokenString copy];
  28. _expirationDate = expirationDate;
  29. }
  30. return self;
  31. }
  32. #pragma mark - NSSecureCoding
  33. + (BOOL)supportsSecureCoding {
  34. return YES;
  35. }
  36. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  37. self = [super init];
  38. if (self) {
  39. _tokenString = [decoder decodeObjectOfClass:[NSString class] forKey:kTokenStringKey];
  40. _expirationDate = [decoder decodeObjectOfClass:[NSDate class] forKey:kExpirationDateKey];
  41. }
  42. return self;
  43. }
  44. - (void)encodeWithCoder:(NSCoder *)encoder {
  45. [encoder encodeObject:_tokenString forKey:kTokenStringKey];
  46. [encoder encodeObject:_expirationDate forKey:kExpirationDateKey];
  47. }
  48. #pragma mark - isEqual
  49. - (BOOL)isEqual:(nullable id)object {
  50. if (object == nil) {
  51. return NO;
  52. }
  53. if (self == object) {
  54. return YES;
  55. }
  56. if (![object isKindOfClass:[GIDToken class]]) {
  57. return NO;
  58. }
  59. return [self isEqualToToken:(GIDToken *)object];
  60. }
  61. - (BOOL)isEqualToToken:(GIDToken *)otherToken {
  62. return [_tokenString isEqual:otherToken.tokenString] &&
  63. [self isTheSameDate:_expirationDate with:otherToken.expirationDate];
  64. }
  65. // The date is nullable in GIDToken. Two `nil` dates are considered equal so
  66. // token equality check succeeds if token strings are equal and have no expiration.
  67. - (BOOL)isTheSameDate:(nullable NSDate *)date1
  68. with:(nullable NSDate *)date2 {
  69. if (!date1 && !date2) {
  70. return YES;
  71. }
  72. return [date1 isEqualToDate:date2];
  73. }
  74. - (NSUInteger)hash {
  75. return [self.tokenString hash] ^ [self.expirationDate hash];
  76. }
  77. @end
  78. NS_ASSUME_NONNULL_END