michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.auth; michael@0: michael@0: import java.io.Serializable; michael@0: import java.security.Principal; michael@0: import java.util.Locale; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.util.LangUtils; michael@0: michael@0: /** michael@0: * {@link Credentials} implementation for Microsoft Windows platforms that includes michael@0: * Windows specific attributes such as name of the domain the user belongs to. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class NTCredentials implements Credentials, Serializable { michael@0: michael@0: private static final long serialVersionUID = -7385699315228907265L; michael@0: michael@0: /** The user principal */ michael@0: private final NTUserPrincipal principal; michael@0: michael@0: /** Password */ michael@0: private final String password; michael@0: michael@0: /** The host the authentication request is originating from. */ michael@0: private final String workstation; michael@0: michael@0: /** michael@0: * The constructor with the fully qualified username and password combined michael@0: * string argument. michael@0: * michael@0: * @param usernamePassword the domain/username:password formed string michael@0: */ michael@0: public NTCredentials(String usernamePassword) { michael@0: super(); michael@0: if (usernamePassword == null) { michael@0: throw new IllegalArgumentException("Username:password string may not be null"); michael@0: } michael@0: String username; michael@0: int atColon = usernamePassword.indexOf(':'); michael@0: if (atColon >= 0) { michael@0: username = usernamePassword.substring(0, atColon); michael@0: this.password = usernamePassword.substring(atColon + 1); michael@0: } else { michael@0: username = usernamePassword; michael@0: this.password = null; michael@0: } michael@0: int atSlash = username.indexOf('/'); michael@0: if (atSlash >= 0) { michael@0: this.principal = new NTUserPrincipal( michael@0: username.substring(0, atSlash).toUpperCase(Locale.ENGLISH), michael@0: username.substring(atSlash + 1)); michael@0: } else { michael@0: this.principal = new NTUserPrincipal( michael@0: null, michael@0: username.substring(atSlash + 1)); michael@0: } michael@0: this.workstation = null; michael@0: } michael@0: michael@0: /** michael@0: * Constructor. michael@0: * @param userName The user name. This should not include the domain to authenticate with. michael@0: * For example: "user" is correct whereas "DOMAIN\\user" is not. michael@0: * @param password The password. michael@0: * @param workstation The workstation the authentication request is originating from. michael@0: * Essentially, the computer name for this machine. michael@0: * @param domain The domain to authenticate within. michael@0: */ michael@0: public NTCredentials( michael@0: final String userName, michael@0: final String password, michael@0: final String workstation, michael@0: final String domain) { michael@0: super(); michael@0: if (userName == null) { michael@0: throw new IllegalArgumentException("User name may not be null"); michael@0: } michael@0: this.principal = new NTUserPrincipal(domain, userName); michael@0: this.password = password; michael@0: if (workstation != null) { michael@0: this.workstation = workstation.toUpperCase(Locale.ENGLISH); michael@0: } else { michael@0: this.workstation = null; michael@0: } michael@0: } michael@0: michael@0: public Principal getUserPrincipal() { michael@0: return this.principal; michael@0: } michael@0: michael@0: public String getUserName() { michael@0: return this.principal.getUsername(); michael@0: } michael@0: michael@0: public String getPassword() { michael@0: return this.password; michael@0: } michael@0: michael@0: /** michael@0: * Retrieves the name to authenticate with. michael@0: * michael@0: * @return String the domain these credentials are intended to authenticate with. michael@0: */ michael@0: public String getDomain() { michael@0: return this.principal.getDomain(); michael@0: } michael@0: michael@0: /** michael@0: * Retrieves the workstation name of the computer originating the request. michael@0: * michael@0: * @return String the workstation the user is logged into. michael@0: */ michael@0: public String getWorkstation() { michael@0: return this.workstation; michael@0: } michael@0: michael@0: @Override michael@0: public int hashCode() { michael@0: int hash = LangUtils.HASH_SEED; michael@0: hash = LangUtils.hashCode(hash, this.principal); michael@0: hash = LangUtils.hashCode(hash, this.workstation); michael@0: return hash; michael@0: } michael@0: michael@0: @Override michael@0: public boolean equals(Object o) { michael@0: if (this == o) return true; michael@0: if (o instanceof NTCredentials) { michael@0: NTCredentials that = (NTCredentials) o; michael@0: if (LangUtils.equals(this.principal, that.principal) michael@0: && LangUtils.equals(this.workstation, that.workstation)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: StringBuilder buffer = new StringBuilder(); michael@0: buffer.append("[principal: "); michael@0: buffer.append(this.principal); michael@0: buffer.append("][workstation: "); michael@0: buffer.append(this.workstation); michael@0: buffer.append("]"); michael@0: return buffer.toString(); michael@0: } michael@0: michael@0: }