head	1.1;
access;
symbols
	HELM_PEER_PORT_BRANCH:1.1.0.2;
locks; strict;
comment	@# @;


1.1
date	2001.02.14.00.41.07;	author elicia;	state dead;
branches
	1.1.2.1;
next	;

1.1.2.1
date	2001.02.14.00.41.07;	author elicia;	state Exp;
branches;
next	1.1.2.2;

1.1.2.2
date	2001.02.14.00.43.14;	author elicia;	state dead;
branches;
next	;


desc
@@


1.1
log
@file MDACookie.java~ was initially added on branch HELM_PEER_PORT_BRANCH.
@
text
@@


1.1.2.1
log
@ported to turbine
@
text
@a0 164
/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.Collab.Net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.tool;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.*;
import javax.servlet.http.*;

import com.workingdogs.village.*;
import org.apache.turbine.util.*;

import org.joist.util.JoistProperties;

import org.tigris.helm.om.*;

/**
 * Writes a MDA cookie for for mod_auth_mda.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@version $Id: MDACookie.java,v 1.14 2001/01/26 18:29:09 edk Exp $
 */
public class MDACookie
{
    /**
     * The name of the cookie expected by the mod_auth_mda Apache module 
     * running on the file server.
     */
    private static final String MDA_COOKIE_NAME = "MDAAuth";

    /**
     * The amount of time that the cookie is valid for.
     */
    private static final int MAX_LIFE_TIME = 86400;

    /**
     * The shared secret with the mod_auth_mda Apache module running on the
     * file server.
     */
    private static final String MDA_CRYPT_KEY = 
        JoistProperties.getProperty("mdaCryptKey");
        //"w b@@5b15#664038f.f9d8U19b7e25 664eY9ad2%4393e,a2ef";

    private static final boolean debug = true;

    /**
     * Writes a cookie containing the digital signature to be interpreted by 
     * the Apache module
     * <a href="http://frogdot.de/mod_auth_mda/">mod_auth_mda</a>.  The cookie
     * scheme (i.e. <code>setSecure(boolean)</code>) is set to the same scheme
     * as that of the request.
     *
     * @@param server The server to create the URL for.  The cookie domain is
     *               also determined from this.
     * @@param user   The user whose email address to sign the URL.
     * @@param dl     The download to create a URL for.
     * @@param req    The HTTP request to get the address of.
     * @@param res    The HTTP reponse to set the cookie on.
     */
    public static void write (String server, Project project, User user, 
                              Boolean isPublicDownload, HttpServletRequest req,
                              HttpServletResponse res)
        throws UnknownHostException, DataSetException, Exception
    {
        try {
            // Get the cookie value.
            String requestIP = req.getRemoteAddr();
            String realm =
                project.generateMDARealmMask(isPublicDownload.booleanValue());
            String email = user.getEmail();
            String cargo = "";
            long time = new java.util.Date().getTime() / 1000;
            MDA mda = new MDA();
            String mdaVal = mda.compute(requestIP, MDA_CRYPT_KEY, email, realm,
                                        cargo, time);
            if (debug) Log.debug("mdaVal=" + mdaVal);
            mdaVal += mda.getTimestamp() + email + '!' + realm + '!' + cargo;

            // Write the cookie data.
            Cookie cookie = new Cookie(MDA_COOKIE_NAME, mdaVal);
            if (debug) Log.debug("server=" + server);
            String cookieDomain = getCookieDomain(server);
            if (debug) Log.debug("cookieDomain=" + cookieDomain);
            cookie.setDomain(cookieDomain);
            //cookie.setComment("mailto:bugs@@tigris.org");
            cookie.setMaxAge(MAX_LIFE_TIME);
            cookie.setPath("/");
            // Indicate whether or not to have the browser only send this cookie 
            // over SSL once it's been set.
            cookie.setSecure(req.getScheme().equals("https"));

            res.addCookie(cookie);
          } catch (DataSetException e) {
              throw new TurbineException(e);
          }
        }

        /**
     * Converts <code>some.host.foo.com</code> to <code>.foo.com</code> and 
     * <code>foo.com</code> to <code>.foo.com</code>.
     *
     * @@param hostname The hostname--including domain--of the host to determine
     *                 the cookie domain for.
     */
    private static String getCookieDomain (String hostname)
        throws UnknownHostException
    {
        int i = hostname.lastIndexOf('.');
        if (i > 0)
        {
            i = hostname.lastIndexOf('.', i - 1);
            return (i == -1 ? '.' + hostname : hostname.substring(i));
        }

        // Couldn't determine the cookie domain from the supplied host
        // name.  Default it to the JVM's host.
        String h = InetAddress.getLocalHost().getHostName();
        return (!h.equals(hostname) ? getCookieDomain(h) : null);
    }
}
@


1.1.2.2
log
@committed by accident
@
text
@d65 1
a65 1
 * @@version $Id: MDACookie.java~,v 1.1.2.1 2001/02/14 00:41:07 elicia Exp $
@


