You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaServletAuthenticatorTest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package org.apache.archiva.security;
  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.archiva.redback.authentication.AuthenticationException;
  21. import org.apache.archiva.redback.authentication.AuthenticationResult;
  22. import org.apache.archiva.redback.authorization.UnauthorizedException;
  23. import org.apache.archiva.redback.system.DefaultSecuritySession;
  24. import org.apache.archiva.redback.system.SecuritySession;
  25. import org.apache.archiva.redback.users.User;
  26. import org.apache.archiva.redback.users.UserManager;
  27. import org.apache.archiva.security.common.ArchivaRoleConstants;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import javax.inject.Inject;
  31. import javax.inject.Named;
  32. import javax.servlet.http.HttpServletRequest;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.when;
  35. /**
  36. * ArchivaServletAuthenticatorTest
  37. */
  38. public class ArchivaServletAuthenticatorTest
  39. extends AbstractSecurityTest
  40. {
  41. @Inject
  42. @Named( value = "servletAuthenticator#test" )
  43. private ServletAuthenticator servletAuth;
  44. private HttpServletRequest request;
  45. @Before
  46. @Override
  47. public void setUp()
  48. throws Exception
  49. {
  50. super.setUp();
  51. request = mock( HttpServletRequest.class );
  52. setupRepository( "corporate" );
  53. }
  54. protected void assignRepositoryManagerRole( String principal, String repoId )
  55. throws Exception
  56. {
  57. roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId, principal );
  58. }
  59. @Test
  60. public void testIsAuthenticatedUserExists()
  61. throws Exception
  62. {
  63. AuthenticationResult result = new AuthenticationResult( true, "user", null );
  64. boolean isAuthenticated = servletAuth.isAuthenticated( request, result );
  65. assertTrue( isAuthenticated );
  66. }
  67. @Test
  68. public void testIsAuthenticatedUserDoesNotExist()
  69. throws Exception
  70. {
  71. AuthenticationResult result = new AuthenticationResult( false, "non-existing-user", null );
  72. try
  73. {
  74. servletAuth.isAuthenticated( request, result );
  75. fail( "Authentication exception should have been thrown." );
  76. }
  77. catch ( AuthenticationException e )
  78. {
  79. assertEquals( "User Credentials Invalid", e.getMessage() );
  80. }
  81. }
  82. @Test
  83. public void testIsAuthorizedUserHasWriteAccess()
  84. throws Exception
  85. {
  86. createUser( USER_ALPACA, "Al 'Archiva' Paca" );
  87. assignRepositoryManagerRole( USER_ALPACA, "corporate" );
  88. UserManager userManager = securitySystem.getUserManager();
  89. User user = userManager.findUser( USER_ALPACA );
  90. AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
  91. SecuritySession session = new DefaultSecuritySession( result, user );
  92. boolean isAuthorized =
  93. servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT );
  94. assertTrue( isAuthorized );
  95. restoreGuestInitialValues( USER_ALPACA );
  96. }
  97. @Test
  98. public void testIsAuthorizedUserHasNoWriteAccess()
  99. throws Exception
  100. {
  101. createUser( USER_ALPACA, "Al 'Archiva' Paca" );
  102. assignRepositoryObserverRole( USER_ALPACA, "corporate" );
  103. //httpServletRequestControl.expectAndReturn( request.getRemoteAddr(), "192.168.111.111" );
  104. when( request.getRemoteAddr() ).thenReturn( "192.168.111.111" );
  105. UserManager userManager = securitySystem.getUserManager();
  106. User user = userManager.findUser( USER_ALPACA );
  107. AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
  108. SecuritySession session = new DefaultSecuritySession( result, user );
  109. try
  110. {
  111. servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT );
  112. fail( "UnauthorizedException should have been thrown." );
  113. }
  114. catch ( UnauthorizedException e )
  115. {
  116. assertEquals( "Access denied for repository corporate", e.getMessage() );
  117. }
  118. restoreGuestInitialValues( USER_ALPACA );
  119. }
  120. @Test
  121. public void testIsAuthorizedUserHasReadAccess()
  122. throws Exception
  123. {
  124. createUser( USER_ALPACA, "Al 'Archiva' Paca" );
  125. assignRepositoryObserverRole( USER_ALPACA, "corporate" );
  126. UserManager userManager = securitySystem.getUserManager();
  127. User user = userManager.findUser( USER_ALPACA );
  128. AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
  129. SecuritySession session = new DefaultSecuritySession( result, user );
  130. boolean isAuthorized =
  131. servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_READ_REPOSITORY );
  132. assertTrue( isAuthorized );
  133. restoreGuestInitialValues( USER_ALPACA );
  134. }
  135. @Test
  136. public void testIsAuthorizedUserHasNoReadAccess()
  137. throws Exception
  138. {
  139. createUser( USER_ALPACA, "Al 'Archiva' Paca" );
  140. UserManager userManager = securitySystem.getUserManager();
  141. User user = userManager.findUser( USER_ALPACA );
  142. AuthenticationResult result = new AuthenticationResult( true, USER_ALPACA, null );
  143. SecuritySession session = new DefaultSecuritySession( result, user );
  144. try
  145. {
  146. servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_READ_REPOSITORY );
  147. fail( "UnauthorizedException should have been thrown." );
  148. }
  149. catch ( UnauthorizedException e )
  150. {
  151. assertEquals( "Access denied for repository corporate", e.getMessage() );
  152. }
  153. restoreGuestInitialValues( USER_ALPACA );
  154. }
  155. @Test
  156. public void testIsAuthorizedGuestUserHasWriteAccess()
  157. throws Exception
  158. {
  159. assignRepositoryManagerRole( USER_GUEST, "corporate" );
  160. boolean isAuthorized =
  161. servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT );
  162. assertTrue( isAuthorized );
  163. // cleanup previously add karma
  164. restoreGuestInitialValues(USER_GUEST);
  165. }
  166. @Test
  167. public void testIsAuthorizedGuestUserHasNoWriteAccess()
  168. throws Exception
  169. {
  170. assignRepositoryObserverRole( USER_GUEST, "corporate" );
  171. boolean isAuthorized =
  172. servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_ADD_ARTIFACT );
  173. assertFalse( isAuthorized );
  174. // cleanup previously add karma
  175. restoreGuestInitialValues(USER_GUEST);
  176. }
  177. @Test
  178. public void testIsAuthorizedGuestUserHasReadAccess()
  179. throws Exception
  180. {
  181. assignRepositoryObserverRole( USER_GUEST, "corporate" );
  182. boolean isAuthorized =
  183. servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_READ_REPOSITORY );
  184. assertTrue( isAuthorized );
  185. // cleanup previously add karma
  186. restoreGuestInitialValues(USER_GUEST);
  187. }
  188. @Test
  189. public void testIsAuthorizedGuestUserHasNoReadAccess()
  190. throws Exception
  191. {
  192. boolean isAuthorized =
  193. servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_READ_REPOSITORY );
  194. assertFalse( isAuthorized );
  195. }
  196. }