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