FIROAuthCredential.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. pendingToken:(nullable NSString *)pendingToken {
  39. self = [super initWithProvider:providerID];
  40. if (self) {
  41. _IDToken = IDToken;
  42. _rawNonce = rawNonce;
  43. _accessToken = accessToken;
  44. _pendingToken = pendingToken;
  45. _secret = secret;
  46. }
  47. return self;
  48. }
  49. - (instancetype)initWithProviderID:(NSString *)providerID
  50. IDToken:(nullable NSString *)IDToken
  51. rawNonce:(nullable NSString *)rawNonce
  52. accessToken:(nullable NSString *)accessToken
  53. secret:(nullable NSString *)secret
  54. displayName:(nullable NSString *)displayName
  55. pendingToken:(nullable NSString *)pendingToken {
  56. self = [super initWithProvider:providerID];
  57. if (self) {
  58. _IDToken = IDToken;
  59. _rawNonce = rawNonce;
  60. _accessToken = accessToken;
  61. _pendingToken = pendingToken;
  62. _secret = secret;
  63. _displayName = displayName;
  64. }
  65. return self;
  66. }
  67. - (instancetype)initWithProviderID:(NSString *)providerID
  68. sessionID:(NSString *)sessionID
  69. OAuthResponseURLString:(NSString *)OAuthResponseURLString {
  70. self = [self initWithProviderID:providerID
  71. IDToken:nil
  72. rawNonce:nil
  73. accessToken:nil
  74. secret:nil
  75. pendingToken:nil];
  76. if (self) {
  77. _OAuthResponseURLString = OAuthResponseURLString;
  78. _sessionID = sessionID;
  79. }
  80. return self;
  81. }
  82. - (nullable instancetype)initWithVerifyAssertionResponse:(FIRVerifyAssertionResponse *)response {
  83. if (response.oauthIDToken.length || response.oauthAccessToken.length ||
  84. response.oauthSecretToken.length) {
  85. return [self initWithProviderID:response.providerID
  86. IDToken:response.oauthIDToken
  87. rawNonce:nil
  88. accessToken:response.oauthAccessToken
  89. secret:response.oauthSecretToken
  90. pendingToken:response.pendingToken];
  91. }
  92. return nil;
  93. }
  94. - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
  95. request.providerIDToken = _IDToken;
  96. request.providerRawNonce = _rawNonce;
  97. request.providerAccessToken = _accessToken;
  98. request.requestURI = _OAuthResponseURLString;
  99. request.sessionID = _sessionID;
  100. request.providerOAuthTokenSecret = _secret;
  101. request.pendingToken = _pendingToken;
  102. request.displayName = _displayName;
  103. }
  104. #pragma mark - NSSecureCoding
  105. + (BOOL)supportsSecureCoding {
  106. return YES;
  107. }
  108. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  109. NSString *IDToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"IDToken"];
  110. NSString *rawNonce = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"rawNonce"];
  111. NSString *accessToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"accessToken"];
  112. NSString *pendingToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"pendingToken"];
  113. NSString *secret = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"secret"];
  114. self = [self initWithProviderID:self.provider
  115. IDToken:IDToken
  116. rawNonce:rawNonce
  117. accessToken:accessToken
  118. secret:secret
  119. pendingToken:pendingToken];
  120. return self;
  121. }
  122. - (void)encodeWithCoder:(NSCoder *)aCoder {
  123. [aCoder encodeObject:self.IDToken forKey:@"IDToken"];
  124. [aCoder encodeObject:self.rawNonce forKey:@"rawNonce"];
  125. [aCoder encodeObject:self.accessToken forKey:@"accessToken"];
  126. [aCoder encodeObject:self.pendingToken forKey:@"pendingToken"];
  127. [aCoder encodeObject:self.secret forKey:@"secret"];
  128. }
  129. @end
  130. NS_ASSUME_NONNULL_END