1 | |
package org.apache.maven.archiva.webdav; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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.ServletAuthenticator; |
27 | |
import org.apache.maven.archiva.webdav.util.RepositoryPathUtil; |
28 | |
import org.apache.maven.archiva.webdav.util.WebdavMethodUtil; |
29 | |
import org.codehaus.plexus.redback.authentication.AuthenticationException; |
30 | |
import org.codehaus.plexus.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.codehaus.plexus.redback.users.UserManager; |
35 | |
import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator; |
36 | |
|
37 | |
|
38 | |
|
39 | |
public class ArchivaDavSessionProvider |
40 | |
implements DavSessionProvider |
41 | |
{ |
42 | |
private ServletAuthenticator servletAuth; |
43 | |
|
44 | |
private HttpAuthenticator httpAuth; |
45 | |
|
46 | |
public ArchivaDavSessionProvider( ServletAuthenticator servletAuth, HttpAuthenticator httpAuth ) |
47 | 0 | { |
48 | 0 | this.servletAuth = servletAuth; |
49 | 0 | this.httpAuth = httpAuth; |
50 | 0 | } |
51 | |
|
52 | |
public boolean attachSession( WebdavRequest request ) |
53 | |
throws DavException |
54 | |
{ |
55 | 0 | final String repositoryId = RepositoryPathUtil.getRepositoryName( removeContextPath( request ) ); |
56 | |
|
57 | |
try |
58 | |
{ |
59 | 0 | AuthenticationResult result = httpAuth.getAuthenticationResult( request, null ); |
60 | |
|
61 | |
|
62 | 0 | request.setDavSession(new ArchivaDavSession()); |
63 | |
|
64 | 0 | return servletAuth.isAuthenticated( request, result ); |
65 | |
} |
66 | 0 | catch ( AuthenticationException e ) |
67 | |
{ |
68 | |
|
69 | 0 | String guest = UserManager.GUEST_USERNAME; |
70 | |
try |
71 | |
{ |
72 | 0 | if ( servletAuth.isAuthorized( guest, |
73 | |
( (ArchivaDavResourceLocator) request.getRequestLocator() ).getRepositoryId(), |
74 | |
WebdavMethodUtil.getMethodPermission( request.getMethod() ) ) ) |
75 | |
{ |
76 | 0 | request.setDavSession( new ArchivaDavSession() ); |
77 | 0 | return true; |
78 | |
} |
79 | |
} |
80 | 0 | catch ( UnauthorizedException ae ) |
81 | |
{ |
82 | 0 | throw new UnauthorizedDavException( repositoryId, |
83 | |
"You are not authenticated and authorized to access any repository." ); |
84 | 0 | } |
85 | |
|
86 | 0 | throw new UnauthorizedDavException( repositoryId, "You are not authenticated." ); |
87 | |
} |
88 | 0 | catch ( MustChangePasswordException e ) |
89 | |
{ |
90 | 0 | throw new UnauthorizedDavException( repositoryId, "You must change your password." ); |
91 | |
} |
92 | 0 | catch ( AccountLockedException e ) |
93 | |
{ |
94 | 0 | throw new UnauthorizedDavException( repositoryId, "User account is locked." ); |
95 | |
} |
96 | |
} |
97 | |
|
98 | |
public void releaseSession( WebdavRequest request ) |
99 | |
{ |
100 | 0 | request.setDavSession(null); |
101 | 0 | } |
102 | |
|
103 | |
private String removeContextPath( final DavServletRequest request ) |
104 | |
{ |
105 | 0 | String path = request.getRequestURI(); |
106 | 0 | String ctx = request.getContextPath(); |
107 | 0 | if ( path.startsWith( ctx ) ) |
108 | |
{ |
109 | 0 | path = path.substring( ctx.length() ); |
110 | |
} |
111 | 0 | return path; |
112 | |
} |
113 | |
} |