001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.auth; 028 029import java.io.Serializable; 030import java.security.Principal; 031import java.util.Locale; 032 033import org.apache.http.annotation.Contract; 034import org.apache.http.annotation.ThreadingBehavior; 035import org.apache.http.util.Args; 036import org.apache.http.util.LangUtils; 037 038/** 039 * {@link Credentials} implementation for Microsoft Windows platforms that includes 040 * Windows specific attributes such as name of the domain the user belongs to. 041 * 042 * @since 4.0 043 */ 044@Contract(threading = ThreadingBehavior.IMMUTABLE) 045public class NTCredentials implements Credentials, Serializable { 046 047 private static final long serialVersionUID = -7385699315228907265L; 048 049 /** The user principal */ 050 private final NTUserPrincipal principal; 051 052 /** Password */ 053 private final String password; 054 055 /** The host the authentication request is originating from. */ 056 private final String workstation; 057 058 /** 059 * The constructor with the fully qualified username and password combined 060 * string argument. 061 * 062 * @param usernamePassword the domain/username:password formed string 063 * @deprecated (4.5) will be replaced with {@code String}, {@code char[]} in 5.0 064 */ 065 @Deprecated 066 public NTCredentials(final String usernamePassword) { 067 super(); 068 Args.notNull(usernamePassword, "Username:password string"); 069 final String username; 070 final int atColon = usernamePassword.indexOf(':'); 071 if (atColon >= 0) { 072 username = usernamePassword.substring(0, atColon); 073 this.password = usernamePassword.substring(atColon + 1); 074 } else { 075 username = usernamePassword; 076 this.password = null; 077 } 078 final int atSlash = username.indexOf('/'); 079 if (atSlash >= 0) { 080 this.principal = new NTUserPrincipal( 081 username.substring(0, atSlash).toUpperCase(Locale.ROOT), 082 username.substring(atSlash + 1)); 083 } else { 084 this.principal = new NTUserPrincipal( 085 null, 086 username.substring(atSlash + 1)); 087 } 088 this.workstation = null; 089 } 090 091 /** 092 * Constructor. 093 * @param userName The user name. This should not include the domain to authenticate with. 094 * For example: "user" is correct whereas "DOMAIN\user" is not. 095 * @param password The password. 096 * @param workstation The workstation the authentication request is originating from. 097 * Essentially, the computer name for this machine. 098 * @param domain The domain to authenticate within. 099 */ 100 public NTCredentials( 101 final String userName, 102 final String password, 103 final String workstation, 104 final String domain) { 105 super(); 106 Args.notNull(userName, "User name"); 107 this.principal = new NTUserPrincipal(domain, userName); 108 this.password = password; 109 if (workstation != null) { 110 this.workstation = workstation.toUpperCase(Locale.ROOT); 111 } else { 112 this.workstation = null; 113 } 114 } 115 116 @Override 117 public Principal getUserPrincipal() { 118 return this.principal; 119 } 120 121 public String getUserName() { 122 return this.principal.getUsername(); 123 } 124 125 @Override 126 public String getPassword() { 127 return this.password; 128 } 129 130 /** 131 * Retrieves the name to authenticate with. 132 * 133 * @return String the domain these credentials are intended to authenticate with. 134 */ 135 public String getDomain() { 136 return this.principal.getDomain(); 137 } 138 139 /** 140 * Retrieves the workstation name of the computer originating the request. 141 * 142 * @return String the workstation the user is logged into. 143 */ 144 public String getWorkstation() { 145 return this.workstation; 146 } 147 148 @Override 149 public int hashCode() { 150 int hash = LangUtils.HASH_SEED; 151 hash = LangUtils.hashCode(hash, this.principal); 152 hash = LangUtils.hashCode(hash, this.workstation); 153 return hash; 154 } 155 156 @Override 157 public boolean equals(final Object o) { 158 if (this == o) { 159 return true; 160 } 161 if (o instanceof NTCredentials) { 162 final NTCredentials that = (NTCredentials) o; 163 if (LangUtils.equals(this.principal, that.principal) 164 && LangUtils.equals(this.workstation, that.workstation)) { 165 return true; 166 } 167 } 168 return false; 169 } 170 171 @Override 172 public String toString() { 173 final StringBuilder buffer = new StringBuilder(); 174 buffer.append("[principal: "); 175 buffer.append(this.principal); 176 buffer.append("][workstation: "); 177 buffer.append(this.workstation); 178 buffer.append("]"); 179 return buffer.toString(); 180 } 181 182}