1 package org.apache.maven.archiva.webdav;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
42 import javax.servlet.http.HttpServletResponse;
45 * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
47 public class ArchivaDavSessionProvider implements DavSessionProvider
49 private Logger log = LoggerFactory.getLogger(ArchivaDavSessionProvider.class);
51 private ServletAuthenticator servletAuth;
53 private HttpAuthenticator httpAuth;
55 public ArchivaDavSessionProvider(WebApplicationContext applicationContext)
57 servletAuth = (ServletAuthenticator) applicationContext.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
59 (HttpAuthenticator) applicationContext.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
62 public boolean attachSession(WebdavRequest request) throws DavException
64 final String repositoryId = RepositoryPathUtil.getRepositoryName(removeContextPath(request));
68 AuthenticationResult result = httpAuth.getAuthenticationResult( request, null );
69 SecuritySession securitySession = httpAuth.getSecuritySession();
71 return servletAuth.isAuthenticated(request, result, repositoryId) &&
72 servletAuth.isAuthorized(request, securitySession, repositoryId, WebdavMethodUtil.isWriteMethod( request.getMethod() ) );
74 catch ( AuthenticationException e )
76 log.error( "Cannot authenticate user.", e );
77 throw new UnauthorizedDavException(repositoryId, "You are not authenticated");
79 catch ( MustChangePasswordException e )
81 log.error( "User must change password." );
82 throw new UnauthorizedDavException(repositoryId, "You must change your password.");
84 catch ( AccountLockedException e )
86 log.error( "User account is locked." );
87 throw new UnauthorizedDavException(repositoryId, "User account is locked.");
89 catch ( AuthorizationException e )
91 log.error( "Fatal Authorization Subsystem Error." );
92 throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Fatal Authorization Subsystem Error." );
94 catch ( UnauthorizedException e )
96 log.error( e.getMessage() );
97 throw new UnauthorizedDavException(repositoryId, e.getMessage() );
101 public void releaseSession(WebdavRequest webdavRequest)
106 private String removeContextPath(final DavServletRequest request)
108 String path = request.getRequestURI();
109 String ctx = request.getContextPath();
110 if (path.startsWith(ctx)) {
111 path = path.substring(ctx.length());