1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.authz;
21  
22  
23  import junit.framework.Assert;
24  import org.apache.directory.shared.ldap.exception.LdapNoPermissionException;
25  import org.apache.directory.shared.ldap.name.LdapDN;
26  import org.apache.directory.server.core.integ.CiRunner;
27  import org.apache.directory.server.core.integ.annotations.*;
28  import org.apache.directory.server.core.DirectoryService;
29  import org.junit.runner.RunWith;
30  
31  import static org.junit.Assert.fail;
32  import org.junit.Test;
33  import static org.apache.directory.server.core.authz.AutzIntegUtils.createUser;
34  import static org.apache.directory.server.core.authz.AutzIntegUtils.getContextAs;
35  import static org.apache.directory.server.core.authz.AutzIntegUtils.createAccessControlSubentry;
36  
37  
38  /**
39   * Tests whether or not authentication with authorization works properly.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev$
43   */
44  @RunWith ( CiRunner.class )
45  @Factory ( AutzIntegUtils.ServiceFactory.class )
46  public class AuthzAuthnIT
47  {
48      public static DirectoryService service;
49  
50  
51      /**
52       * Checks to make sure a user can authenticate with RootDSE as the
53       * provider URL without need of any access control permissions.
54       *
55       * @throws javax.naming.NamingException if the test encounters an error
56       */
57      @Test
58      public void testAuthnWithRootDSE() throws Exception
59      {
60          createUser( "billyd", "billyd" );
61  
62          LdapDN userName = new LdapDN( "uid=billyd,ou=users,ou=system" ); 
63          try
64          {
65              // Authenticate to RootDSE
66              getContextAs( userName, "billyd", "" );
67          }
68          catch ( LdapNoPermissionException e )
69          {
70              fail( "Authentication should not have failed." );
71          }
72      }
73      
74      
75      /**
76       * Checks to make sure a user cannot authenticate with a naming context
77       * as the provider URL if it does not have appropriate Browse permissions.
78       *
79       * @throws javax.naming.NamingException if the test encounters an error
80       */
81      @Test
82      public void testAuthnFailsWithSystemPartition() throws Exception
83      {
84          createUser( "billyd", "billyd" );
85          
86          LdapDN userName = new LdapDN( "uid=billyd,ou=users,ou=system" ); 
87          try
88          {
89              // Authenticate to "ou=system"
90              getContextAs( userName, "billyd", "ou=system" );
91              fail( "Authentication should have failed." );
92          }
93          catch ( LdapNoPermissionException e )
94          {
95              Assert.assertNotNull( e ); 
96          }
97      }
98      
99      
100     /**
101      * Checks to make sure a user can authenticate with a naming context
102      * as the provider URL if it has appropriate Browse permissions.
103      *
104      * @throws javax.naming.NamingException if the test encounters an error
105      */
106     @Test
107     public void testAuthnPassesWithSystemPartition() throws Exception
108     {
109         createUser( "billyd", "billyd" );
110         
111         // Create ACI with minimum level of required privileges:
112         // Only for user "uid=billyd,ou=users,ou=system"
113         // Only to The entry "ou=system"
114         // Only Browse permission
115         // Note: In order to read contents of the bound context
116         //       user will need appropriate Read permissions.
117         createAccessControlSubentry(
118             "grantBrowseForTheWholeNamingContext",
119             "{ maximum 0 }", // !!!!! Replace this with "{ minimum 1 }" for practicing !
120             "{ " + "identificationTag \"browseACI\", "
121             + "precedence 14, " + "authenticationLevel none, " + "itemOrUserFirst userFirst: { "
122             + "userClasses { name { \"uid=billyd,ou=users,ou=system\" } }, " + "userPermissions { { "
123             + "protectedItems { entry }, "
124             + "grantsAndDenials { grantBrowse } } } } }" );
125         
126         LdapDN userName = new LdapDN( "uid=billyd,ou=users,ou=system" ); 
127         try
128         {
129             // Authenticate to "ou=system"
130             getContextAs( userName, "billyd", "ou=system" );
131         }
132         catch ( LdapNoPermissionException e )
133         {
134             fail( "Authentication should not have failed." );
135         }
136     }
137 }