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.Sspi.CtxtHandle;
27  import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import waffle.windows.auth.IWindowsIdentity;
33  import waffle.windows.auth.IWindowsImpersonationContext;
34  import waffle.windows.auth.IWindowsSecurityContext;
35  
36  /**
37   * The Class MockWindowsSecurityContext.
38   */
39  public class MockWindowsSecurityContext implements IWindowsSecurityContext {
40  
41      /** The identity. */
42      private final IWindowsIdentity identity;
43  
44      /**
45       * Instantiates a new mock windows security context.
46       *
47       * @param username
48       *            the username
49       */
50      public MockWindowsSecurityContext(final String username) {
51          final List<String> groups = new ArrayList<>();
52          groups.add("Users");
53          groups.add("Everyone");
54          this.identity = new MockWindowsIdentity(username, groups);
55      }
56  
57      @Override
58      public void dispose() {
59          // Do Nothing
60      }
61  
62      @Override
63      public boolean isContinue() {
64          return false;
65      }
66  
67      @Override
68      public CtxtHandle getHandle() {
69          return new CtxtHandle();
70      }
71  
72      @Override
73      public IWindowsIdentity getIdentity() {
74          return this.identity;
75      }
76  
77      @Override
78      public String getPrincipalName() {
79          return this.identity.getFqn();
80      }
81  
82      @Override
83      public String getSecurityPackage() {
84          return "Mock";
85      }
86  
87      @Override
88      public byte[] getToken() {
89          return new byte[0];
90      }
91  
92      @Override
93      public IWindowsImpersonationContext impersonate() {
94          return new MockWindowsImpersonationContext();
95      }
96  
97      /**
98       * Initialize.
99       */
100     public void initialize() {
101         // Do Nothing
102     }
103 
104     @Override
105     public void initialize(final CtxtHandle continueCtx, final SecBufferDesc continueToken,
106             final String targetPrincipalName) {
107         // Do Nothing
108     }
109 
110 }