FIROAuthCredential.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017 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/Public/FirebaseAuth/FIROAuthCredential.h"
  17. #import "FirebaseAuth/Sources/AuthProvider/FIRAuthCredential_Internal.h"
  18. #import "FirebaseAuth/Sources/AuthProvider/OAuth/FIROAuthCredential_Internal.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionResponse.h"
  21. #import "FirebaseAuth/Sources/Utilities/FIRAuthExceptionUtils.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. @interface FIROAuthCredential ()
  24. @property(nonatomic, nullable) NSString *rawNonce;
  25. - (nullable instancetype)initWithProvider:(NSString *)provider NS_UNAVAILABLE;
  26. @end
  27. @implementation FIROAuthCredential
  28. - (nullable instancetype)initWithProvider:(NSString *)provider {
  29. [FIRAuthExceptionUtils
  30. raiseMethodNotImplementedExceptionWithReason:@"Please call the designated initializer."];
  31. return nil;
  32. }
  33. - (instancetype)initWithProviderID:(NSString *)providerID
  34. IDToken:(nullable NSString *)IDToken
  35. rawNonce:(nullable NSString *)rawNonce
  36. accessToken:(nullable NSString *)accessToken
  37. secret:(nullable NSString *)secret
  38. fullName:(nullable NSPersonNameComponents *)fullName
  39. pendingToken:(nullable NSString *)pendingToken {
  40. self = [super initWithProvider:providerID];
  41. if (self) {
  42. _IDToken = IDToken;
  43. _rawNonce = rawNonce;
  44. _accessToken = accessToken;
  45. _pendingToken = pendingToken;
  46. _secret = secret;
  47. _fullName = fullName;
  48. }
  49. return self;
  50. }
  51. - (instancetype)initWithProviderID:(NSString *)providerID
  52. sessionID:(NSString *)sessionID
  53. OAuthResponseURLString:(NSString *)OAuthResponseURLString {
  54. self = [self initWithProviderID:providerID
  55. IDToken:nil
  56. rawNonce:nil
  57. accessToken:nil
  58. secret:nil
  59. fullName:nil
  60. pendingToken:nil];
  61. if (self) {
  62. _OAuthResponseURLString = OAuthResponseURLString;
  63. _sessionID = sessionID;
  64. }
  65. return self;
  66. }
  67. - (nullable instancetype)initWithVerifyAssertionResponse:(FIRVerifyAssertionResponse *)response {
  68. if (response.oauthIDToken.length || response.oauthAccessToken.length ||
  69. response.oauthSecretToken.length) {
  70. return [self initWithProviderID:response.providerID
  71. IDToken:response.oauthIDToken
  72. rawNonce:nil
  73. accessToken:response.oauthAccessToken
  74. secret:response.oauthSecretToken
  75. fullName:nil
  76. pendingToken:response.pendingToken];
  77. }
  78. return nil;
  79. }
  80. - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
  81. request.providerIDToken = _IDToken;
  82. request.providerRawNonce = _rawNonce;
  83. request.providerAccessToken = _accessToken;
  84. request.requestURI = _OAuthResponseURLString;
  85. request.sessionID = _sessionID;
  86. request.providerOAuthTokenSecret = _secret;
  87. request.pendingToken = _pendingToken;
  88. request.fullName = _fullName;
  89. }
  90. #pragma mark - NSSecureCoding
  91. + (BOOL)supportsSecureCoding {
  92. return YES;
  93. }
  94. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  95. NSString *IDToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"IDToken"];
  96. NSString *rawNonce = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"rawNonce"];
  97. NSString *accessToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"accessToken"];
  98. NSString *pendingToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"pendingToken"];
  99. NSString *secret = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"secret"];
  100. NSPersonNameComponents *fullName = [aDecoder decodeObjectOfClass:[NSPersonNameComponents class]
  101. forKey:@"fullName"];
  102. self = [self initWithProviderID:self.provider
  103. IDToken:IDToken
  104. rawNonce:rawNonce
  105. accessToken:accessToken
  106. secret:secret
  107. fullName:fullName
  108. pendingToken:pendingToken];
  109. return self;
  110. }
  111. - (void)encodeWithCoder:(NSCoder *)aCoder {
  112. [aCoder encodeObject:self.IDToken forKey:@"IDToken"];
  113. [aCoder encodeObject:self.rawNonce forKey:@"rawNonce"];
  114. [aCoder encodeObject:self.accessToken forKey:@"accessToken"];
  115. [aCoder encodeObject:self.pendingToken forKey:@"pendingToken"];
  116. [aCoder encodeObject:self.secret forKey:@"secret"];
  117. [aCoder encodeObject:self.fullName forKey:@"fullName"];
  118. }
  119. @end
  120. NS_ASSUME_NONNULL_END