]> source.dussan.org Git - archiva.git/blob
a5c447e8df7d93848d7056d8373b79a7b16139a2
[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.DavException;
23 import org.apache.jackrabbit.webdav.DavServletRequest;
24 import org.apache.jackrabbit.webdav.DavSessionProvider;
25 import org.apache.jackrabbit.webdav.WebdavRequest;
26 import org.apache.maven.archiva.security.ArchivaXworkUser;
27 import org.apache.maven.archiva.security.ServletAuthenticator;
28 import org.apache.maven.archiva.webdav.util.RepositoryPathUtil;
29 import org.apache.maven.archiva.webdav.util.WebdavMethodUtil;
30 import org.codehaus.plexus.redback.authentication.AuthenticationException;
31 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
32 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
33 import org.codehaus.plexus.redback.policy.AccountLockedException;
34 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
35 import org.codehaus.plexus.redback.users.UserManager;
36 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
37
38 /**
39  */
40 public class ArchivaDavSessionProvider
41     implements DavSessionProvider
42 {
43     private ServletAuthenticator servletAuth;
44
45     private HttpAuthenticator httpAuth;
46     
47     public ArchivaDavSessionProvider( ServletAuthenticator servletAuth, HttpAuthenticator httpAuth, ArchivaXworkUser archivaXworkUser )
48     {
49         this.servletAuth = servletAuth;
50         this.httpAuth = httpAuth;
51     }
52
53     public boolean attachSession( WebdavRequest request )
54         throws DavException
55     {    
56         final String repositoryId = RepositoryPathUtil.getRepositoryName( removeContextPath( request ) );
57         
58         try
59         {
60             AuthenticationResult result = httpAuth.getAuthenticationResult( request, null );
61             
62             //Create a dav session
63             request.setDavSession(new ArchivaDavSession());
64             
65             return servletAuth.isAuthenticated( request, result );
66         }
67         catch ( AuthenticationException e )
68         {   
69             // safety check for MRM-911            
70             String guest = UserManager.GUEST_USERNAME;
71             try
72             {
73                 if ( servletAuth.isAuthorized( guest,
74                                                ( (ArchivaDavResourceLocator) request.getRequestLocator() ).getRepositoryId(),
75                                                WebdavMethodUtil.getMethodPermission( request.getMethod() ) ) )
76                 {
77                     request.setDavSession( new ArchivaDavSession() );
78                     return true;
79                 }
80             }
81             catch ( UnauthorizedException ae )
82             {
83                 throw new UnauthorizedDavException( repositoryId,
84                     "You are not authenticated and authorized to access any repository." );
85             }
86             
87             throw new UnauthorizedDavException( repositoryId, "You are not authenticated." );            
88         }
89         catch ( MustChangePasswordException e )
90         {         
91             throw new UnauthorizedDavException( repositoryId, "You must change your password." );
92         }
93         catch ( AccountLockedException e )
94         {         
95             throw new UnauthorizedDavException( repositoryId, "User account is locked." );
96         }        
97     }
98
99     public void releaseSession( WebdavRequest request )
100     {
101         request.setDavSession(null);
102     }
103     
104     private String removeContextPath( final DavServletRequest request )
105     {
106         String path = request.getRequestURI();
107         String ctx = request.getContextPath();
108         if ( path.startsWith( ctx ) )
109         {
110             path = path.substring( ctx.length() );
111         }
112         return path;
113     }    
114 }