This project has retired. For details please refer to its
Attic page.
ArchivaDavSessionProvider xref
1 package org.apache.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.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.apache.archiva.redback.authorization.UnauthorizedException;
32 import org.apache.archiva.redback.policy.AccountLockedException;
33 import org.apache.archiva.redback.policy.MustChangePasswordException;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.apache.archiva.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 {
48 this.servletAuth = servletAuth;
49 this.httpAuth = httpAuth;
50 }
51
52 @Override
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
63 request.setDavSession( new ArchivaDavSession() );
64
65 return servletAuth.isAuthenticated( request, result );
66 }
67 catch ( AuthenticationException e )
68 {
69
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 @Override
100 public void releaseSession( WebdavRequest request )
101 {
102 request.setDavSession( null );
103 }
104
105 private String removeContextPath( final DavServletRequest request )
106 {
107 String path = request.getRequestURI();
108 String ctx = request.getContextPath();
109 if ( path.startsWith( ctx ) )
110 {
111 path = path.substring( ctx.length() );
112 }
113 return path;
114 }
115 }