No Description

FIRGetOOBConfirmationCodeRequest.m 9.3KB

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