Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ArchivaDavSessionProvider.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package org.apache.maven.archiva.webdav;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.jackrabbit.webdav.DavException;
  21. import org.apache.jackrabbit.webdav.DavServletRequest;
  22. import org.apache.jackrabbit.webdav.DavSessionProvider;
  23. import org.apache.jackrabbit.webdav.WebdavRequest;
  24. import org.apache.maven.archiva.security.ServletAuthenticator;
  25. import org.apache.maven.archiva.webdav.util.RepositoryPathUtil;
  26. import org.apache.maven.archiva.webdav.util.WebdavMethodUtil;
  27. import org.codehaus.plexus.redback.authentication.AuthenticationException;
  28. import org.codehaus.plexus.redback.authentication.AuthenticationResult;
  29. import org.codehaus.plexus.redback.authorization.UnauthorizedException;
  30. import org.codehaus.plexus.redback.policy.AccountLockedException;
  31. import org.codehaus.plexus.redback.policy.MustChangePasswordException;
  32. import org.codehaus.plexus.redback.users.UserManager;
  33. import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
  34. /**
  35. */
  36. public class ArchivaDavSessionProvider
  37. implements DavSessionProvider
  38. {
  39. private ServletAuthenticator servletAuth;
  40. private HttpAuthenticator httpAuth;
  41. public ArchivaDavSessionProvider( ServletAuthenticator servletAuth, HttpAuthenticator httpAuth )
  42. {
  43. this.servletAuth = servletAuth;
  44. this.httpAuth = httpAuth;
  45. }
  46. public boolean attachSession( WebdavRequest request )
  47. throws DavException
  48. {
  49. final String repositoryId = RepositoryPathUtil.getRepositoryName( removeContextPath( request ) );
  50. try
  51. {
  52. AuthenticationResult result = httpAuth.getAuthenticationResult( request, null );
  53. //Create a dav session
  54. request.setDavSession(new ArchivaDavSession());
  55. return servletAuth.isAuthenticated( request, result );
  56. }
  57. catch ( AuthenticationException e )
  58. {
  59. // safety check for MRM-911
  60. String guest = UserManager.GUEST_USERNAME;
  61. try
  62. {
  63. if ( servletAuth.isAuthorized( guest,
  64. ( (ArchivaDavResourceLocator) request.getRequestLocator() ).getRepositoryId(),
  65. WebdavMethodUtil.getMethodPermission( request.getMethod() ) ) )
  66. {
  67. request.setDavSession( new ArchivaDavSession() );
  68. return true;
  69. }
  70. }
  71. catch ( UnauthorizedException ae )
  72. {
  73. throw new UnauthorizedDavException( repositoryId,
  74. "You are not authenticated and authorized to access any repository." );
  75. }
  76. throw new UnauthorizedDavException( repositoryId, "You are not authenticated." );
  77. }
  78. catch ( MustChangePasswordException e )
  79. {
  80. throw new UnauthorizedDavException( repositoryId, "You must change your password." );
  81. }
  82. catch ( AccountLockedException e )
  83. {
  84. throw new UnauthorizedDavException( repositoryId, "User account is locked." );
  85. }
  86. }
  87. public void releaseSession( WebdavRequest request )
  88. {
  89. request.setDavSession(null);
  90. }
  91. private String removeContextPath( final DavServletRequest request )
  92. {
  93. String path = request.getRequestURI();
  94. String ctx = request.getContextPath();
  95. if ( path.startsWith( ctx ) )
  96. {
  97. path = path.substring( ctx.length() );
  98. }
  99. return path;
  100. }
  101. }