FIRGetOOBConfirmationCodeRequest.m 9.2 KB

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