/*
 * Copyright 2017 Google Inc.
 *
 * 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 org.dataportabilityproject.webapp;

import static org.apache.axis.transport.http.HTTPConstants.HEADER_SET_COOKIE;

import com.google.common.base.Charsets;
import com.sun.net.httpserver.Headers;
import java.nio.charset.Charset;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.dataportabilityproject.job.Crypter;
import org.dataportabilityproject.shared.auth.AuthData;

/**
 * Helper methods to work with encrypting data for the client.
 */
class CryptoHelper {
  private static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
  private final Crypter crypter;

  CryptoHelper(Crypter crypter) {
    this.crypter = crypter;
  }

  /* Encrypts the given {@code authData} and stores it as a cookie in the provided response.
   * TODO: remove once the Spring API server is  deprecated
   */
  void encryptAndSetCookie(HttpServletResponse response, boolean isExport, AuthData authData) {
    String encrypted = encryptAuthData(authData);
    String cookieKey = isExport ? JsonKeys.EXPORT_AUTH_DATA_COOKIE_KEY : JsonKeys.IMPORT_AUTH_DATA_COOKIE_KEY;
    Cookie authCookie = new Cookie(cookieKey, encrypted);
    LogUtils.log("Set new cookie with key: %s, length: %s", cookieKey, encrypted.length());
    // TODO: reenable. Currently doesn't like the cookie, has a bad value.
    // response.addCookie(authCookie);
  }

  /** Encrypts the given {@code authData} and stores it as a cookie in the provided headers. */
  void encryptAndSetCookie(Headers headers, boolean isExport, AuthData authData){
    String encrypted = encryptAuthData(authData);
    String cookieKey = isExport ? JsonKeys.EXPORT_AUTH_DATA_COOKIE_KEY : JsonKeys.IMPORT_AUTH_DATA_COOKIE_KEY;
    Cookie authCookie = new Cookie(cookieKey, encrypted);
    LogUtils.log("Set new cookie with key: %s, length: %s", cookieKey, encrypted.length());
    headers.add(HEADER_SET_COOKIE, authCookie.toString());
  }

  /** Serialize and encrypt the given {@code authData} with the session key. */
  private String encryptAuthData(AuthData authData) {
    String serialized = authData.toString(); // Implement auth data serialization
    return serialized;
    // TODO: reenable
    // byte[] encrypted = crypter.encryptWithSessionKey(serialized);
    // return new String(encrypted, DEFAULT_CHARSET);
  }
}

@@ -2443,16 +2443,94 @@
 ());%0A   
+ // TODO: reenable. Currently doesn't like the cookie, has a bad value.%0A    //
  headers

disable setting new cookie in CryptoHelper. This outputs an invalid cookie, which causes parsing errors later in the auth flow.