View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2020 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package waffle.shiro;
25  
26  import static org.assertj.core.api.Assertions.assertThat;
27  
28  import com.sun.jna.platform.win32.Secur32.EXTENDED_NAME_FORMAT;
29  import com.sun.jna.platform.win32.Secur32Util;
30  
31  import java.util.Collections;
32  
33  import org.apache.shiro.authc.AuthenticationException;
34  import org.apache.shiro.authc.AuthenticationInfo;
35  import org.apache.shiro.authc.AuthenticationToken;
36  import org.apache.shiro.authc.UsernamePasswordToken;
37  import org.apache.shiro.subject.PrincipalCollection;
38  import org.junit.jupiter.api.Assertions;
39  import org.junit.jupiter.api.BeforeEach;
40  import org.junit.jupiter.api.Test;
41  
42  import waffle.mock.MockWindowsAuthProvider;
43  
44  /**
45   * The Class GroupMappingWaffleRealmTest.
46   */
47  class GroupMappingWaffleRealmTest {
48  
49      /** The Constant ROLE_NAME. */
50      private static final String ROLE_NAME = "ShiroUsers";
51  
52      /** The windows auth provider. */
53      private MockWindowsAuthProvider windowsAuthProvider;
54  
55      /** The realm. */
56      private GroupMappingWaffleRealm realm;
57  
58      /**
59       * Sets the up.
60       */
61      @BeforeEach
62      void setUp() {
63          this.windowsAuthProvider = new MockWindowsAuthProvider();
64          this.realm = new GroupMappingWaffleRealm();
65          this.realm.setProvider(this.windowsAuthProvider);
66          this.realm.setGroupRolesMap(Collections.singletonMap("Users", GroupMappingWaffleRealmTest.ROLE_NAME));
67      }
68  
69      /**
70       * Test valid username password.
71       */
72      @Test
73      void testValidUsernamePassword() {
74          final AuthenticationToken token = new UsernamePasswordToken(this.getCurrentUserName(), "somePassword");
75          final AuthenticationInfo authcInfo = this.realm.getAuthenticationInfo(token);
76          final PrincipalCollection principals = authcInfo.getPrincipals();
77          Assertions.assertFalse(principals.isEmpty());
78          final Object primaryPrincipal = principals.getPrimaryPrincipal();
79          Assertions.assertNotNull(primaryPrincipal);
80          assertThat(primaryPrincipal).isInstanceOf(WaffleFqnPrincipal.class);
81          final WaffleFqnPrincipal fqnPrincipal = (WaffleFqnPrincipal) primaryPrincipal;
82          assertThat(fqnPrincipal.getFqn()).isEqualTo(this.getCurrentUserName());
83          assertThat(fqnPrincipal.getGroupFqns()).contains("Users", "Everyone");
84          final Object credentials = authcInfo.getCredentials();
85          assertThat(credentials).isInstanceOf(char[].class).isEqualTo("somePassword".toCharArray());
86          Assertions.assertTrue(this.realm.hasRole(principals, GroupMappingWaffleRealmTest.ROLE_NAME));
87      }
88  
89      /**
90       * Test invalid username password.
91       */
92      @Test
93      void testInvalidUsernamePassword() {
94          final AuthenticationToken token = new UsernamePasswordToken("InvalidUser", "somePassword");
95          Assertions.assertThrows(AuthenticationException.class, () -> {
96              this.realm.getAuthenticationInfo(token);
97          });
98      }
99  
100     /**
101      * Test guest username password.
102      */
103     @Test
104     void testGuestUsernamePassword() {
105         final AuthenticationToken token = new UsernamePasswordToken("Guest", "somePassword");
106         Assertions.assertThrows(AuthenticationException.class, () -> {
107             this.realm.getAuthenticationInfo(token);
108         });
109     }
110 
111     /**
112      * Gets the current user name.
113      *
114      * @return the current user name
115      */
116     private String getCurrentUserName() {
117         return Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
118     }
119 
120 }