View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 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.mock;
25  
26  import com.sun.jna.platform.win32.Secur32.EXTENDED_NAME_FORMAT;
27  import com.sun.jna.platform.win32.Secur32Util;
28  
29  import java.nio.charset.StandardCharsets;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import waffle.windows.auth.IWindowsAccount;
34  import waffle.windows.auth.IWindowsAuthProvider;
35  import waffle.windows.auth.IWindowsComputer;
36  import waffle.windows.auth.IWindowsDomain;
37  import waffle.windows.auth.IWindowsIdentity;
38  import waffle.windows.auth.IWindowsSecurityContext;
39  
40  /**
41   * The Class MockWindowsAuthProvider.
42   */
43  public class MockWindowsAuthProvider implements IWindowsAuthProvider {
44  
45      /** The Constant GUEST. */
46      private static final String GUEST = "Guest";
47  
48      /** The groups. */
49      private final List<String> groups = new ArrayList<>();
50  
51      /**
52       * Instantiates a new mock windows auth provider.
53       */
54      public MockWindowsAuthProvider() {
55          this.groups.add("Users");
56          this.groups.add("Everyone");
57      }
58  
59      /**
60       * Adds the group.
61       *
62       * @param name
63       *            the name
64       */
65      public void addGroup(final String name) {
66          this.groups.add(name);
67      }
68  
69      @Override
70      public IWindowsSecurityContext acceptSecurityToken(final String connectionId, final byte[] token,
71              final String securityPackage) {
72          return new MockWindowsSecurityContext(new String(token, StandardCharsets.UTF_8));
73      }
74  
75      @Override
76      public IWindowsComputer getCurrentComputer() {
77          return null;
78      }
79  
80      @Override
81      public IWindowsDomain[] getDomains() {
82          return new IWindowsDomain[0];
83      }
84  
85      @Override
86      public IWindowsIdentity logonDomainUser(final String username, final String domain, final String password) {
87          return null;
88      }
89  
90      @Override
91      public IWindowsIdentity logonDomainUserEx(final String username, final String domain, final String password,
92              final int logonType, final int logonProvider) {
93          return null;
94      }
95  
96      /**
97       * Will login the current user with any password. Will logon a "Guest" user as guest.
98       *
99       * @param username
100      *            the username
101      * @param password
102      *            the password
103      *
104      * @return the i windows identity
105      */
106     @Override
107     public IWindowsIdentity logonUser(final String username, final String password) {
108         final String currentUsername = Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
109         if (username.equals(currentUsername)) {
110             return new MockWindowsIdentity(currentUsername, this.groups);
111         } else if (username.equals(MockWindowsAuthProvider.GUEST)) {
112             return new MockWindowsIdentity(MockWindowsAuthProvider.GUEST, this.groups);
113         } else {
114             throw new RuntimeException("Mock error: " + username);
115         }
116     }
117 
118     @Override
119     public IWindowsAccount lookupAccount(final String username) {
120         return null;
121     }
122 
123     @Override
124     public void resetSecurityToken(final String connectionId) {
125         // Do Nothing
126     }
127 
128 }