FIRGetOOBConfirmationCodeRequest.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "FIRActionCodeSettings.h"
  18. #import "FIRAuthErrorUtils.h"
  19. #import "FIRAuth_Internal.h"
  20. /** @var kEndpoint
  21. @brief The getOobConfirmationCode endpoint name.
  22. */
  23. static NSString *const kGetOobConfirmationCodeEndpoint = @"getOobConfirmationCode";
  24. /** @var kRequestTypeKey
  25. @brief The name of the required "requestType" property in the request.
  26. */
  27. static NSString *const kRequestTypeKey = @"requestType";
  28. /** @var kEmailKey
  29. @brief The name of the "email" property in the request.
  30. */
  31. static NSString *const kEmailKey = @"email";
  32. /** @var kIDTokenKey
  33. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  34. despite it's confusing (backwards compatiable) parameter name.
  35. */
  36. static NSString *const kIDTokenKey = @"idToken";
  37. /** @var kContinueURLKey
  38. @brief The key for the "continue URL" value in the request.
  39. */
  40. static NSString *const kContinueURLKey = @"continueUrl";
  41. /** @var kIosBundeIDKey
  42. @brief The key for the "iOS Bundle Identifier" value in the request.
  43. */
  44. static NSString *const kIosBundleIDKey = @"iOSBundleId";
  45. /** @var kAndroidPackageNameKey
  46. @brief The key for the "Android Package Name" value in the request.
  47. */
  48. static NSString *const kAndroidPackageNameKey = @"androidPackageName";
  49. /** @var kAndroidInstallAppKey
  50. @brief The key for the request parameter indicating whether the android app should be installed
  51. or not.
  52. */
  53. static NSString *const kAndroidInstallAppKey = @"androidInstallApp";
  54. /** @var kAndroidMinimumVersionKey
  55. @brief The key for the "minimum Android version supported" value in the request.
  56. */
  57. static NSString *const kAndroidMinimumVersionKey = @"androidMinimumVersion";
  58. /** @var kCanHandleCodeInAppKey
  59. @brief The key for the request parameter indicating whether the action code can be handled in
  60. the app or not.
  61. */
  62. static NSString *const kCanHandleCodeInAppKey = @"canHandleCodeInApp";
  63. /** @var kPasswordResetRequestTypeValue
  64. @brief The value for the "PASSWORD_RESET" request type.
  65. */
  66. static NSString *const kPasswordResetRequestTypeValue = @"PASSWORD_RESET";
  67. /** @var kVerifyEmailRequestTypeValue
  68. @brief The value for the "VERIFY_EMAIL" request type.
  69. */
  70. static NSString *const kVerifyEmailRequestTypeValue = @"VERIFY_EMAIL";
  71. @interface FIRGetOOBConfirmationCodeRequest ()
  72. /** @fn initWithRequestType:email:APIKey:
  73. @brief Designated initializer.
  74. @param requestType The types of OOB Confirmation Code to request.
  75. @param email The email of the user.
  76. @param accessToken The STS Access Token of the currently signed in user.
  77. @param actionCodeSettings An object of FIRActionCodeSettings which specifies action code
  78. settings to be applied to the OOB code request.
  79. @param requestConfiguration An object containing configurations to be added to the request.
  80. */
  81. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  82. email:(nullable NSString *)email
  83. accessToken:(nullable NSString *)accessToken
  84. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  85. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  86. NS_DESIGNATED_INITIALIZER;
  87. @end
  88. @implementation FIRGetOOBConfirmationCodeRequest
  89. /** @var requestTypeStringValueForRequestType:
  90. @brief Returns the string equivilent for an @c FIRGetOOBConfirmationCodeRequestType value.
  91. */
  92. + (NSString *)requestTypeStringValueForRequestType:
  93. (FIRGetOOBConfirmationCodeRequestType)requestType {
  94. switch (requestType) {
  95. case FIRGetOOBConfirmationCodeRequestTypePasswordReset:
  96. return kPasswordResetRequestTypeValue;
  97. case FIRGetOOBConfirmationCodeRequestTypeVerifyEmail:
  98. return kVerifyEmailRequestTypeValue;
  99. // No default case so that we get a compiler warning if a new value was added to the enum.
  100. }
  101. }
  102. + (FIRGetOOBConfirmationCodeRequest *)
  103. passwordResetRequestWithEmail:(NSString *)email
  104. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  105. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  106. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypePasswordReset
  107. email:email
  108. accessToken:nil
  109. actionCodeSettings:actionCodeSettings
  110. requestConfiguration:requestConfiguration];
  111. }
  112. + (FIRGetOOBConfirmationCodeRequest *)
  113. verifyEmailRequestWithAccessToken:(NSString *)accessToken
  114. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  115. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  116. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeVerifyEmail
  117. email:nil
  118. accessToken:accessToken
  119. actionCodeSettings:actionCodeSettings
  120. requestConfiguration:requestConfiguration];
  121. }
  122. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  123. email:(nullable NSString *)email
  124. accessToken:(nullable NSString *)accessToken
  125. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  126. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  127. self = [super initWithEndpoint:kGetOobConfirmationCodeEndpoint
  128. requestConfiguration:requestConfiguration];
  129. if (self) {
  130. _requestType = requestType;
  131. _email = email;
  132. _accessToken = accessToken;
  133. _continueURL = actionCodeSettings.URL.absoluteString;
  134. _iOSBundleID = actionCodeSettings.iOSBundleID;
  135. _androidPackageName = actionCodeSettings.androidPackageName;
  136. _androidMinimumVersion = actionCodeSettings.androidMinimumVersion;
  137. _androidInstallApp = actionCodeSettings.androidInstallIfNotAvailable;
  138. _handleCodeInApp = actionCodeSettings.handleCodeInApp;
  139. }
  140. return self;
  141. }
  142. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  143. NSMutableDictionary *body = [@{
  144. kRequestTypeKey : [[self class] requestTypeStringValueForRequestType:_requestType]
  145. } mutableCopy];
  146. // For password reset requests, we only need an email address in addition to the already required
  147. // fields.
  148. if (_requestType == FIRGetOOBConfirmationCodeRequestTypePasswordReset) {
  149. body[kEmailKey] = _email;
  150. }
  151. // For verify email requests, we only need an STS Access Token in addition to the already required
  152. // fields.
  153. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeVerifyEmail) {
  154. body[kIDTokenKey] = _accessToken;
  155. }
  156. if (_continueURL) {
  157. body[kContinueURLKey] = _continueURL;
  158. }
  159. if (_iOSBundleID) {
  160. body[kIosBundleIDKey] = _iOSBundleID;
  161. }
  162. if (_androidPackageName) {
  163. body[kAndroidPackageNameKey] = _androidPackageName;
  164. }
  165. if (_androidMinimumVersion) {
  166. body[kAndroidMinimumVersionKey] = _androidMinimumVersion;
  167. }
  168. if (_androidInstallApp) {
  169. body[kAndroidInstallAppKey] = @YES;
  170. }
  171. if (_handleCodeInApp) {
  172. body[kCanHandleCodeInAppKey] = @YES;
  173. }
  174. return body;
  175. }
  176. @end