1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.http.impl.auth.win;
29
30 import java.io.Serializable;
31 import java.security.Principal;
32
33 import org.apache.http.annotation.Contract;
34 import org.apache.http.annotation.ThreadingBehavior;
35 import org.apache.http.auth.Credentials;
36
37 import com.sun.jna.platform.win32.Secur32.EXTENDED_NAME_FORMAT;
38 import com.sun.jna.platform.win32.Secur32Util;
39
40 /**
41 * Returns the current Windows user credentials
42 * <p>
43 * EXPERIMENTAL
44 * </p>
45 *
46 * @since 4.4
47 */
48 @Contract(threading = ThreadingBehavior.IMMUTABLE)
49 public final class CurrentWindowsCredentials implements Credentials, Serializable, Principal {
50
51 private static final long serialVersionUID = 4361166468529298169L;
52
53 public static final CurrentWindowsCredentialssCredentials.html#CurrentWindowsCredentials">CurrentWindowsCredentials INSTANCE = new CurrentWindowsCredentials();
54
55 /**
56 * Get the SAM-compatible username of the currently logged-on user.
57 *
58 * @return String.
59 */
60 public static String getCurrentUsername() {
61 return Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
62 }
63
64 private CurrentWindowsCredentials() {
65 }
66
67 @Override
68 public Principal getUserPrincipal() {
69 return this;
70 }
71
72 @Override
73 public int hashCode() {
74 return getClass().hashCode();
75 }
76
77 @Override
78 public boolean equals(final Object o) {
79 if (this == o) {
80 return true;
81 }
82 if (o == null) {
83 return false;
84 }
85 return getClass().equals(o.getClass());
86 }
87
88 @Override
89 public String toString() {
90 return getCurrentUsername();
91 }
92
93 /**
94 * Returns an empty password
95 */
96 @Override
97 public String getPassword() {
98 return "";
99 }
100
101 @Override
102 public String getName() {
103 return getCurrentUsername();
104 }
105
106 }
107
108
109