/*
 * PartnerProvider.java	1.00 22-Feb-2001
 *
 * Copyright 2000-2001 by Oracle Corporation
 * 500 Oracle Parkway, Redwood Shores, California, 94065, U.S.A.
 * All rights reserved.
 */

package oracle.portal.sample.v1.devguide.partnerApp;

import oracle.portal.provider.v1.*;
import oracle.portal.provider.v1.http.*;
import java.lang.reflect.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.DriverManager;
import java.sql.Connection;

import oracle.security.sso.enabler.SSOEnabler;
import oracle.security.sso.enabler.SSOUserInfo;
import oracle.security.sso.enabler.SSOEnablerUtil;
import oracle.security.sso.enabler.SSOEnablerException;


public class PartnerProvider extends DefaultProvider
{
  private static  PartnerSSOEnabler mpSsoBean;

 /**
  *  Constructor
  *
  *  Call super to constructor the Default Provider.
  */
  public PartnerProvider()
  {
    super();
  }

 /**
  *  initSession performs all session and login work.
  *
  *  In this case the initSession simply needs to create the application cookie.
  *  and return this to the caller.
  *  This routine is called during the doLogin calls of the provider framwork.
  *
  *  @param  user  Is a ProviderUser object which can be seen in the
  *  JPDK API Documentation.
  *
  * @return An object array of, in this case, cookies.
  */
  public Object[] initSession(ProviderUser user, ExternalPrincipal extuser)
    throws ProviderException, AuthenticationException
  {

    String bakedAppCookie = null;
    String appCookieName = null;
    String appCookieDomain = null;
    String appCookieScope = null;
    String appCookieDesc = null;

    //call super initSession
    super.initSession(user, extuser);

    //Since we are not able to check if the partner application cookie exists,
    //just create it.
    try
    {
      // Set and Get cookie information
      bakedAppCookie  = mpSsoBean.bakeAppCookie(user.getName());
      appCookieName   = mpSsoBean.getAppCookieName();
      appCookieDomain = mpSsoBean.getAppCookieDomain();
      appCookieScope  = mpSsoBean.getAppCookieScope();
      appCookieDesc   = mpSsoBean.getAppCookieDesc();

    }
    catch(Exception e1)
    {
      throw new ProviderException(e1.toString());
    }

    Cookie jspAppCookie = new Cookie(appCookieName,bakedAppCookie);
    Cookie[] cookies = new Cookie[1];
    jspAppCookie.setDomain(appCookieDomain);
    // In-memory cookie for better security
    jspAppCookie.setMaxAge(-1);
    jspAppCookie.setPath(appCookieScope);
    jspAppCookie.setComment(appCookieDesc);
    cookies[0] = jspAppCookie;

    return cookies;

  }//initSession

 /**
  *  init initializes the default provider
  *
  *  Call super to constructor the Default Provider.
  */
  public void init(long providerId, ProviderLog logger, ServletConfig sc, HttpServletRequest request) throws ProviderException
  {
    //call super init
    super.init(providerId,logger,sc,request);

    // initialize PartnerSSOEnabler variables with listener servlet
    // configuration arguments
    mpSsoBean = new PartnerSSOEnabler(
      sc.getInitParameter("listenerToken"),
      sc.getInitParameter("requestedUrl"),
      sc.getInitParameter("onCancelUrl"),
      sc.getInitParameter("partnerAppCookieName"),
      sc.getInitParameter("partnerAppCookieDomain"),
      sc.getInitParameter("partnerAppCookieScope"),
      sc.getInitParameter("partnerAppCookieDesc"),
      sc.getInitParameter("dbSchema"),
      sc.getInitParameter("dbPassword"),
      sc.getInitParameter("dbhost"),
      sc.getInitParameter("dbport"),
      sc.getInitParameter("dbsid")
    );

  }//init

 /**
  * getSSOBean returns the PartnerSSOEnabler object that was initialized
  * in init.
  */
  public static PartnerSSOEnabler getSSOBean ()
  {
    return mpSsoBean;
  }

}
