You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.4 KiB

6 years ago
  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 "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. @implementation FIRVerifyPhoneNumberRequest
  47. - (nullable instancetype)initWithTemporaryProof:(NSString *)temporaryProof
  48. phoneNumber:(NSString *)phoneNumber
  49. operation:(FIRAuthOperationType)operation
  50. requestConfiguration:
  51. (FIRAuthRequestConfiguration *)requestConfiguration {
  52. self = [super initWithEndpoint:kVerifyPhoneNumberEndPoint
  53. requestConfiguration:requestConfiguration];
  54. if (self) {
  55. _temporaryProof = [temporaryProof copy];
  56. _phoneNumber = [phoneNumber copy];
  57. _operation = operation;
  58. }
  59. return self;
  60. }
  61. - (nullable instancetype)initWithVerificationID:(NSString *)verificationID
  62. verificationCode:(NSString *)verificationCode
  63. operation:(FIRAuthOperationType)operation
  64. requestConfiguration:
  65. (FIRAuthRequestConfiguration *)requestConfiguration {
  66. self = [super initWithEndpoint:kVerifyPhoneNumberEndPoint
  67. requestConfiguration:requestConfiguration];
  68. if (self) {
  69. _verificationID = verificationID;
  70. _verificationCode = verificationCode;
  71. _operation = operation;
  72. }
  73. return self;
  74. }
  75. /** @fn FIRAuthOperationString
  76. @brief Returns a string object corresponding to the provided FIRAuthOperationType value.
  77. @param operationType The value of the FIRAuthOperationType enum which will be translated to its
  78. corresponding string value.
  79. @return The string value corresponding to the FIRAuthOperationType argument.
  80. */
  81. NSString *const FIRAuthOperationString(FIRAuthOperationType operationType) {
  82. switch(operationType){
  83. case FIRAuthOperationTypeUnspecified:
  84. return @"VERIFY_OP_UNSPECIFIED";
  85. case FIRAuthOperationTypeSignUpOrSignIn:
  86. return @"SIGN_UP_OR_IN";
  87. case FIRAuthOperationTypeReauth:
  88. return @"REAUTH";
  89. case FIRAuthOperationTypeLink:
  90. return @"LINK";
  91. case FIRAuthOperationTypeUpdate:
  92. return @"UPDATE";
  93. }
  94. }
  95. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *__autoreleasing _Nullable *)error {
  96. NSMutableDictionary *postBody = [NSMutableDictionary dictionary];
  97. if (_verificationID) {
  98. postBody[kVerificationIDKey] = _verificationID;
  99. }
  100. if (_verificationCode) {
  101. postBody[kVerificationCodeKey] = _verificationCode;
  102. }
  103. if (_accessToken) {
  104. postBody[kIDTokenKey] = _accessToken;
  105. }
  106. if (_temporaryProof) {
  107. postBody[kTemporaryProofKey] = _temporaryProof;
  108. }
  109. if (_phoneNumber) {
  110. postBody[kPhoneNumberKey] = _phoneNumber;
  111. }
  112. NSString *operation = FIRAuthOperationString(_operation);
  113. postBody[kOperationKey] = operation;
  114. return postBody;
  115. }
  116. @end
  117. NS_ASSUME_NONNULL_END