android-app.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * Copyright 2018 Google Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.ShaCertificate = exports.AndroidApp = void 0;
  20. const error_1 = require("../utils/error");
  21. const validator = require("../utils/validator");
  22. const project_management_api_request_internal_1 = require("./project-management-api-request-internal");
  23. const app_metadata_1 = require("./app-metadata");
  24. /**
  25. * A reference to a Firebase Android app.
  26. *
  27. * Do not call this constructor directly. Instead, use {@link ProjectManagement.androidApp}.
  28. */
  29. class AndroidApp {
  30. /**
  31. * @internal
  32. */
  33. constructor(appId, requestHandler) {
  34. this.appId = appId;
  35. this.requestHandler = requestHandler;
  36. if (!validator.isNonEmptyString(appId)) {
  37. throw new error_1.FirebaseProjectManagementError('invalid-argument', 'appId must be a non-empty string.');
  38. }
  39. this.resourceName = `projects/-/androidApps/${appId}`;
  40. }
  41. /**
  42. * Retrieves metadata about this Android app.
  43. *
  44. * @returns A promise that resolves to the retrieved metadata about this Android app.
  45. */
  46. getMetadata() {
  47. return this.requestHandler.getResource(this.resourceName)
  48. .then((responseData) => {
  49. (0, project_management_api_request_internal_1.assertServerResponse)(validator.isNonNullObject(responseData), responseData, 'getMetadata()\'s responseData must be a non-null object.');
  50. const requiredFieldsList = ['name', 'appId', 'projectId', 'packageName'];
  51. requiredFieldsList.forEach((requiredField) => {
  52. (0, project_management_api_request_internal_1.assertServerResponse)(validator.isNonEmptyString(responseData[requiredField]), responseData, `getMetadata()'s responseData.${requiredField} must be a non-empty string.`);
  53. });
  54. const metadata = {
  55. platform: app_metadata_1.AppPlatform.ANDROID,
  56. resourceName: responseData.name,
  57. appId: responseData.appId,
  58. displayName: responseData.displayName || null,
  59. projectId: responseData.projectId,
  60. packageName: responseData.packageName,
  61. };
  62. return metadata;
  63. });
  64. }
  65. /**
  66. * Sets the optional user-assigned display name of the app.
  67. *
  68. * @param newDisplayName - The new display name to set.
  69. *
  70. * @returns A promise that resolves when the display name has been set.
  71. */
  72. setDisplayName(newDisplayName) {
  73. return this.requestHandler.setDisplayName(this.resourceName, newDisplayName);
  74. }
  75. /**
  76. * Gets the list of SHA certificates associated with this Android app in Firebase.
  77. *
  78. * @returns The list of SHA-1 and SHA-256 certificates associated with this Android app in
  79. * Firebase.
  80. */
  81. getShaCertificates() {
  82. return this.requestHandler.getAndroidShaCertificates(this.resourceName)
  83. .then((responseData) => {
  84. (0, project_management_api_request_internal_1.assertServerResponse)(validator.isNonNullObject(responseData), responseData, 'getShaCertificates()\'s responseData must be a non-null object.');
  85. if (!responseData.certificates) {
  86. return [];
  87. }
  88. (0, project_management_api_request_internal_1.assertServerResponse)(validator.isArray(responseData.certificates), responseData, '"certificates" field must be present in the getShaCertificates() response data.');
  89. const requiredFieldsList = ['name', 'shaHash'];
  90. return responseData.certificates.map((certificateJson) => {
  91. requiredFieldsList.forEach((requiredField) => {
  92. (0, project_management_api_request_internal_1.assertServerResponse)(validator.isNonEmptyString(certificateJson[requiredField]), responseData, `getShaCertificates()'s responseData.certificates[].${requiredField} must be a `
  93. + 'non-empty string.');
  94. });
  95. return new ShaCertificate(certificateJson.shaHash, certificateJson.name);
  96. });
  97. });
  98. }
  99. /**
  100. * Adds the given SHA certificate to this Android app.
  101. *
  102. * @param certificateToAdd - The SHA certificate to add.
  103. *
  104. * @returns A promise that resolves when the given certificate
  105. * has been added to the Android app.
  106. */
  107. addShaCertificate(certificateToAdd) {
  108. return this.requestHandler.addAndroidShaCertificate(this.resourceName, certificateToAdd);
  109. }
  110. /**
  111. * Deletes the specified SHA certificate from this Android app.
  112. *
  113. * @param certificateToDelete - The SHA certificate to delete.
  114. *
  115. * @returns A promise that resolves when the specified
  116. * certificate has been removed from the Android app.
  117. */
  118. deleteShaCertificate(certificateToDelete) {
  119. if (!certificateToDelete.resourceName) {
  120. throw new error_1.FirebaseProjectManagementError('invalid-argument', 'Specified certificate does not include a resourceName. (Use AndroidApp.getShaCertificates() to retrieve ' +
  121. 'certificates with a resourceName.');
  122. }
  123. return this.requestHandler.deleteResource(certificateToDelete.resourceName);
  124. }
  125. /**
  126. * Gets the configuration artifact associated with this app.
  127. *
  128. * @returns A promise that resolves to the Android app's
  129. * Firebase config file, in UTF-8 string format. This string is typically
  130. * intended to be written to a JSON file that gets shipped with your Android
  131. * app.
  132. */
  133. getConfig() {
  134. return this.requestHandler.getConfig(this.resourceName)
  135. .then((responseData) => {
  136. (0, project_management_api_request_internal_1.assertServerResponse)(validator.isNonNullObject(responseData), responseData, 'getConfig()\'s responseData must be a non-null object.');
  137. const base64ConfigFileContents = responseData.configFileContents;
  138. (0, project_management_api_request_internal_1.assertServerResponse)(validator.isBase64String(base64ConfigFileContents), responseData, 'getConfig()\'s responseData.configFileContents must be a base64 string.');
  139. return Buffer.from(base64ConfigFileContents, 'base64').toString('utf8');
  140. });
  141. }
  142. }
  143. exports.AndroidApp = AndroidApp;
  144. /**
  145. * A SHA-1 or SHA-256 certificate.
  146. *
  147. * Do not call this constructor directly. Instead, use
  148. * [`projectManagement.shaCertificate()`](projectManagement.ProjectManagement#shaCertificate).
  149. */
  150. class ShaCertificate {
  151. /**
  152. * Creates a ShaCertificate using the given hash. The ShaCertificate's type (eg. 'sha256') is
  153. * automatically determined from the hash itself.
  154. *
  155. * @param shaHash - The sha256 or sha1 hash for this certificate.
  156. * @example
  157. * ```javascript
  158. * var shaHash = shaCertificate.shaHash;
  159. * ```
  160. * @param resourceName - The Firebase resource name for this certificate. This does not need to be
  161. * set when creating a new certificate.
  162. * @example
  163. * ```javascript
  164. * var resourceName = shaCertificate.resourceName;
  165. * ```
  166. *
  167. * @internal
  168. */
  169. constructor(shaHash, resourceName) {
  170. this.shaHash = shaHash;
  171. this.resourceName = resourceName;
  172. if (/^[a-fA-F0-9]{40}$/.test(shaHash)) {
  173. this.certType = 'sha1';
  174. }
  175. else if (/^[a-fA-F0-9]{64}$/.test(shaHash)) {
  176. this.certType = 'sha256';
  177. }
  178. else {
  179. throw new error_1.FirebaseProjectManagementError('invalid-argument', 'shaHash must be either a sha256 hash or a sha1 hash.');
  180. }
  181. }
  182. }
  183. exports.ShaCertificate = ShaCertificate;