mobile/android/thirdparty/ch/boye/httpclientandroidlib/auth/NTCredentials.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 *
michael@0 4 * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0 5 * contributor license agreements. See the NOTICE file distributed with
michael@0 6 * this work for additional information regarding copyright ownership.
michael@0 7 * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0 8 * (the "License"); you may not use this file except in compliance with
michael@0 9 * the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing, software
michael@0 14 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 16 * See the License for the specific language governing permissions and
michael@0 17 * limitations under the License.
michael@0 18 * ====================================================================
michael@0 19 *
michael@0 20 * This software consists of voluntary contributions made by many
michael@0 21 * individuals on behalf of the Apache Software Foundation. For more
michael@0 22 * information on the Apache Software Foundation, please see
michael@0 23 * <http://www.apache.org/>.
michael@0 24 *
michael@0 25 */
michael@0 26
michael@0 27 package ch.boye.httpclientandroidlib.auth;
michael@0 28
michael@0 29 import java.io.Serializable;
michael@0 30 import java.security.Principal;
michael@0 31 import java.util.Locale;
michael@0 32
michael@0 33 import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0 34
michael@0 35 import ch.boye.httpclientandroidlib.util.LangUtils;
michael@0 36
michael@0 37 /**
michael@0 38 * {@link Credentials} implementation for Microsoft Windows platforms that includes
michael@0 39 * Windows specific attributes such as name of the domain the user belongs to.
michael@0 40 *
michael@0 41 * @since 4.0
michael@0 42 */
michael@0 43 @Immutable
michael@0 44 public class NTCredentials implements Credentials, Serializable {
michael@0 45
michael@0 46 private static final long serialVersionUID = -7385699315228907265L;
michael@0 47
michael@0 48 /** The user principal */
michael@0 49 private final NTUserPrincipal principal;
michael@0 50
michael@0 51 /** Password */
michael@0 52 private final String password;
michael@0 53
michael@0 54 /** The host the authentication request is originating from. */
michael@0 55 private final String workstation;
michael@0 56
michael@0 57 /**
michael@0 58 * The constructor with the fully qualified username and password combined
michael@0 59 * string argument.
michael@0 60 *
michael@0 61 * @param usernamePassword the domain/username:password formed string
michael@0 62 */
michael@0 63 public NTCredentials(String usernamePassword) {
michael@0 64 super();
michael@0 65 if (usernamePassword == null) {
michael@0 66 throw new IllegalArgumentException("Username:password string may not be null");
michael@0 67 }
michael@0 68 String username;
michael@0 69 int atColon = usernamePassword.indexOf(':');
michael@0 70 if (atColon >= 0) {
michael@0 71 username = usernamePassword.substring(0, atColon);
michael@0 72 this.password = usernamePassword.substring(atColon + 1);
michael@0 73 } else {
michael@0 74 username = usernamePassword;
michael@0 75 this.password = null;
michael@0 76 }
michael@0 77 int atSlash = username.indexOf('/');
michael@0 78 if (atSlash >= 0) {
michael@0 79 this.principal = new NTUserPrincipal(
michael@0 80 username.substring(0, atSlash).toUpperCase(Locale.ENGLISH),
michael@0 81 username.substring(atSlash + 1));
michael@0 82 } else {
michael@0 83 this.principal = new NTUserPrincipal(
michael@0 84 null,
michael@0 85 username.substring(atSlash + 1));
michael@0 86 }
michael@0 87 this.workstation = null;
michael@0 88 }
michael@0 89
michael@0 90 /**
michael@0 91 * Constructor.
michael@0 92 * @param userName The user name. This should not include the domain to authenticate with.
michael@0 93 * For example: "user" is correct whereas "DOMAIN\\user" is not.
michael@0 94 * @param password The password.
michael@0 95 * @param workstation The workstation the authentication request is originating from.
michael@0 96 * Essentially, the computer name for this machine.
michael@0 97 * @param domain The domain to authenticate within.
michael@0 98 */
michael@0 99 public NTCredentials(
michael@0 100 final String userName,
michael@0 101 final String password,
michael@0 102 final String workstation,
michael@0 103 final String domain) {
michael@0 104 super();
michael@0 105 if (userName == null) {
michael@0 106 throw new IllegalArgumentException("User name may not be null");
michael@0 107 }
michael@0 108 this.principal = new NTUserPrincipal(domain, userName);
michael@0 109 this.password = password;
michael@0 110 if (workstation != null) {
michael@0 111 this.workstation = workstation.toUpperCase(Locale.ENGLISH);
michael@0 112 } else {
michael@0 113 this.workstation = null;
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 public Principal getUserPrincipal() {
michael@0 118 return this.principal;
michael@0 119 }
michael@0 120
michael@0 121 public String getUserName() {
michael@0 122 return this.principal.getUsername();
michael@0 123 }
michael@0 124
michael@0 125 public String getPassword() {
michael@0 126 return this.password;
michael@0 127 }
michael@0 128
michael@0 129 /**
michael@0 130 * Retrieves the name to authenticate with.
michael@0 131 *
michael@0 132 * @return String the domain these credentials are intended to authenticate with.
michael@0 133 */
michael@0 134 public String getDomain() {
michael@0 135 return this.principal.getDomain();
michael@0 136 }
michael@0 137
michael@0 138 /**
michael@0 139 * Retrieves the workstation name of the computer originating the request.
michael@0 140 *
michael@0 141 * @return String the workstation the user is logged into.
michael@0 142 */
michael@0 143 public String getWorkstation() {
michael@0 144 return this.workstation;
michael@0 145 }
michael@0 146
michael@0 147 @Override
michael@0 148 public int hashCode() {
michael@0 149 int hash = LangUtils.HASH_SEED;
michael@0 150 hash = LangUtils.hashCode(hash, this.principal);
michael@0 151 hash = LangUtils.hashCode(hash, this.workstation);
michael@0 152 return hash;
michael@0 153 }
michael@0 154
michael@0 155 @Override
michael@0 156 public boolean equals(Object o) {
michael@0 157 if (this == o) return true;
michael@0 158 if (o instanceof NTCredentials) {
michael@0 159 NTCredentials that = (NTCredentials) o;
michael@0 160 if (LangUtils.equals(this.principal, that.principal)
michael@0 161 && LangUtils.equals(this.workstation, that.workstation)) {
michael@0 162 return true;
michael@0 163 }
michael@0 164 }
michael@0 165 return false;
michael@0 166 }
michael@0 167
michael@0 168 @Override
michael@0 169 public String toString() {
michael@0 170 StringBuilder buffer = new StringBuilder();
michael@0 171 buffer.append("[principal: ");
michael@0 172 buffer.append(this.principal);
michael@0 173 buffer.append("][workstation: ");
michael@0 174 buffer.append(this.workstation);
michael@0 175 buffer.append("]");
michael@0 176 return buffer.toString();
michael@0 177 }
michael@0 178
michael@0 179 }

mercurial