1
2
3
4
5
6
7
8
9
10
11
12
13
14 package waffle.windows.auth.impl;
15
16 import waffle.windows.auth.IWindowsCredentialsHandle;
17
18 import com.sun.jna.platform.win32.Secur32;
19 import com.sun.jna.platform.win32.Sspi;
20 import com.sun.jna.platform.win32.Sspi.CredHandle;
21 import com.sun.jna.platform.win32.Sspi.TimeStamp;
22 import com.sun.jna.platform.win32.Win32Exception;
23 import com.sun.jna.platform.win32.WinError;
24
25
26
27
28
29
30
31 public class WindowsCredentialsHandleImpl implements IWindowsCredentialsHandle {
32
33
34 private final String principalName;
35
36
37 private final int credentialsType;
38
39
40 private final String securityPackage;
41
42
43 private CredHandle handle;
44
45
46 private TimeStamp clientLifetime;
47
48
49
50
51
52
53
54
55
56
57
58 public WindowsCredentialsHandleImpl(final String newPrincipalName, final int newCredentialsType,
59 final String newSecurityPackage) {
60 this.principalName = newPrincipalName;
61 this.credentialsType = newCredentialsType;
62 this.securityPackage = newSecurityPackage;
63 }
64
65
66
67
68
69
70
71
72 public static IWindowsCredentialsHandle getCurrent(final String securityPackage) {
73 final IWindowsCredentialsHandle handle = new WindowsCredentialsHandleImpl(null, Sspi.SECPKG_CRED_OUTBOUND,
74 securityPackage);
75 handle.initialize();
76 return handle;
77 }
78
79
80
81
82 @Override
83 public void initialize() {
84 this.handle = new CredHandle();
85 this.clientLifetime = new TimeStamp();
86 final int rc = Secur32.INSTANCE.AcquireCredentialsHandle(this.principalName, this.securityPackage,
87 this.credentialsType, null, null, null, null, this.handle, this.clientLifetime);
88 if (WinError.SEC_E_OK != rc) {
89 throw new Win32Exception(rc);
90 }
91 }
92
93
94
95
96 @Override
97 public void dispose() {
98 if (this.handle != null && !this.handle.isNull()) {
99 final int rc = Secur32.INSTANCE.FreeCredentialsHandle(this.handle);
100 if (WinError.SEC_E_OK != rc) {
101 throw new Win32Exception(rc);
102 }
103 }
104 }
105
106
107
108
109
110
111 @Override
112 public CredHandle getHandle() {
113 return this.handle;
114 }
115 }