|
| 1 | +/*! |
| 2 | + * Copyright 2018 Google Inc. |
| 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 | + |
| 17 | +import { FirebaseProjectManagementError } from '../utils/error'; |
| 18 | +import * as validator from '../utils/validator'; |
| 19 | +import { ProjectManagementRequestHandler, assertServerResponse } from './project-management-api-request'; |
| 20 | + |
| 21 | +export class AndroidApp { |
| 22 | + constructor( |
| 23 | + public readonly appId: string, |
| 24 | + private readonly requestHandler: ProjectManagementRequestHandler) { |
| 25 | + if (!validator.isNonEmptyString(appId)) { |
| 26 | + throw new FirebaseProjectManagementError( |
| 27 | + 'invalid-argument', 'appId must be a non-empty string.'); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public getMetadata(): Promise<AndroidAppMetadata> { |
| 32 | + return this.requestHandler.getAndroidMetadata(this.appId) |
| 33 | + .then((responseData: any) => { |
| 34 | + assertServerResponse( |
| 35 | + validator.isNonNullObject(responseData), |
| 36 | + responseData, |
| 37 | + 'getMetadata()\'s responseData must be a non-null object.'); |
| 38 | + |
| 39 | + const requiredFieldsList = ['name', 'appId', 'projectId', 'packageName']; |
| 40 | + requiredFieldsList.forEach((requiredField) => { |
| 41 | + assertServerResponse( |
| 42 | + validator.isNonEmptyString(responseData[requiredField]), |
| 43 | + responseData, |
| 44 | + `getMetadata()\'s responseData.${requiredField} must be a non-empty string.`); |
| 45 | + }); |
| 46 | + |
| 47 | + const metadata: AndroidAppMetadata = { |
| 48 | + resourceName: responseData.name, |
| 49 | + appId: responseData.appId, |
| 50 | + displayName: responseData.displayName || null, |
| 51 | + projectId: responseData.projectId, |
| 52 | + packageName: responseData.packageName, |
| 53 | + }; |
| 54 | + return metadata; |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + public setDisplayName(newDisplayName: string): Promise<void> { |
| 59 | + return this.requestHandler.setAndroidDisplayName(this.appId, newDisplayName); |
| 60 | + } |
| 61 | + |
| 62 | + public getShaCertificates(): Promise<ShaCertificate[]> { |
| 63 | + return this.requestHandler.getAndroidShaCertificates(this.appId) |
| 64 | + .then((responseData: any) => { |
| 65 | + assertServerResponse( |
| 66 | + validator.isNonNullObject(responseData), |
| 67 | + responseData, |
| 68 | + 'getShaCertificates()\'s responseData must be a non-null object.'); |
| 69 | + |
| 70 | + if (!responseData.certificates) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + assertServerResponse( |
| 75 | + validator.isArray(responseData.certificates), |
| 76 | + responseData, |
| 77 | + '"certificates" field must be present in the getShaCertificates() response data.'); |
| 78 | + |
| 79 | + const requiredFieldsList = ['name', 'shaHash']; |
| 80 | + |
| 81 | + return responseData.certificates.map((certificateJson: any) => { |
| 82 | + requiredFieldsList.forEach((requiredField) => { |
| 83 | + assertServerResponse( |
| 84 | + validator.isNonEmptyString(certificateJson[requiredField]), |
| 85 | + responseData, |
| 86 | + `getShaCertificates()\'s responseData.certificates[].${requiredField} must be a ` |
| 87 | + + `non-empty string.`); |
| 88 | + }); |
| 89 | + |
| 90 | + return new ShaCertificate(certificateJson.shaHash, certificateJson.name); |
| 91 | + }); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + public addShaCertificate(certificateToAdd: ShaCertificate): Promise<void> { |
| 96 | + return this.requestHandler.addAndroidShaCertificate(this.appId, certificateToAdd); |
| 97 | + } |
| 98 | + |
| 99 | + public deleteShaCertificate(certificateToDelete: ShaCertificate): Promise<void> { |
| 100 | + return this.requestHandler.deleteAndroidShaCertificate(certificateToDelete); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return {Promise<string>} A promise that resolves to a UTF-8 JSON string, typically intended to |
| 105 | + * be written to a JSON file. |
| 106 | + */ |
| 107 | + public getConfig(): Promise<string> { |
| 108 | + return this.requestHandler.getAndroidConfig(this.appId) |
| 109 | + .then((responseData: any) => { |
| 110 | + assertServerResponse( |
| 111 | + validator.isNonNullObject(responseData), |
| 112 | + responseData, |
| 113 | + 'getConfig()\'s responseData must be a non-null object.'); |
| 114 | + |
| 115 | + const base64ConfigFileContents = responseData.configFileContents; |
| 116 | + assertServerResponse( |
| 117 | + validator.isBase64String(base64ConfigFileContents), |
| 118 | + responseData, |
| 119 | + `getConfig()\'s responseData.configFileContents must be a base64 string.`); |
| 120 | + |
| 121 | + return Buffer.from(base64ConfigFileContents, 'base64').toString('utf8'); |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export interface AndroidAppMetadata { |
| 127 | + readonly resourceName: string; |
| 128 | + readonly appId: string; |
| 129 | + readonly displayName: string | null; |
| 130 | + readonly projectId: string; |
| 131 | + readonly packageName: string; |
| 132 | +} |
| 133 | + |
| 134 | +export class ShaCertificate { |
| 135 | + public readonly certType: ('sha1' | 'sha256'); |
| 136 | + |
| 137 | + /** |
| 138 | + * Creates a ShaCertificate using the given hash. The ShaCertificate's type (eg. 'sha256') is |
| 139 | + * automatically determined from the hash itself. |
| 140 | + * |
| 141 | + * @param shaHash The sha256 or sha1 hash for this certificate. |
| 142 | + * @param resourceName The Firebase resource name for this certificate. This does not need to be |
| 143 | + * set when creating a new certificate. |
| 144 | + */ |
| 145 | + constructor(public readonly shaHash: string, public readonly resourceName?: string) { |
| 146 | + if (/^[a-fA-F0-9]{40}$/.test(shaHash)) { |
| 147 | + this.certType = 'sha1'; |
| 148 | + } else if (/^[a-fA-F0-9]{64}$/.test(shaHash)) { |
| 149 | + this.certType = 'sha256'; |
| 150 | + } else { |
| 151 | + throw new FirebaseProjectManagementError( |
| 152 | + 'invalid-argument', 'shaHash must be either a sha256 hash or a sha1 hash.'); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments