]> source.dussan.org Git - archiva.git/blob
1ec63dfc66359949d4f9ce76056827775660ac3d
[archiva.git] /
1 package org.apache.maven.archiva.webdav;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.jackrabbit.webdav.DavSessionProvider;
23 import org.apache.jackrabbit.webdav.WebdavRequest;
24 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.DavServletRequest;
26 import org.apache.maven.archiva.webdav.util.WebdavMethodUtil;
27 import org.apache.maven.archiva.webdav.util.RepositoryPathUtil;
28 import org.apache.maven.archiva.security.ServletAuthenticator;
29 import org.codehaus.plexus.redback.authentication.AuthenticationException;
30 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
31 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
32 import org.codehaus.plexus.redback.policy.AccountLockedException;
33 import org.codehaus.plexus.redback.system.SecuritySession;
34 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
35 import org.codehaus.plexus.redback.authorization.AuthorizationException;
36 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
37 import org.codehaus.plexus.spring.PlexusToSpringUtils;
38 import org.springframework.web.context.WebApplicationContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import javax.servlet.http.HttpServletResponse;
43
44 /**
45  * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
46  */
47 public class ArchivaDavSessionProvider implements DavSessionProvider
48 {
49     private Logger log = LoggerFactory.getLogger(ArchivaDavSessionProvider.class);
50     
51     private ServletAuthenticator servletAuth;    
52     
53     private HttpAuthenticator httpAuth;
54             
55     public ArchivaDavSessionProvider(WebApplicationContext applicationContext)
56     {
57         servletAuth = (ServletAuthenticator) applicationContext.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
58         httpAuth =
59             (HttpAuthenticator) applicationContext.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
60     }
61
62     public boolean attachSession(WebdavRequest request) throws DavException
63     {
64         final String repositoryId = RepositoryPathUtil.getRepositoryName(removeContextPath(request));
65         
66         try
67         {
68             AuthenticationResult result = httpAuth.getAuthenticationResult( request, null );
69             SecuritySession securitySession = httpAuth.getSecuritySession();
70             
71             return servletAuth.isAuthenticated(request, result, repositoryId) && 
72                 servletAuth.isAuthorized(request, securitySession, repositoryId, WebdavMethodUtil.isWriteMethod( request.getMethod() ) );
73         }
74         catch ( AuthenticationException e )
75         {
76             log.error( "Cannot authenticate user.", e );
77             throw new UnauthorizedDavException(repositoryId, "You are not authenticated");
78         }
79         catch ( MustChangePasswordException e )
80         {
81             log.error( "User must change password." );
82             throw new UnauthorizedDavException(repositoryId, "You must change your password.");
83         }
84         catch ( AccountLockedException e )
85         {
86             log.error( "User account is locked." );
87             throw new UnauthorizedDavException(repositoryId, "User account is locked.");
88         }
89         catch ( AuthorizationException e )
90         {
91             log.error( "Fatal Authorization Subsystem Error." );
92             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Fatal Authorization Subsystem Error." );
93         }
94         catch ( UnauthorizedException e )
95         {
96             log.error( e.getMessage() );
97             throw new UnauthorizedDavException(repositoryId, e.getMessage() );
98         }
99     }
100
101     public void releaseSession(WebdavRequest webdavRequest)
102     {
103         
104     }   
105     
106     private String removeContextPath(final DavServletRequest request)
107     {
108         String path = request.getRequestURI();
109         String ctx = request.getContextPath();
110         if (path.startsWith(ctx)) {
111             path = path.substring(ctx.length());
112         }
113         return path;
114     }
115 }