View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
13   */
14  package waffle.mock;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import waffle.windows.auth.IWindowsIdentity;
20  import waffle.windows.auth.IWindowsImpersonationContext;
21  import waffle.windows.auth.IWindowsSecurityContext;
22  
23  import com.sun.jna.platform.win32.Sspi.CtxtHandle;
24  import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
25  
26  /**
27   * The Class MockWindowsSecurityContext.
28   *
29   * @author dblock[at]dblock[dot]org
30   */
31  public class MockWindowsSecurityContext implements IWindowsSecurityContext {
32  
33      /** The identity. */
34      private final IWindowsIdentity identity;
35  
36      /**
37       * Instantiates a new mock windows security context.
38       *
39       * @param username
40       *            the username
41       */
42      public MockWindowsSecurityContext(final String username) {
43          final List<String> groups = new ArrayList<String>();
44          groups.add("Users");
45          groups.add("Everyone");
46          this.identity = new MockWindowsIdentity(username, groups);
47      }
48  
49      /* (non-Javadoc)
50       * @see waffle.windows.auth.IWindowsSecurityContext#dispose()
51       */
52      @Override
53      public void dispose() {
54          // Do Nothing
55      }
56  
57      /* (non-Javadoc)
58       * @see waffle.windows.auth.IWindowsSecurityContext#isContinue()
59       */
60      @Override
61      public boolean isContinue() {
62          return false;
63      }
64  
65      /* (non-Javadoc)
66       * @see waffle.windows.auth.IWindowsSecurityContext#getHandle()
67       */
68      @Override
69      public CtxtHandle getHandle() {
70          return new CtxtHandle();
71      }
72  
73      /* (non-Javadoc)
74       * @see waffle.windows.auth.IWindowsSecurityContext#getIdentity()
75       */
76      @Override
77      public IWindowsIdentity getIdentity() {
78          return this.identity;
79      }
80  
81      /* (non-Javadoc)
82       * @see waffle.windows.auth.IWindowsSecurityContext#getPrincipalName()
83       */
84      @Override
85      public String getPrincipalName() {
86          return this.identity.getFqn();
87      }
88  
89      /* (non-Javadoc)
90       * @see waffle.windows.auth.IWindowsSecurityContext#getSecurityPackage()
91       */
92      @Override
93      public String getSecurityPackage() {
94          return "Mock";
95      }
96  
97      /* (non-Javadoc)
98       * @see waffle.windows.auth.IWindowsSecurityContext#getToken()
99       */
100     @Override
101     public byte[] getToken() {
102         return new byte[0];
103     }
104 
105     /* (non-Javadoc)
106      * @see waffle.windows.auth.IWindowsSecurityContext#impersonate()
107      */
108     @Override
109     public IWindowsImpersonationContext impersonate() {
110         return new MockWindowsImpersonationContext();
111     }
112 
113     /**
114      * Initialize.
115      */
116     public void initialize() {
117         // Do Nothing
118     }
119 
120     /* (non-Javadoc)
121      * @see waffle.windows.auth.IWindowsSecurityContext#initialize(com.sun.jna.platform.win32.Sspi.CtxtHandle, com.sun.jna.platform.win32.Sspi.SecBufferDesc, java.lang.String)
122      */
123     @Override
124     public void initialize(final CtxtHandle continueCtx, final SecBufferDesc continueToken,
125             final String targetPrincipalName) {
126         // Do Nothing
127     }
128 }