project-config.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.ProjectConfig = void 0;
  5. /*!
  6. * Copyright 2022 Google Inc.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. const validator = require("../utils/validator");
  21. const error_1 = require("../utils/error");
  22. const auth_config_1 = require("./auth-config");
  23. const deep_copy_1 = require("../utils/deep-copy");
  24. /**
  25. * Represents a project configuration.
  26. */
  27. class ProjectConfig {
  28. /**
  29. * The multi-factor auth configuration.
  30. */
  31. get multiFactorConfig() {
  32. return this.multiFactorConfig_;
  33. }
  34. /**
  35. * Validates a project config options object. Throws an error on failure.
  36. *
  37. * @param request - The project config options object to validate.
  38. */
  39. static validate(request) {
  40. if (!validator.isNonNullObject(request)) {
  41. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"UpdateProjectConfigRequest" must be a valid non-null object.');
  42. }
  43. const validKeys = {
  44. smsRegionConfig: true,
  45. multiFactorConfig: true,
  46. recaptchaConfig: true,
  47. passwordPolicyConfig: true,
  48. emailPrivacyConfig: true,
  49. };
  50. // Check for unsupported top level attributes.
  51. for (const key in request) {
  52. if (!(key in validKeys)) {
  53. throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, `"${key}" is not a valid UpdateProjectConfigRequest parameter.`);
  54. }
  55. }
  56. // Validate SMS Regions Config if provided.
  57. if (typeof request.smsRegionConfig !== 'undefined') {
  58. auth_config_1.SmsRegionsAuthConfig.validate(request.smsRegionConfig);
  59. }
  60. // Validate Multi Factor Config if provided
  61. if (typeof request.multiFactorConfig !== 'undefined') {
  62. auth_config_1.MultiFactorAuthConfig.validate(request.multiFactorConfig);
  63. }
  64. // Validate reCAPTCHA config attribute.
  65. if (typeof request.recaptchaConfig !== 'undefined') {
  66. auth_config_1.RecaptchaAuthConfig.validate(request.recaptchaConfig);
  67. }
  68. // Validate Password policy Config if provided
  69. if (typeof request.passwordPolicyConfig !== 'undefined') {
  70. auth_config_1.PasswordPolicyAuthConfig.validate(request.passwordPolicyConfig);
  71. }
  72. // Validate Email Privacy Config if provided.
  73. if (typeof request.emailPrivacyConfig !== 'undefined') {
  74. auth_config_1.EmailPrivacyAuthConfig.validate(request.emailPrivacyConfig);
  75. }
  76. }
  77. /**
  78. * Build the corresponding server request for a UpdateProjectConfigRequest object.
  79. * @param configOptions - The properties to convert to a server request.
  80. * @returns The equivalent server request.
  81. *
  82. * @internal
  83. */
  84. static buildServerRequest(configOptions) {
  85. ProjectConfig.validate(configOptions);
  86. const request = {};
  87. if (typeof configOptions.smsRegionConfig !== 'undefined') {
  88. request.smsRegionConfig = configOptions.smsRegionConfig;
  89. }
  90. if (typeof configOptions.multiFactorConfig !== 'undefined') {
  91. request.mfa = auth_config_1.MultiFactorAuthConfig.buildServerRequest(configOptions.multiFactorConfig);
  92. }
  93. if (typeof configOptions.recaptchaConfig !== 'undefined') {
  94. request.recaptchaConfig = configOptions.recaptchaConfig;
  95. }
  96. if (typeof configOptions.passwordPolicyConfig !== 'undefined') {
  97. request.passwordPolicyConfig = auth_config_1.PasswordPolicyAuthConfig.buildServerRequest(configOptions.passwordPolicyConfig);
  98. }
  99. if (typeof configOptions.emailPrivacyConfig !== 'undefined') {
  100. request.emailPrivacyConfig = configOptions.emailPrivacyConfig;
  101. }
  102. return request;
  103. }
  104. /**
  105. * The reCAPTCHA configuration.
  106. */
  107. get recaptchaConfig() {
  108. return this.recaptchaConfig_;
  109. }
  110. /**
  111. * The Project Config object constructor.
  112. *
  113. * @param response - The server side response used to initialize the Project Config object.
  114. * @constructor
  115. * @internal
  116. */
  117. constructor(response) {
  118. if (typeof response.smsRegionConfig !== 'undefined') {
  119. this.smsRegionConfig = response.smsRegionConfig;
  120. }
  121. //Backend API returns "mfa" in case of project config and "mfaConfig" in case of tenant config.
  122. //The SDK exposes it as multiFactorConfig always.
  123. if (typeof response.mfa !== 'undefined') {
  124. this.multiFactorConfig_ = new auth_config_1.MultiFactorAuthConfig(response.mfa);
  125. }
  126. if (typeof response.recaptchaConfig !== 'undefined') {
  127. this.recaptchaConfig_ = new auth_config_1.RecaptchaAuthConfig(response.recaptchaConfig);
  128. }
  129. if (typeof response.passwordPolicyConfig !== 'undefined') {
  130. this.passwordPolicyConfig = new auth_config_1.PasswordPolicyAuthConfig(response.passwordPolicyConfig);
  131. }
  132. if (typeof response.emailPrivacyConfig !== 'undefined') {
  133. this.emailPrivacyConfig = response.emailPrivacyConfig;
  134. }
  135. }
  136. /**
  137. * Returns a JSON-serializable representation of this object.
  138. *
  139. * @returns A JSON-serializable representation of this object.
  140. */
  141. toJSON() {
  142. // JSON serialization
  143. const json = {
  144. smsRegionConfig: (0, deep_copy_1.deepCopy)(this.smsRegionConfig),
  145. multiFactorConfig: (0, deep_copy_1.deepCopy)(this.multiFactorConfig),
  146. recaptchaConfig: this.recaptchaConfig_?.toJSON(),
  147. passwordPolicyConfig: (0, deep_copy_1.deepCopy)(this.passwordPolicyConfig),
  148. emailPrivacyConfig: (0, deep_copy_1.deepCopy)(this.emailPrivacyConfig),
  149. };
  150. if (typeof json.smsRegionConfig === 'undefined') {
  151. delete json.smsRegionConfig;
  152. }
  153. if (typeof json.multiFactorConfig === 'undefined') {
  154. delete json.multiFactorConfig;
  155. }
  156. if (typeof json.recaptchaConfig === 'undefined') {
  157. delete json.recaptchaConfig;
  158. }
  159. if (typeof json.passwordPolicyConfig === 'undefined') {
  160. delete json.passwordPolicyConfig;
  161. }
  162. if (typeof json.emailPrivacyConfig === 'undefined') {
  163. delete json.emailPrivacyConfig;
  164. }
  165. return json;
  166. }
  167. }
  168. exports.ProjectConfig = ProjectConfig;