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.windows.auth.impl;
25  
26  import com.sun.jna.platform.win32.Secur32;
27  import com.sun.jna.platform.win32.Sspi;
28  import com.sun.jna.platform.win32.Sspi.CredHandle;
29  import com.sun.jna.platform.win32.Sspi.TimeStamp;
30  import com.sun.jna.platform.win32.Win32Exception;
31  import com.sun.jna.platform.win32.WinError;
32  
33  import waffle.windows.auth.IWindowsCredentialsHandle;
34  
35  /**
36   * Pre-existing credentials of a security principal. This is a handle to a previously authenticated logon data used by a
37   * security principal to establish its own identity, such as a password, or a Kerberos protocol ticket.
38   */
39  public class WindowsCredentialsHandleImpl implements IWindowsCredentialsHandle {
40  
41      /** The principal name. */
42      private final String principalName;
43  
44      /** The credentials type. */
45      private final int credentialsType;
46  
47      /** The security package. */
48      private final String securityPackage;
49  
50      /** The handle. */
51      private CredHandle handle;
52  
53      /**
54       * A new Windows credentials handle.
55       *
56       * @param newPrincipalName
57       *            Principal name.
58       * @param newCredentialsType
59       *            Credentials type.
60       * @param newSecurityPackage
61       *            Security package.
62       */
63      public WindowsCredentialsHandleImpl(final String newPrincipalName, final int newCredentialsType,
64              final String newSecurityPackage) {
65          this.principalName = newPrincipalName;
66          this.credentialsType = newCredentialsType;
67          this.securityPackage = newSecurityPackage;
68      }
69  
70      /**
71       * Returns the current credentials handle.
72       *
73       * @param securityPackage
74       *            Security package, eg. "Negotiate".
75       *
76       * @return A windows credentials handle
77       */
78      public static IWindowsCredentialsHandle getCurrent(final String securityPackage) {
79          final IWindowsCredentialsHandle handle = new WindowsCredentialsHandleImpl(null, Sspi.SECPKG_CRED_OUTBOUND,
80                  securityPackage);
81          handle.initialize();
82          return handle;
83      }
84  
85      /**
86       * Initialize a new credentials handle.
87       */
88      @Override
89      public void initialize() {
90          this.handle = new CredHandle();
91          final TimeStamp clientLifetime = new TimeStamp();
92          final int rc = Secur32.INSTANCE.AcquireCredentialsHandle(this.principalName, this.securityPackage,
93                  this.credentialsType, null, null, null, null, this.handle, clientLifetime);
94          if (WinError.SEC_E_OK != rc) {
95              throw new Win32Exception(rc);
96          }
97      }
98  
99      /**
100      * Dispose of the credentials handle.
101      */
102     @Override
103     public void dispose() {
104         if (this.handle != null && !this.handle.isNull()) {
105             final int rc = Secur32.INSTANCE.FreeCredentialsHandle(this.handle);
106             if (WinError.SEC_E_OK != rc) {
107                 throw new Win32Exception(rc);
108             }
109         }
110     }
111 
112     /**
113      * Get CredHandle.
114      *
115      * @return the handle
116      */
117     @Override
118     public CredHandle getHandle() {
119         return this.handle;
120     }
121 }