FIRGetOOBConfirmationCodeRequest.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. @interface FIRGetOOBConfirmationCodeRequest ()
  93. /** @fn initWithRequestType:email:APIKey:
  94. @brief Designated initializer.
  95. @param requestType The types of OOB Confirmation Code to request.
  96. @param email The email of the user.
  97. @param newEmail The email of the user to be updated.
  98. @param accessToken The STS Access Token of the currently signed in user.
  99. @param actionCodeSettings An object of FIRActionCodeSettings which specifies action code
  100. settings to be applied to the OOB code request.
  101. @param requestConfiguration An object containing configurations to be added to the request.
  102. */
  103. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  104. email:(nullable NSString *)email
  105. newEmail:(nullable NSString *)newEmail
  106. accessToken:(nullable NSString *)accessToken
  107. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  108. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  109. NS_DESIGNATED_INITIALIZER;
  110. @end
  111. @implementation FIRGetOOBConfirmationCodeRequest
  112. /** @var requestTypeStringValueForRequestType:
  113. @brief Returns the string equivilent for an @c FIRGetOOBConfirmationCodeRequestType value.
  114. */
  115. + (NSString *)requestTypeStringValueForRequestType:
  116. (FIRGetOOBConfirmationCodeRequestType)requestType {
  117. switch (requestType) {
  118. case FIRGetOOBConfirmationCodeRequestTypePasswordReset:
  119. return kPasswordResetRequestTypeValue;
  120. case FIRGetOOBConfirmationCodeRequestTypeVerifyEmail:
  121. return kVerifyEmailRequestTypeValue;
  122. case FIRGetOOBConfirmationCodeRequestTypeEmailLink:
  123. return kEmailLinkSignInTypeValue;
  124. case FIRGetOOBConfirmationCodeRequestTypeVerifyBeforeUpdateEmail:
  125. return kVerifyBeforeUpdateEmailRequestTypeValue;
  126. // No default case so that we get a compiler warning if a new value was added to the enum.
  127. }
  128. }
  129. + (nullable FIRGetOOBConfirmationCodeRequest *)
  130. passwordResetRequestWithEmail:(NSString *)email
  131. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  132. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  133. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypePasswordReset
  134. email:email
  135. newEmail:nil
  136. accessToken:nil
  137. actionCodeSettings:actionCodeSettings
  138. requestConfiguration:requestConfiguration];
  139. }
  140. + (nullable FIRGetOOBConfirmationCodeRequest *)
  141. verifyEmailRequestWithAccessToken:(NSString *)accessToken
  142. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  143. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  144. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeVerifyEmail
  145. email:nil
  146. newEmail:nil
  147. accessToken:accessToken
  148. actionCodeSettings:actionCodeSettings
  149. requestConfiguration:requestConfiguration];
  150. }
  151. + (nullable FIRGetOOBConfirmationCodeRequest *)
  152. signInWithEmailLinkRequest:(NSString *)email
  153. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  154. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  155. return [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeEmailLink
  156. email:email
  157. newEmail:nil
  158. accessToken:nil
  159. actionCodeSettings:actionCodeSettings
  160. requestConfiguration:requestConfiguration];
  161. }
  162. + (nullable FIRGetOOBConfirmationCodeRequest *)
  163. verifyBeforeUpdateEmailWithAccessToken:(NSString *)accessToken
  164. newEmail:(NSString *)newEmail
  165. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  166. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  167. return
  168. [[self alloc] initWithRequestType:FIRGetOOBConfirmationCodeRequestTypeVerifyBeforeUpdateEmail
  169. email:nil
  170. newEmail:newEmail
  171. accessToken:accessToken
  172. actionCodeSettings:actionCodeSettings
  173. requestConfiguration:requestConfiguration];
  174. }
  175. - (nullable instancetype)initWithRequestType:(FIRGetOOBConfirmationCodeRequestType)requestType
  176. email:(nullable NSString *)email
  177. newEmail:(nullable NSString *)newEmail
  178. accessToken:(nullable NSString *)accessToken
  179. actionCodeSettings:(nullable FIRActionCodeSettings *)actionCodeSettings
  180. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  181. self = [super initWithEndpoint:kGetOobConfirmationCodeEndpoint
  182. requestConfiguration:requestConfiguration];
  183. if (self) {
  184. _requestType = requestType;
  185. _email = email;
  186. _updatedEmail = newEmail;
  187. _accessToken = accessToken;
  188. _continueURL = actionCodeSettings.URL.absoluteString;
  189. _iOSBundleID = actionCodeSettings.iOSBundleID;
  190. _androidPackageName = actionCodeSettings.androidPackageName;
  191. _androidMinimumVersion = actionCodeSettings.androidMinimumVersion;
  192. _androidInstallApp = actionCodeSettings.androidInstallIfNotAvailable;
  193. _handleCodeInApp = actionCodeSettings.handleCodeInApp;
  194. _dynamicLinkDomain = actionCodeSettings.dynamicLinkDomain;
  195. }
  196. return self;
  197. }
  198. - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)error {
  199. NSMutableDictionary *body =
  200. [@{kRequestTypeKey : [[self class] requestTypeStringValueForRequestType:_requestType]}
  201. mutableCopy];
  202. // For password reset requests, we only need an email address in addition to the already required
  203. // fields.
  204. if (_requestType == FIRGetOOBConfirmationCodeRequestTypePasswordReset) {
  205. body[kEmailKey] = _email;
  206. }
  207. // For verify email requests, we only need an STS Access Token in addition to the already required
  208. // fields.
  209. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeVerifyEmail) {
  210. body[kIDTokenKey] = _accessToken;
  211. }
  212. // For email sign-in link requests, we only need an email address in addition to the already
  213. // required fields.
  214. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeEmailLink) {
  215. body[kEmailKey] = _email;
  216. }
  217. // For email sign-in link requests, we only need an STS Access Token, a new email address in
  218. // addition to the already required fields.
  219. if (_requestType == FIRGetOOBConfirmationCodeRequestTypeVerifyBeforeUpdateEmail) {
  220. body[kNewEmailKey] = _updatedEmail;
  221. body[kIDTokenKey] = _accessToken;
  222. }
  223. if (_continueURL) {
  224. body[kContinueURLKey] = _continueURL;
  225. }
  226. if (_iOSBundleID) {
  227. body[kIosBundleIDKey] = _iOSBundleID;
  228. }
  229. if (_androidPackageName) {
  230. body[kAndroidPackageNameKey] = _androidPackageName;
  231. }
  232. if (_androidMinimumVersion) {
  233. body[kAndroidMinimumVersionKey] = _androidMinimumVersion;
  234. }
  235. if (_androidInstallApp) {
  236. body[kAndroidInstallAppKey] = @YES;
  237. }
  238. if (_handleCodeInApp) {
  239. body[kCanHandleCodeInAppKey] = @YES;
  240. }
  241. if (_dynamicLinkDomain) {
  242. body[kDynamicLinkDomainKey] = _dynamicLinkDomain;
  243. }
  244. if (self.tenantID) {
  245. body[kTenantIDKey] = self.tenantID;
  246. }
  247. return body;
  248. }
  249. @end
  250. NS_ASSUME_NONNULL_END