Example usage for org.apache.http.impl.auth.win CurrentWindowsCredentials getCurrentUsername

List of usage examples for org.apache.http.impl.auth.win CurrentWindowsCredentials getCurrentUsername

Introduction

In this page you can find the example usage for org.apache.http.impl.auth.win CurrentWindowsCredentials getCurrentUsername.

Prototype

public static String getCurrentUsername() 

Source Link

Document

Get the SAM-compatible username of the currently logged-on user.

Usage

From source file:org.apache.http.impl.auth.win.WindowsNegotiateScheme.java

@Override
public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context)
        throws AuthenticationException {

    final String response;
    if (clientCred == null) {
        // ?? We don't use the credentials, should we allow anything?
        if (!(credentials instanceof CurrentWindowsCredentials)) {
            throw new InvalidCredentialsException("Credentials cannot be used for " + getSchemeName()
                    + " authentication: " + credentials.getClass().getName());
        }/*w  w  w  .  j  a v a2 s  .  c  om*/

        // client credentials handle
        try {
            final String username = CurrentWindowsCredentials.getCurrentUsername();
            final TimeStamp lifetime = new TimeStamp();

            clientCred = new CredHandle();
            final int rc = Secur32.INSTANCE.AcquireCredentialsHandle(username, scheme,
                    Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null, clientCred, lifetime);

            if (WinError.SEC_E_OK != rc) {
                throw new Win32Exception(rc);
            }

            response = getToken(null, null, username);
        } catch (Throwable t) {
            dispose();
            throw new AuthenticationException("Authentication Failed", t);
        }
    } else if (this.challenge == null || this.challenge.length() == 0) {
        dispose();
        throw new AuthenticationException("Authentication Failed");
    } else {
        try {
            final byte[] continueTokenBytes = Base64.decodeBase64(this.challenge);
            final SecBufferDesc continueTokenBuffer = new SecBufferDesc(Sspi.SECBUFFER_TOKEN,
                    continueTokenBytes);
            response = getToken(this.sppicontext, continueTokenBuffer, "localhost");
        } catch (Throwable t) {
            dispose();
            throw new AuthenticationException("Authentication Failed", t);
        }
    }

    final CharArrayBuffer buffer = new CharArrayBuffer(scheme.length() + 30);
    if (isProxy()) {
        buffer.append(AUTH.PROXY_AUTH_RESP);
    } else {
        buffer.append(AUTH.WWW_AUTH_RESP);
    }
    buffer.append(": ");
    buffer.append(scheme); // NTLM or Negotiate
    buffer.append(" ");
    buffer.append(response);
    return new BufferedHeader(buffer);
}