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.

288 lines
11 KiB

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