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.Secur32;
17  import com.sun.jna.platform.win32.Win32Exception;
18  import com.sun.jna.platform.win32.Sspi.CtxtHandle;
19  import com.sun.jna.platform.win32.WinError;
20  
21  import waffle.windows.auth.IWindowsImpersonationContext;
22  
23  /**
24   * The Class WindowsSecurityContextImpersonationContextImpl.
25   *
26   * @author dblock[at]dblock[dot]org
27   */
28  public class WindowsSecurityContextImpersonationContextImpl implements IWindowsImpersonationContext {
29  
30      /** The ctx. */
31      private final CtxtHandle ctx;
32  
33      /**
34       * Instantiates a new windows security context impersonation context impl.
35       *
36       * @param newCtx
37       *            the new ctx
38       */
39      public WindowsSecurityContextImpersonationContextImpl(final CtxtHandle newCtx) {
40          final int rc = Secur32.INSTANCE.ImpersonateSecurityContext(newCtx);
41          if (rc != WinError.SEC_E_OK) {
42              throw new Win32Exception(rc);
43          }
44  
45          this.ctx = newCtx;
46      }
47  
48      /* (non-Javadoc)
49       * @see waffle.windows.auth.IWindowsImpersonationContext#revertToSelf()
50       */
51      @Override
52      public void revertToSelf() {
53          final int rc = Secur32.INSTANCE.RevertSecurityContext(this.ctx);
54          if (rc != WinError.SEC_E_OK) {
55              throw new Win32Exception(rc);
56          }
57      }
58  }