1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
46
47 class GroupMappingWaffleRealmTest {
48
49
50 private static final String ROLE_NAME = "ShiroUsers";
51
52
53 private MockWindowsAuthProvider windowsAuthProvider;
54
55
56 private GroupMappingWaffleRealm realm;
57
58
59
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
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
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
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
113
114
115
116 private String getCurrentUserName() {
117 return Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
118 }
119
120 }