1 package org.apache.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.archiva.redback.authentication.AuthenticationException;
23 import org.apache.archiva.security.ServletAuthenticator;
24 import org.apache.jackrabbit.webdav.DavException;
25 import org.apache.jackrabbit.webdav.DavServletRequest;
26 import org.apache.jackrabbit.webdav.DavSessionProvider;
27 import org.apache.jackrabbit.webdav.WebdavRequest;
28 import org.apache.archiva.webdav.util.RepositoryPathUtil;
29 import org.apache.archiva.webdav.util.WebdavMethodUtil;
30 import org.apache.archiva.redback.authentication.AuthenticationResult;
31 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
32 import org.codehaus.plexus.redback.policy.AccountLockedException;
33 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
39 public class ArchivaDavSessionProvider
40 implements DavSessionProvider
42 private ServletAuthenticator servletAuth;
44 private HttpAuthenticator httpAuth;
46 public ArchivaDavSessionProvider( ServletAuthenticator servletAuth, HttpAuthenticator httpAuth )
48 this.servletAuth = servletAuth;
49 this.httpAuth = httpAuth;
52 public boolean attachSession( WebdavRequest request )
55 final String repositoryId = RepositoryPathUtil.getRepositoryName( removeContextPath( request ) );
59 AuthenticationResult result = httpAuth.getAuthenticationResult( request, null );
61 //Create a dav session
62 request.setDavSession( new ArchivaDavSession() );
64 return servletAuth.isAuthenticated( request, result );
66 catch ( AuthenticationException e )
68 // safety check for MRM-911
69 String guest = UserManager.GUEST_USERNAME;
72 if ( servletAuth.isAuthorized( guest,
73 ( (ArchivaDavResourceLocator) request.getRequestLocator() ).getRepositoryId(),
74 WebdavMethodUtil.getMethodPermission( request.getMethod() ) ) )
76 request.setDavSession( new ArchivaDavSession() );
80 catch ( UnauthorizedException ae )
82 throw new UnauthorizedDavException( repositoryId,
83 "You are not authenticated and authorized to access any repository." );
86 throw new UnauthorizedDavException( repositoryId, "You are not authenticated." );
88 catch ( MustChangePasswordException e )
90 throw new UnauthorizedDavException( repositoryId, "You must change your password." );
92 catch ( AccountLockedException e )
94 throw new UnauthorizedDavException( repositoryId, "User account is locked." );
98 public void releaseSession( WebdavRequest request )
100 request.setDavSession( null );
103 private String removeContextPath( final DavServletRequest request )
105 String path = request.getRequestURI();
106 String ctx = request.getContextPath();
107 if ( path.startsWith( ctx ) )
109 path = path.substring( ctx.length() );