FIRGetOOBConfirmationCodeRequest.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "FIRGetOOBConfirmationCodeRequest.h"
  17. #import "../Private/FIRAuthErrorUtils.h"
  18. #import "../Private/FIRAuth_Internal.h"
  19. /** @var kEndpoint
  20. @brief The getOobConfirmationCode endpoint name.
  21. */
  22. static NSString *const kEndpoint = @"getOobConfirmationCode";
  23. /** @var kRequestTypeKey
  24. @brief The name of the required "requestType" property in the request.
  25. */
  26. static NSString *const kRequestTypeKey = @"requestType";
  27. /** @var kEmailKey
  28. @brief The name of the "email" property in the request.
  29. */
  30. static NSString *const kEmailKey = @"email";
  31. /** @var kIDTokenKey
  32. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  33. despite it's confusing (backwards compatiable) parameter name.
  34. */
  35. static NSString *const kIDTokenKey = @"idToken";
  36. /** @var kPasswordResetRequestTypeValue
  37. @brief The value for the "PASSWORD_RESET" request type.
  38. */
  39. static NSString *const kPasswordResetRequestTypeValue = @"PASSWORD_RESET";
  40. /** @var kVerifyEmailRequestTypeValue
  41. @brief The value for the "VERIFY_EMAIL" request type.
  42. */
  43. static NSString *const kVerifyEmailRequestTypeValue = @"VERIFY_EMAIL";
  44. @interface FIRGetOOBConfirmationCodeRequest ()
  45. /** @fn initWithRequestType:email:APIKey:
  46. @brief Designated initializer.
  47. @param requestType The types of OOB Confirmation Code to request.
  48. @param email The email of the user.
  49. @param accessToken The STS Access Token of the currently signed in user.
  50. @param APIKey The client's API Key.
  51. */
  52. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  53. email:(nullable NSString *)email
  54. accessToken:(nullable NSString *)accessToken
  55. APIKey:(nullable NSString *)APIKey
  56. NS_DESIGNATED_INITIALIZER;
  57. @end
  58. @implementation FIRGetOOBConfirmationCodeRequest
  59. /** @var requestTypeStringValueForRequestType:
  60. @brief Returns the string equivilent for an @c FIRGetOOBConfirmationCodeRequestType value.
  61. */
  62. + (NSString *)requestTypeStringValueForRequestType:
  63. (FIRGetOOBConfirmationCodeRequestType)requestType {
  64. switch (requestType) {
  65. case FIRGetOOBConfirmationCodeRequestTypePasswordReset:
  66. return kPasswordResetRequestTypeValue;
  67. case FIRGetOOBConfirmationCodeRequestTypeVerifyEmail:
  68. return kVerifyEmailRequestTypeValue;
  69. // No default case so that we get a compiler warning if a new value was added to the enum.
  70. }
  71. }
  72. + (FIRGetOOBConfirmationCodeRequest *)passwordResetRequestWithEmail:(NSString *)email
  73. APIKey:(NSString *)APIKey {
  74. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypePasswordReset
  75. email:email
  76. accessToken:nil
  77. APIKey:APIKey];
  78. }
  79. + (FIRGetOOBConfirmationCodeRequest *)
  80. verifyEmailRequestWithAccessToken:(NSString *)accessToken APIKey:(NSString *)APIKey {
  81. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeVerifyEmail
  82. email:nil
  83. accessToken:accessToken
  84. APIKey:APIKey];
  85. }
  86. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  87. email:(nullable NSString *)email
  88. accessToken:(nullable NSString *)accessToken
  89. APIKey:(nullable NSString *)APIKey {
  90. self = [super initWithEndpoint:kEndpoint APIKey:APIKey];
  91. if (self) {
  92. _requestType = requestType;
  93. _email = email;
  94. _accessToken = accessToken;
  95. }
  96. return self;
  97. }
  98. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  99. NSMutableDictionary *body = [@{
  100. kRequestTypeKey : [[self class] requestTypeStringValueForRequestType:_requestType]
  101. } mutableCopy];
  102. // For password reset requests, we only need an email address in addition to the already required
  103. // fields.
  104. if (_requestType == FIRGetOOBConfirmationCodeRequestTypePasswordReset) {
  105. body[kEmailKey] = _email;
  106. }
  107. // For verify email requests, we only need an STS Access Token in addition to the already required
  108. // fields.
  109. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeVerifyEmail) {
  110. body[kIDTokenKey] = _accessToken;
  111. }
  112. return body;
  113. }
  114. @end