FIRVerifyPhoneNumberRequest.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/Backend/RPC/FIRVerifyPhoneNumberRequest.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. /** @var kVerifyPhoneNumberEndPoint
  19. @brief The "verifyPhoneNumber" endpoint.
  20. */
  21. static NSString *const kVerifyPhoneNumberEndPoint = @"verifyPhoneNumber";
  22. /** @var kVerificationIDKey
  23. @brief The key for the verification ID parameter in the request.
  24. */
  25. static NSString *const kVerificationIDKey = @"sessionInfo";
  26. /** @var kVerificationCodeKey
  27. @brief The key for the verification code parameter in the request.
  28. */
  29. static NSString *const kVerificationCodeKey = @"code";
  30. /** @var kIDTokenKey
  31. @brief The key for the "ID Token" value in the request.
  32. */
  33. static NSString *const kIDTokenKey = @"idToken";
  34. /** @var kTemporaryProofKey
  35. @brief The key for the temporary proof value in the request.
  36. */
  37. static NSString *const kTemporaryProofKey = @"temporaryProof";
  38. /** @var kPhoneNumberKey
  39. @brief The key for the phone number value in the request.
  40. */
  41. static NSString *const kPhoneNumberKey = @"phoneNumber";
  42. /** @var kOperationKey
  43. @brief The key for the operation value in the request.
  44. */
  45. static NSString *const kOperationKey = @"operation";
  46. /** @var kTenantIDKey
  47. @brief The key for the tenant id value in the request.
  48. */
  49. static NSString *const kTenantIDKey = @"tenantId";
  50. @implementation FIRVerifyPhoneNumberRequest
  51. - (nullable instancetype)initWithTemporaryProof:(NSString *)temporaryProof
  52. phoneNumber:(NSString *)phoneNumber
  53. operation:(FIRAuthOperationType)operation
  54. requestConfiguration:
  55. (FIRAuthRequestConfiguration *)requestConfiguration {
  56. self = [super initWithEndpoint:kVerifyPhoneNumberEndPoint
  57. requestConfiguration:requestConfiguration];
  58. if (self) {
  59. _temporaryProof = [temporaryProof copy];
  60. _phoneNumber = [phoneNumber copy];
  61. _operation = operation;
  62. }
  63. return self;
  64. }
  65. - (nullable instancetype)initWithVerificationID:(NSString *)verificationID
  66. verificationCode:(NSString *)verificationCode
  67. operation:(FIRAuthOperationType)operation
  68. requestConfiguration:
  69. (FIRAuthRequestConfiguration *)requestConfiguration {
  70. self = [super initWithEndpoint:kVerifyPhoneNumberEndPoint
  71. requestConfiguration:requestConfiguration];
  72. if (self) {
  73. _verificationID = verificationID;
  74. _verificationCode = verificationCode;
  75. _operation = operation;
  76. }
  77. return self;
  78. }
  79. /** @fn FIRAuthOperationString
  80. @brief Returns a string object corresponding to the provided FIRAuthOperationType value.
  81. @param operationType The value of the FIRAuthOperationType enum which will be translated to its
  82. corresponding string value.
  83. @return The string value corresponding to the FIRAuthOperationType argument.
  84. */
  85. NSString *const FIRAuthOperationString(FIRAuthOperationType operationType) {
  86. switch (operationType) {
  87. case FIRAuthOperationTypeUnspecified:
  88. return @"VERIFY_OP_UNSPECIFIED";
  89. case FIRAuthOperationTypeSignUpOrSignIn:
  90. return @"SIGN_UP_OR_IN";
  91. case FIRAuthOperationTypeReauth:
  92. return @"REAUTH";
  93. case FIRAuthOperationTypeLink:
  94. return @"LINK";
  95. case FIRAuthOperationTypeUpdate:
  96. return @"UPDATE";
  97. }
  98. }
  99. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *__autoreleasing _Nullable *)error {
  100. NSMutableDictionary *postBody = [NSMutableDictionary dictionary];
  101. if (_verificationID) {
  102. postBody[kVerificationIDKey] = _verificationID;
  103. }
  104. if (_verificationCode) {
  105. postBody[kVerificationCodeKey] = _verificationCode;
  106. }
  107. if (_accessToken) {
  108. postBody[kIDTokenKey] = _accessToken;
  109. }
  110. if (_temporaryProof) {
  111. postBody[kTemporaryProofKey] = _temporaryProof;
  112. }
  113. if (_phoneNumber) {
  114. postBody[kPhoneNumberKey] = _phoneNumber;
  115. }
  116. if (self.tenantID) {
  117. postBody[kTenantIDKey] = self.tenantID;
  118. }
  119. NSString *operation = FIRAuthOperationString(_operation);
  120. postBody[kOperationKey] = operation;
  121. return [postBody copy];
  122. }
  123. @end
  124. NS_ASSUME_NONNULL_END