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.windows.auth.impl;
15  
16  import com.sun.jna.platform.win32.Advapi32;
17  import com.sun.jna.platform.win32.Kernel32;
18  import com.sun.jna.platform.win32.Win32Exception;
19  import com.sun.jna.platform.win32.WinNT.HANDLE;
20  
21  import waffle.windows.auth.IWindowsImpersonationContext;
22  
23  /**
24   * The Class WindowsIdentityImpersonationContextImpl.
25   *
26   * @author dblock[at]dblock[dot]org
27   */
28  public class WindowsIdentityImpersonationContextImpl implements IWindowsImpersonationContext {
29  
30      /**
31       * Impersonate a logged on user.
32       * 
33       * @param windowsIdentity
34       *            Windows identity obtained via LogonUser.
35       */
36      public WindowsIdentityImpersonationContextImpl(final HANDLE windowsIdentity) {
37          if (!Advapi32.INSTANCE.ImpersonateLoggedOnUser(windowsIdentity)) {
38              throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
39          }
40      }
41  
42      /* (non-Javadoc)
43       * @see waffle.windows.auth.IWindowsImpersonationContext#revertToSelf()
44       */
45      @Override
46      public void revertToSelf() {
47          Advapi32.INSTANCE.RevertToSelf();
48      }
49  }