View Javadoc
1   package org.metricshub.wmi.wbem;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2023 - 2025 MetricsHub
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
21   */
22  
23  import com.sun.jna.Memory;
24  import com.sun.jna.Native;
25  import com.sun.jna.Pointer;
26  import com.sun.jna.Structure;
27  
28  /**
29   * COAUTHIDENTITY struct
30   * <p>
31   * See https://docs.microsoft.com/en-us/windows/win32/api/wtypesbase/ns-wtypesbase-coauthidentity
32   */
33  @Structure.FieldOrder({ "user", "userLength", "domain", "domainLength", "password", "passwordLength", "flags" })
34  public class CoAuthIdentity extends Structure {
35  
36  	public Pointer user;
37  	public int userLength;
38  	public Pointer domain;
39  	public int domainLength;
40  	public Pointer password;
41  	public int passwordLength;
42  	public int flags;
43  
44  	/**
45  	 * Creates a new COAUTHIDENTITY struct from the specified username and password
46  	 * @param username username, or domain\\username, or username@domain
47  	 * @param password associated password
48  	 */
49  	public CoAuthIdentity(final String username, final char[] password) {
50  		String domain = null;
51  		String user = username;
52  
53  		// Extract domain from username, which can be domain\\user or
54  		// user@domain
55  		final int backslashIndex = username.indexOf('\\');
56  		if (backslashIndex > -1) {
57  			domain = username.substring(0, backslashIndex);
58  			user = username.substring(backslashIndex + 1);
59  		} else {
60  			final int atIndex = username.indexOf('@');
61  			if (atIndex > -1) {
62  				user = username.substring(0, atIndex);
63  				domain = username.substring(atIndex + 1);
64  			}
65  		}
66  
67  		// Sets the user field (native)
68  		this.user = new Memory(Native.WCHAR_SIZE * (user.length() + 1L));
69  		this.user.setWideString(0, user);
70  		this.userLength = user.length();
71  
72  		// Sets the domain field
73  		if (domain != null) {
74  			this.domain = new Memory(Native.WCHAR_SIZE * (domain.length() + 1L));
75  			this.domain.setWideString(0, domain);
76  			this.domainLength = domain.length();
77  		} else {
78  			this.domain = null;
79  			this.domainLength = 0;
80  		}
81  
82  		// Sets the password field (native)
83  		// Note: too bad we couldn't manage to copy the password as a char array
84  		// without creating a String object
85  		if (password != null) {
86  			this.password = new Memory(Native.WCHAR_SIZE * (password.length + 1L));
87  			this.password.setWideString(0, String.valueOf(password));
88  			this.passwordLength = password.length;
89  		} else {
90  			this.password = null;
91  			this.passwordLength = 0;
92  		}
93  
94  		// Unicode
95  		this.flags = 0x2;
96  	}
97  }