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.

234 lines
8.9 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 "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 kEmailLinkSignInTypeValue
  68. @brief The value for the "EMAIL_SIGNIN" request type.
  69. */
  70. static NSString *const kEmailLinkSignInTypeValue= @"EMAIL_SIGNIN";
  71. /** @var kVerifyEmailRequestTypeValue
  72. @brief The value for the "VERIFY_EMAIL" request type.
  73. */
  74. static NSString *const kVerifyEmailRequestTypeValue = @"VERIFY_EMAIL";
  75. @interface FIRGetOOBConfirmationCodeRequest ()
  76. /** @fn initWithRequestType:email:APIKey:
  77. @brief Designated initializer.
  78. @param requestType The types of OOB Confirmation Code to request.
  79. @param email The email of the user.
  80. @param accessToken The STS Access Token of the currently signed in user.
  81. @param actionCodeSettings An object of FIRActionCodeSettings which specifies action code
  82. settings to be applied to the OOB code request.
  83. @param requestConfiguration An object containing configurations to be added to the request.
  84. */
  85. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  86. email:(nullable NSString *)email
  87. accessToken:(nullable NSString *)accessToken
  88. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  89. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  90. NS_DESIGNATED_INITIALIZER;
  91. @end
  92. @implementation FIRGetOOBConfirmationCodeRequest
  93. /** @var requestTypeStringValueForRequestType:
  94. @brief Returns the string equivilent for an @c FIRGetOOBConfirmationCodeRequestType value.
  95. */
  96. + (NSString *)requestTypeStringValueForRequestType:
  97. (FIRGetOOBConfirmationCodeRequestType)requestType {
  98. switch (requestType) {
  99. case FIRGetOOBConfirmationCodeRequestTypePasswordReset:
  100. return kPasswordResetRequestTypeValue;
  101. case FIRGetOOBConfirmationCodeRequestTypeVerifyEmail:
  102. return kVerifyEmailRequestTypeValue;
  103. case FIRGetOOBConfirmationCodeRequestTypeEmailLink:
  104. return kEmailLinkSignInTypeValue;
  105. // No default case so that we get a compiler warning if a new value was added to the enum.
  106. }
  107. }
  108. + (FIRGetOOBConfirmationCodeRequest *)
  109. passwordResetRequestWithEmail:(NSString *)email
  110. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  111. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  112. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypePasswordReset
  113. email:email
  114. accessToken:nil
  115. actionCodeSettings:actionCodeSettings
  116. requestConfiguration:requestConfiguration];
  117. }
  118. + (FIRGetOOBConfirmationCodeRequest *)
  119. verifyEmailRequestWithAccessToken:(NSString *)accessToken
  120. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  121. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  122. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeVerifyEmail
  123. email:nil
  124. accessToken:accessToken
  125. actionCodeSettings:actionCodeSettings
  126. requestConfiguration:requestConfiguration];
  127. }
  128. + (FIRGetOOBConfirmationCodeRequest *)
  129. signInWithEmailLinkRequest:(NSString *)email
  130. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  131. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  132. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeEmailLink
  133. email:email
  134. accessToken:nil
  135. actionCodeSettings:actionCodeSettings
  136. requestConfiguration:requestConfiguration];
  137. }
  138. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  139. email:(nullable NSString *)email
  140. accessToken:(nullable NSString *)accessToken
  141. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  142. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  143. self = [super initWithEndpoint:kGetOobConfirmationCodeEndpoint
  144. requestConfiguration:requestConfiguration];
  145. if (self) {
  146. _requestType = requestType;
  147. _email = email;
  148. _accessToken = accessToken;
  149. _continueURL = actionCodeSettings.URL.absoluteString;
  150. _iOSBundleID = actionCodeSettings.iOSBundleID;
  151. _androidPackageName = actionCodeSettings.androidPackageName;
  152. _androidMinimumVersion = actionCodeSettings.androidMinimumVersion;
  153. _androidInstallApp = actionCodeSettings.androidInstallIfNotAvailable;
  154. _handleCodeInApp = actionCodeSettings.handleCodeInApp;
  155. }
  156. return self;
  157. }
  158. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  159. NSMutableDictionary *body = [@{
  160. kRequestTypeKey : [[self class] requestTypeStringValueForRequestType:_requestType]
  161. } mutableCopy];
  162. // For password reset requests, we only need an email address in addition to the already required
  163. // fields.
  164. if (_requestType == FIRGetOOBConfirmationCodeRequestTypePasswordReset) {
  165. body[kEmailKey] = _email;
  166. }
  167. // For verify email requests, we only need an STS Access Token in addition to the already required
  168. // fields.
  169. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeVerifyEmail) {
  170. body[kIDTokenKey] = _accessToken;
  171. }
  172. // For email sign-in link requests, we only need an email address in addition to the already
  173. // required fields.
  174. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeEmailLink) {
  175. body[kEmailKey] = _email;
  176. }
  177. if (_continueURL) {
  178. body[kContinueURLKey] = _continueURL;
  179. }
  180. if (_iOSBundleID) {
  181. body[kIosBundleIDKey] = _iOSBundleID;
  182. }
  183. if (_androidPackageName) {
  184. body[kAndroidPackageNameKey] = _androidPackageName;
  185. }
  186. if (_androidMinimumVersion) {
  187. body[kAndroidMinimumVersionKey] = _androidMinimumVersion;
  188. }
  189. if (_androidInstallApp) {
  190. body[kAndroidInstallAppKey] = @YES;
  191. }
  192. if (_handleCodeInApp) {
  193. body[kCanHandleCodeInAppKey] = @YES;
  194. }
  195. return body;
  196. }
  197. @end