// Copyright (C) 2008 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
//
//      http://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.enterprise.connector.security.identity;

import com.google.enterprise.connector.common.cookie.CookieSet;
import com.google.enterprise.connector.common.cookie.CookieUtil;
import com.google.enterprise.connector.spi.AbstractAuthnIdentity;

import java.util.Collection;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

/**
 * This mis-named class models the per-session info associated with verifying a particular
 * identity using a particular mechanism.  A better name might be "verification info", but
 * we have yet to settle on an appropriate name.  This object directly holds the
 * per-session data, while the configuration info, which is shared by all sessions, is
 * stored in a separate object.
 */
public class DomainCredentials extends AbstractAuthnIdentity {

  private final AuthnDomain configInfo;
  private final CredentialsGroup cg;
  private final CookieSet cookies;

  DomainCredentials(AuthnDomain configInfo, CredentialsGroup cg) {
    super();
    this.configInfo = configInfo;
    this.cg = cg;
    cookies = new CookieSet();
    cg.addElement(this);
  }

  // Used for testing only:
  public static DomainCredentials dummy(HttpSession session) {
    return new DomainCredentials(null, CredentialsGroup.dummy(session));
  }

  /* @Override */
  public String getDomain() {
    return configInfo.getName();
  }

  /* @Override */
  public AuthnMechanism getMechanism() {
    return configInfo.getMechanism();
  }

  /* @Override */
  public String getSampleUrl() {
    return configInfo.getSampleUrl();
  }

  /* @Override */
  public String getUsername() {
    return cg.getUsername();
  }

  /* @Override */
  public void setUsername(String username) {
    cg.setUsername(username);
  }

  /* @Override */
  public String getPassword() {
    return cg.getPassword();
  }

  /* @Override */
  public HttpSession getSession() {
    return cg.getSession();
  }

  /* @Override */
  public Collection<Cookie> getCookies() {
    return cookies;
  }

  /**
   * Generate a string giving an outline of the contents.
   *
   * @return The outline string.
   */
  public String dumpInfo() {
    StringBuilder sb = new StringBuilder();
    sb.append(getUsername());
    sb.append(":#");
    sb.append(getPassword().hashCode());
    sb.append("; ");
    sb.append(CookieUtil.cookieHeaderValue(getCookies(), false));
    return sb.toString();
  }
}

@@ -2150,32 +2150,126 @@
 mpleUrl();%0A  %7D%0A%0A
+  /* @Override */%0A  public String getAuthority() %7B%0A    return configInfo.getAuthority();%0A  %7D%0A%0A
   /* @Override *
@@ -2898,24 +2898,59 @@
 sername());%0A
+    if (getPassword() != null) %7B%0A  
     sb.appen
@@ -2954,24 +2954,26 @@
 pend(%22:#%22);%0A
+  
     sb.appen
@@ -3009,39 +3009,36 @@
     
-sb.append(%22; %22);%0A    sb.append(
+%7D%0A    String cookieString = 
 Cook
@@ -3082,18 +3082,109 @@
 , false)
-)
 ;
+%0A    if (cookieString != null) %7B%0A      sb.append(%22; %22);%0A      sb.append(cookieString);%0A    %7D
 %0A    ret

Change dumpInfo() to handle edge cases better.