// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps;

import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse;
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
import com.google.cloud.secretmanager.v1.SecretVersionName;
import java.io.IOException;

public class SecretHandler {
  private static final String PROJECT_ID = "unitebystep";
  private static final String API_KEY_ID = "api-key";
  private static final String VERSION_ID = "1";

  /**
   * Returns a String containing the API key. Throws an exception if the Secret manager client is
   * unable to be created.
   */
  public static String getApiKey() throws IOException {
    SecretManagerServiceClient client = null;
    String result = "";
    try {
      client = SecretManagerServiceClient.create();
      SecretVersionName secretVersionName =
          SecretVersionName.of(PROJECT_ID, API_KEY_ID, VERSION_ID);

      AccessSecretVersionResponse response = client.accessSecretVersion(secretVersionName);
      result = response.getPayload().getData().toStringUtf8();

    } catch (IOException e) {
      throw new IOException("failed to create secret manager service client");
    } finally {
      if (client != null) {
        client.close();
      }
      return result;
    }
  }
}

@@ -607,16 +607,65 @@
 e.sps;%0A%0A
+import com.google.auth.oauth2.GoogleCredentials;%0A
 import c
@@ -870,27 +870,92 @@
 java.io.
-IOException
+ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0A%0Apubli
@@ -1090,16 +1090,80 @@
 i-key%22;%0A
+  private static final String FIREBASE_KEY_ID = %22firebase-key%22;%0A
   privat
@@ -2012,11 +2012,1021 @@
    %7D%0A  %7D
+%0A%0A  /**%0A   * Returns a GoogleCredentials object containing the Firebase Admin key. Throws an exception if%0A   * the Secret manager client is unable to be created.%0A   */%0A  public static GoogleCredentials getFirebaseKey() throws IOException %7B%0A    SecretManagerServiceClient client = null;%0A    String result = %22%22;%0A    GoogleCredentials cred = null;%0A    try %7B%0A      client = SecretManagerServiceClient.create();%0A      SecretVersionName secretVersionName =%0A          SecretVersionName.of(PROJECT_ID, FIREBASE_KEY_ID, VERSION_ID);%0A%0A      AccessSecretVersionResponse response = client.accessSecretVersion(secretVersionName);%0A      result = response.getPayload().getData().toStringUtf8();%0A    %7D catch (IOException e) %7B%0A      throw new IOException(%22failed to create secret manager service client%22);%0A    %7D finally %7B%0A      if (client != null) %7B%0A        client.close();%0A      %7D%0A    %7D%0A    InputStream stream = new ByteArrayInputStream(result.getBytes());%0A    cred = GoogleCredentials.fromStream(stream);%0A    return cred;%0A  %7D
 %0A%7D%0A

Add secret for Firebase Admin key