|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.installations.remote; |
| 16 | + |
| 17 | +import androidx.annotation.NonNull; |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.URL; |
| 20 | +import java.util.zip.GZIPOutputStream; |
| 21 | +import javax.net.ssl.HttpsURLConnection; |
| 22 | +import org.json.JSONException; |
| 23 | +import org.json.JSONObject; |
| 24 | + |
| 25 | +/** Http client that sends request to Firebase Installations backend API. */ |
| 26 | +public class FirebaseInstallationServiceClient { |
| 27 | + private static final String FIREBASE_INSTALLATIONS_API_DOMAIN = |
| 28 | + "firebaseinstallations.googleapis.com"; |
| 29 | + private static final String CREATE_REQUEST_RESOURCE_NAME_FORMAT = "projects/%s/installations"; |
| 30 | + private static final String GENERATE_AUTH_TOKEN_REQUEST_RESOURCE_NAME_FORMAT = |
| 31 | + "projects/%s/installations/%s/auth:generate"; |
| 32 | + private static final String DELETE_REQUEST_RESOURCE_NAME_FORMAT = "projects/%s/installations/%s"; |
| 33 | + private static final String FIREBASE_INSTALLATIONS_API_VERSION = "v1"; |
| 34 | + private static final String FIREBASE_INSTALLATION_AUTH_VERSION = "FIS_V2"; |
| 35 | + |
| 36 | + private static final String CONTENT_TYPE_HEADER_KEY = "Content-Type"; |
| 37 | + private static final String JSON_CONTENT_TYPE = "application/json"; |
| 38 | + private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding"; |
| 39 | + private static final String GZIP_CONTENT_ENCODING = "gzip"; |
| 40 | + |
| 41 | + public enum Code { |
| 42 | + OK, |
| 43 | + |
| 44 | + HTTP_CLIENT_ERROR, |
| 45 | + |
| 46 | + CONFLICT, |
| 47 | + |
| 48 | + NETWORK_ERROR, |
| 49 | + |
| 50 | + SERVER_ERROR, |
| 51 | + |
| 52 | + UNAUTHORIZED, |
| 53 | + } |
| 54 | + |
| 55 | + @NonNull |
| 56 | + public Code createFirebaseInstallation( |
| 57 | + long projectNumber, |
| 58 | + @NonNull String apiKey, |
| 59 | + @NonNull String firebaseInstallationId, |
| 60 | + @NonNull String appId) { |
| 61 | + String resourceName = String.format(CREATE_REQUEST_RESOURCE_NAME_FORMAT, projectNumber); |
| 62 | + try { |
| 63 | + URL url = |
| 64 | + new URL( |
| 65 | + String.format( |
| 66 | + "https://%s/%s/%s?key=%s", |
| 67 | + FIREBASE_INSTALLATIONS_API_DOMAIN, |
| 68 | + FIREBASE_INSTALLATIONS_API_VERSION, |
| 69 | + resourceName, |
| 70 | + apiKey)); |
| 71 | + |
| 72 | + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); |
| 73 | + httpsURLConnection.setDoOutput(true); |
| 74 | + httpsURLConnection.setRequestMethod("POST"); |
| 75 | + httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); |
| 76 | + httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); |
| 77 | + GZIPOutputStream gzipOutputStream = |
| 78 | + new GZIPOutputStream(httpsURLConnection.getOutputStream()); |
| 79 | + try { |
| 80 | + gzipOutputStream.write( |
| 81 | + buildCreateFirebaseInstallationRequestBody(firebaseInstallationId, appId) |
| 82 | + .toString() |
| 83 | + .getBytes("UTF-8")); |
| 84 | + } catch (JSONException e) { |
| 85 | + throw new IllegalStateException(e); |
| 86 | + } finally { |
| 87 | + gzipOutputStream.close(); |
| 88 | + } |
| 89 | + |
| 90 | + int httpResponseCode = httpsURLConnection.getResponseCode(); |
| 91 | + switch (httpResponseCode) { |
| 92 | + case 200: |
| 93 | + return Code.OK; |
| 94 | + case 401: |
| 95 | + return Code.UNAUTHORIZED; |
| 96 | + case 409: |
| 97 | + return Code.CONFLICT; |
| 98 | + default: |
| 99 | + return Code.SERVER_ERROR; |
| 100 | + } |
| 101 | + } catch (IOException e) { |
| 102 | + return Code.NETWORK_ERROR; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static JSONObject buildCreateFirebaseInstallationRequestBody(String fid, String appId) |
| 107 | + throws JSONException { |
| 108 | + JSONObject firebaseInstallationData = new JSONObject(); |
| 109 | + firebaseInstallationData.put("fid", fid); |
| 110 | + firebaseInstallationData.put("appId", appId); |
| 111 | + firebaseInstallationData.put("appVersion", FIREBASE_INSTALLATION_AUTH_VERSION); |
| 112 | + return firebaseInstallationData; |
| 113 | + } |
| 114 | + |
| 115 | + @NonNull |
| 116 | + public Code deleteFirebaseInstallation( |
| 117 | + long projectNumber, |
| 118 | + @NonNull String apiKey, |
| 119 | + @NonNull String fid, |
| 120 | + @NonNull String refreshToken) { |
| 121 | + String resourceName = String.format(DELETE_REQUEST_RESOURCE_NAME_FORMAT, projectNumber, fid); |
| 122 | + try { |
| 123 | + URL url = |
| 124 | + new URL( |
| 125 | + String.format( |
| 126 | + "https://%s/%s/%s?key=%s", |
| 127 | + FIREBASE_INSTALLATIONS_API_DOMAIN, |
| 128 | + FIREBASE_INSTALLATIONS_API_VERSION, |
| 129 | + resourceName, |
| 130 | + apiKey)); |
| 131 | + |
| 132 | + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); |
| 133 | + httpsURLConnection.setDoOutput(true); |
| 134 | + httpsURLConnection.setRequestMethod("DELETE"); |
| 135 | + httpsURLConnection.addRequestProperty("Authorization", "FIS_V2 " + refreshToken); |
| 136 | + httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); |
| 137 | + httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); |
| 138 | + |
| 139 | + int httpResponseCode = httpsURLConnection.getResponseCode(); |
| 140 | + switch (httpResponseCode) { |
| 141 | + case 200: |
| 142 | + return Code.OK; |
| 143 | + case 401: |
| 144 | + return Code.UNAUTHORIZED; |
| 145 | + default: |
| 146 | + return Code.SERVER_ERROR; |
| 147 | + } |
| 148 | + } catch (IOException e) { |
| 149 | + return Code.NETWORK_ERROR; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @NonNull |
| 154 | + public Code generateAuthToken( |
| 155 | + long projectNumber, |
| 156 | + @NonNull String apiKey, |
| 157 | + @NonNull String fid, |
| 158 | + @NonNull String refreshToken) { |
| 159 | + String resourceName = |
| 160 | + String.format(GENERATE_AUTH_TOKEN_REQUEST_RESOURCE_NAME_FORMAT, projectNumber, fid); |
| 161 | + try { |
| 162 | + URL url = |
| 163 | + new URL( |
| 164 | + String.format( |
| 165 | + "https://%s/%s/%s?key=%s", |
| 166 | + FIREBASE_INSTALLATIONS_API_DOMAIN, |
| 167 | + FIREBASE_INSTALLATIONS_API_VERSION, |
| 168 | + resourceName, |
| 169 | + apiKey)); |
| 170 | + |
| 171 | + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); |
| 172 | + httpsURLConnection.setDoOutput(true); |
| 173 | + httpsURLConnection.setRequestMethod("POST"); |
| 174 | + httpsURLConnection.addRequestProperty("Authorization", "FIS_V2 " + refreshToken); |
| 175 | + httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); |
| 176 | + httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); |
| 177 | + |
| 178 | + int httpResponseCode = httpsURLConnection.getResponseCode(); |
| 179 | + switch (httpResponseCode) { |
| 180 | + case 200: |
| 181 | + return Code.OK; |
| 182 | + case 401: |
| 183 | + return Code.UNAUTHORIZED; |
| 184 | + default: |
| 185 | + return Code.SERVER_ERROR; |
| 186 | + } |
| 187 | + } catch (IOException e) { |
| 188 | + return Code.NETWORK_ERROR; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments