FIROAuthCredential.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "FIROAuthCredential.h"
  17. #import "FIRAuthCredential_Internal.h"
  18. #import "FIRAuthExceptionUtils.h"
  19. #import "FIROAuthCredential_Internal.h"
  20. #import "FIRVerifyAssertionRequest.h"
  21. #import "FIRVerifyAssertionResponse.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. @interface FIROAuthCredential ()
  24. - (nullable instancetype)initWithProvider:(NSString *)provider NS_UNAVAILABLE;
  25. @end
  26. @implementation FIROAuthCredential
  27. - (nullable instancetype)initWithProvider:(NSString *)provider {
  28. [FIRAuthExceptionUtils raiseMethodNotImplementedExceptionWithReason:
  29. @"Please call the designated initializer."];
  30. return nil;
  31. }
  32. - (instancetype)initWithProviderID:(NSString *)providerID
  33. IDToken:(nullable NSString *)IDToken
  34. accessToken:(nullable NSString *)accessToken
  35. secret:(nullable NSString *)secret
  36. pendingToken:(nullable NSString *)pendingToken {
  37. self = [super initWithProvider:providerID];
  38. if (self) {
  39. _IDToken = IDToken;
  40. _accessToken = accessToken;
  41. _pendingToken = pendingToken;
  42. _secret = secret;
  43. }
  44. return self;
  45. }
  46. - (instancetype)initWithProviderID:(NSString *)providerID
  47. sessionID:(NSString *)sessionID
  48. OAuthResponseURLString:(NSString *)OAuthResponseURLString {
  49. self =
  50. [self initWithProviderID:providerID IDToken:nil accessToken:nil secret:nil pendingToken:nil];
  51. if (self) {
  52. _OAuthResponseURLString = OAuthResponseURLString;
  53. _sessionID = sessionID;
  54. }
  55. return self;
  56. }
  57. - (nullable instancetype)initWithVerifyAssertionResponse:(FIRVerifyAssertionResponse *)response {
  58. if (response.oauthIDToken.length || response.oauthAccessToken.length ||
  59. response.oauthSecretToken.length) {
  60. return [self initWithProviderID:response.providerID
  61. IDToken:response.oauthIDToken
  62. accessToken:response.oauthAccessToken
  63. secret:response.oauthSecretToken
  64. pendingToken:response.pendingToken];
  65. }
  66. return nil;
  67. }
  68. - (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
  69. request.providerIDToken = _IDToken;
  70. request.providerAccessToken = _accessToken;
  71. request.requestURI = _OAuthResponseURLString;
  72. request.sessionID = _sessionID;
  73. request.providerOAuthTokenSecret = _secret;
  74. request.pendingToken = _pendingToken;
  75. }
  76. #pragma mark - NSSecureCoding
  77. + (BOOL)supportsSecureCoding {
  78. return YES;
  79. }
  80. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  81. NSString *IDToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"IDToken"];
  82. NSString *accessToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"accessToken"];
  83. NSString *pendingToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"pendingToken"];
  84. NSString *secret = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"secret"];
  85. self = [self initWithProviderID:self.provider
  86. IDToken:IDToken
  87. accessToken:accessToken
  88. secret:secret
  89. pendingToken:pendingToken];
  90. return self;
  91. }
  92. - (void)encodeWithCoder:(NSCoder *)aCoder {
  93. [aCoder encodeObject:self.IDToken forKey:@"IDToken"];
  94. [aCoder encodeObject:self.accessToken forKey:@"accessToken"];
  95. [aCoder encodeObject:self.pendingToken forKey:@"pendingToken"];
  96. [aCoder encodeObject:self.secret forKey:@"secret"];
  97. }
  98. @end
  99. NS_ASSUME_NONNULL_END