001    //$HeadURL$
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.security;
037    
038    import java.net.URL;
039    import java.util.Map;
040    
041    import org.deegree.framework.log.ILogger;
042    import org.deegree.framework.log.LoggerFactory;
043    import org.deegree.framework.xml.NamespaceContext;
044    import org.deegree.framework.xml.XMLFragment;
045    import org.deegree.framework.xml.XMLTools;
046    import org.deegree.i18n.Messages;
047    import org.deegree.ogcbase.CommonNamespaces;
048    import org.deegree.security.drm.SecurityAccessManager;
049    import org.deegree.security.drm.WrongCredentialsException;
050    import org.deegree.security.drm.model.User;
051    import org.w3c.dom.Document;
052    
053    /**
054     *
055     *
056     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
057     * @author last edited by: $Author: poth $
058     *
059     * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
060     */
061    public class WASAuthentication extends AbstractAuthentication {
062    
063        private static final ILogger LOG = LoggerFactory.getLogger( WASAuthentication.class );
064    
065        private static final NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
066    
067        protected static final String AUTH_PARAM_SESSIONID = "SESSIONID";
068    
069        protected static final String INIT_PARAM_WAS = "WAS";
070    
071        protected static final String INIT_PARAM_BASEREQUEST = "WAS";
072    
073        /**
074         *
075         * @param authenticationName
076         * @param initParams
077         */
078        public WASAuthentication( String authenticationName, Map<String, String> initParams ) {
079            super( authenticationName, initParams );
080        }
081    
082        /*
083         * (non-Javadoc)
084         *
085         * @see org.deegree.security.AbstractAuthentication#authenticate(java.util.Map)
086         */
087        public User authenticate( Map<String, String> params )
088                                throws WrongCredentialsException {
089    
090            String sessionID = params.get( AUTH_PARAM_SESSIONID );
091            User usr = null;
092            if ( sessionID != null ) {
093                String[] user = new String[3];
094                String urlStr = initParams.get( INIT_PARAM_WAS );
095                urlStr = urlStr.replaceFirst( "\\[SESSIONID\\]", sessionID );
096                LOG.logDebug( "request WAS for user information: " + urlStr );
097                Document doc = null;
098                try {
099                    URL url = new URL( urlStr );
100                    XMLFragment xml = new XMLFragment( url );
101                    doc = xml.getRootElement().getOwnerDocument();
102                    user[0] = XMLTools.getNodeAsString( doc, "/User/UserName", nsContext, null );
103                    user[1] = XMLTools.getNodeAsString( doc, "/User/Password", nsContext, null );
104                } catch ( Exception e ) {
105                    LOG.logError( e.getMessage(), e );
106                    throw new WrongCredentialsException( Messages.getMessage( "OWSProxyServletFilter.WASACCESS" ) );
107                }
108    
109                if ( user[0] != null ) {
110                    try {
111                        SecurityAccessManager sam = SecurityAccessManager.getInstance();
112                        usr = sam.getUserByName( user[0] );
113                        usr.authenticate( user[1] );
114                    } catch ( Exception e ) {
115                        throw new WrongCredentialsException( Messages.getMessage( "OWSPROXY_USER_AUTH_ERROR", user[0] ) );
116                    }
117                } else {
118                    String msg = "undefined error";
119                    try {
120                        msg = XMLTools.getNodeAsString( doc, "//ServiceException", nsContext, "general error" );
121                    } catch ( Exception e ) {
122                        // should never happen
123                    }
124                    throw new WrongCredentialsException( msg );
125                }
126            }
127    
128            return usr;
129        }
130    
131    }