FIRGetOOBConfirmationCodeRequest.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeRequest.h"
  17. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRActionCodeSettings.h"
  18. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  19. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. /** @var kEndpoint
  22. @brief The getOobConfirmationCode endpoint name.
  23. */
  24. static NSString *const kGetOobConfirmationCodeEndpoint = @"getOobConfirmationCode";
  25. /** @var kRequestTypeKey
  26. @brief The name of the required "requestType" property in the request.
  27. */
  28. static NSString *const kRequestTypeKey = @"requestType";
  29. /** @var kEmailKey
  30. @brief The name of the "email" property in the request.
  31. */
  32. static NSString *const kEmailKey = @"email";
  33. /** @var kNewEmailKey
  34. @brief The name of the "newEmail" property in the request.
  35. */
  36. static NSString *const kNewEmailKey = @"newEmail";
  37. /** @var kIDTokenKey
  38. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  39. despite it's confusing (backwards compatiable) parameter name.
  40. */
  41. static NSString *const kIDTokenKey = @"idToken";
  42. /** @var kContinueURLKey
  43. @brief The key for the "continue URL" value in the request.
  44. */
  45. static NSString *const kContinueURLKey = @"continueUrl";
  46. /** @var kIosBundeIDKey
  47. @brief The key for the "iOS Bundle Identifier" value in the request.
  48. */
  49. static NSString *const kIosBundleIDKey = @"iOSBundleId";
  50. /** @var kAndroidPackageNameKey
  51. @brief The key for the "Android Package Name" value in the request.
  52. */
  53. static NSString *const kAndroidPackageNameKey = @"androidPackageName";
  54. /** @var kAndroidInstallAppKey
  55. @brief The key for the request parameter indicating whether the android app should be installed
  56. or not.
  57. */
  58. static NSString *const kAndroidInstallAppKey = @"androidInstallApp";
  59. /** @var kAndroidMinimumVersionKey
  60. @brief The key for the "minimum Android version supported" value in the request.
  61. */
  62. static NSString *const kAndroidMinimumVersionKey = @"androidMinimumVersion";
  63. /** @var kCanHandleCodeInAppKey
  64. @brief The key for the request parameter indicating whether the action code can be handled in
  65. the app or not.
  66. */
  67. static NSString *const kCanHandleCodeInAppKey = @"canHandleCodeInApp";
  68. /** @var kDynamicLinkDomainKey
  69. @brief The key for the "dynamic link domain" value in the request.
  70. */
  71. static NSString *const kDynamicLinkDomainKey = @"dynamicLinkDomain";
  72. /** @var kPasswordResetRequestTypeValue
  73. @brief The value for the "PASSWORD_RESET" request type.
  74. */
  75. static NSString *const kPasswordResetRequestTypeValue = @"PASSWORD_RESET";
  76. /** @var kEmailLinkSignInTypeValue
  77. @brief The value for the "EMAIL_SIGNIN" request type.
  78. */
  79. static NSString *const kEmailLinkSignInTypeValue = @"EMAIL_SIGNIN";
  80. /** @var kVerifyEmailRequestTypeValue
  81. @brief The value for the "VERIFY_EMAIL" request type.
  82. */
  83. static NSString *const kVerifyEmailRequestTypeValue = @"VERIFY_EMAIL";
  84. /** @var kVerifyBeforeUpdateEmailRequestTypeValue
  85. @brief The value for the "VERIFY_AND_CHANGE_EMAIL" request type.
  86. */
  87. static NSString *const kVerifyBeforeUpdateEmailRequestTypeValue = @"VERIFY_AND_CHANGE_EMAIL";
  88. /** @var kTenantIDKey
  89. @brief The key for the tenant id value in the request.
  90. */
  91. static NSString *const kTenantIDKey = @"tenantId";
  92. /** @var kCaptchaResponseKey
  93. @brief The key for the "captchaResponse" value in the request.
  94. */
  95. static NSString *const kCaptchaResponseKey = @"captchaResp";
  96. /** @var kClientType
  97. @brief The key for the "clientType" value in the request.
  98. */
  99. static NSString *const kClientType = @"clientType";
  100. /** @var kRecaptchaVersion
  101. @brief The key for the "recaptchaVersion" value in the request.
  102. */
  103. static NSString *const kRecaptchaVersion = @"recaptchaVersion";
  104. @interface FIRGetOOBConfirmationCodeRequest ()
  105. /** @fn initWithRequestType:email:APIKey:
  106. @brief Designated initializer.
  107. @param requestType The types of OOB Confirmation Code to request.
  108. @param email The email of the user.
  109. @param newEmail The email of the user to be updated.
  110. @param accessToken The STS Access Token of the currently signed in user.
  111. @param actionCodeSettings An object of FIRActionCodeSettings which specifies action code
  112. settings to be applied to the OOB code request.
  113. @param requestConfiguration An object containing configurations to be added to the request.
  114. */
  115. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  116. email:(nullable NSString *)email
  117. newEmail:(nullable NSString *)newEmail
  118. accessToken:(nullable NSString *)accessToken
  119. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  120. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  121. NS_DESIGNATED_INITIALIZER;
  122. @end
  123. @implementation FIRGetOOBConfirmationCodeRequest
  124. /** @var requestTypeStringValueForRequestType:
  125. @brief Returns the string equivilent for an @c FIRGetOOBConfirmationCodeRequestType value.
  126. */
  127. + (NSString *)requestTypeStringValueForRequestType:
  128. (FIRGetOOBConfirmationCodeRequestType)requestType {
  129. switch (requestType) {
  130. case FIRGetOOBConfirmationCodeRequestTypePasswordReset:
  131. return kPasswordResetRequestTypeValue;
  132. case FIRGetOOBConfirmationCodeRequestTypeVerifyEmail:
  133. return kVerifyEmailRequestTypeValue;
  134. case FIRGetOOBConfirmationCodeRequestTypeEmailLink:
  135. return kEmailLinkSignInTypeValue;
  136. case FIRGetOOBConfirmationCodeRequestTypeVerifyBeforeUpdateEmail:
  137. return kVerifyBeforeUpdateEmailRequestTypeValue;
  138. // No default case so that we get a compiler warning if a new value was added to the enum.
  139. }
  140. }
  141. + (nullable FIRGetOOBConfirmationCodeRequest *)
  142. passwordResetRequestWithEmail:(NSString *)email
  143. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  144. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  145. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypePasswordReset
  146. email:email
  147. newEmail:nil
  148. accessToken:nil
  149. actionCodeSettings:actionCodeSettings
  150. requestConfiguration:requestConfiguration];
  151. }
  152. + (nullable FIRGetOOBConfirmationCodeRequest *)
  153. verifyEmailRequestWithAccessToken:(NSString *)accessToken
  154. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  155. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  156. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeVerifyEmail
  157. email:nil
  158. newEmail:nil
  159. accessToken:accessToken
  160. actionCodeSettings:actionCodeSettings
  161. requestConfiguration:requestConfiguration];
  162. }
  163. + (nullable FIRGetOOBConfirmationCodeRequest *)
  164. signInWithEmailLinkRequest:(NSString *)email
  165. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  166. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  167. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeEmailLink
  168. email:email
  169. newEmail:nil
  170. accessToken:nil
  171. actionCodeSettings:actionCodeSettings
  172. requestConfiguration:requestConfiguration];
  173. }
  174. + (nullable FIRGetOOBConfirmationCodeRequest *)
  175. verifyBeforeUpdateEmailWithAccessToken:(NSString *)accessToken
  176. newEmail:(NSString *)newEmail
  177. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  178. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  179. return
  180. [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeVerifyBeforeUpdateEmail
  181. email:nil
  182. newEmail:newEmail
  183. accessToken:accessToken
  184. actionCodeSettings:actionCodeSettings
  185. requestConfiguration:requestConfiguration];
  186. }
  187. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  188. email:(nullable NSString *)email
  189. newEmail:(nullable NSString *)newEmail
  190. accessToken:(nullable NSString *)accessToken
  191. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  192. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  193. self = [super initWithEndpoint:kGetOobConfirmationCodeEndpoint
  194. requestConfiguration:requestConfiguration];
  195. if (self) {
  196. _requestType = requestType;
  197. _email = email;
  198. _updatedEmail = newEmail;
  199. _accessToken = accessToken;
  200. _continueURL = actionCodeSettings.URL.absoluteString;
  201. _iOSBundleID = actionCodeSettings.iOSBundleID;
  202. _androidPackageName = actionCodeSettings.androidPackageName;
  203. _androidMinimumVersion = actionCodeSettings.androidMinimumVersion;
  204. _androidInstallApp = actionCodeSettings.androidInstallIfNotAvailable;
  205. _handleCodeInApp = actionCodeSettings.handleCodeInApp;
  206. _dynamicLinkDomain = actionCodeSettings.dynamicLinkDomain;
  207. }
  208. return self;
  209. }
  210. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  211. NSMutableDictionary *body =
  212. [@{kRequestTypeKey : [[self class] requestTypeStringValueForRequestType:_requestType]}
  213. mutableCopy];
  214. // For password reset requests, we only need an email address in addition to the already required
  215. // fields.
  216. if (_requestType == FIRGetOOBConfirmationCodeRequestTypePasswordReset) {
  217. body[kEmailKey] = _email;
  218. }
  219. // For verify email requests, we only need an STS Access Token in addition to the already required
  220. // fields.
  221. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeVerifyEmail) {
  222. body[kIDTokenKey] = _accessToken;
  223. }
  224. // For email sign-in link requests, we only need an email address in addition to the already
  225. // required fields.
  226. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeEmailLink) {
  227. body[kEmailKey] = _email;
  228. }
  229. // For email sign-in link requests, we only need an STS Access Token, a new email address in
  230. // addition to the already required fields.
  231. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeVerifyBeforeUpdateEmail) {
  232. body[kNewEmailKey] = _updatedEmail;
  233. body[kIDTokenKey] = _accessToken;
  234. }
  235. if (_continueURL) {
  236. body[kContinueURLKey] = _continueURL;
  237. }
  238. if (_iOSBundleID) {
  239. body[kIosBundleIDKey] = _iOSBundleID;
  240. }
  241. if (_androidPackageName) {
  242. body[kAndroidPackageNameKey] = _androidPackageName;
  243. }
  244. if (_androidMinimumVersion) {
  245. body[kAndroidMinimumVersionKey] = _androidMinimumVersion;
  246. }
  247. if (_androidInstallApp) {
  248. body[kAndroidInstallAppKey] = @YES;
  249. }
  250. if (_handleCodeInApp) {
  251. body[kCanHandleCodeInAppKey] = @YES;
  252. }
  253. if (_dynamicLinkDomain) {
  254. body[kDynamicLinkDomainKey] = _dynamicLinkDomain;
  255. }
  256. if (_captchaResponse) {
  257. body[kCaptchaResponseKey] = _captchaResponse;
  258. }
  259. if (self.clientType) {
  260. body[kClientType] = self.clientType;
  261. }
  262. if (_recaptchaVersion) {
  263. body[kRecaptchaVersion] = _recaptchaVersion;
  264. }
  265. if (self.tenantID) {
  266. body[kTenantIDKey] = self.tenantID;
  267. }
  268. return body;
  269. }
  270. - (void)injectRecaptchaFields:(NSString *_Nullable)recaptchaResponse
  271. recaptchaVersion:(NSString *)recaptchaVersion {
  272. _captchaResponse = recaptchaResponse;
  273. _recaptchaVersion = recaptchaVersion;
  274. }
  275. @end
  276. NS_ASSUME_NONNULL_END