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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/auth/NTCredentials.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.auth;
    1.31 +
    1.32 +import java.io.Serializable;
    1.33 +import java.security.Principal;
    1.34 +import java.util.Locale;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.annotation.Immutable;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.util.LangUtils;
    1.39 +
    1.40 +/**
    1.41 + * {@link Credentials} implementation for Microsoft Windows platforms that includes
    1.42 + * Windows specific attributes such as name of the domain the user belongs to.
    1.43 + *
    1.44 + * @since 4.0
    1.45 + */
    1.46 +@Immutable
    1.47 +public class NTCredentials implements Credentials, Serializable {
    1.48 +
    1.49 +    private static final long serialVersionUID = -7385699315228907265L;
    1.50 +
    1.51 +    /** The user principal  */
    1.52 +    private final NTUserPrincipal principal;
    1.53 +
    1.54 +    /** Password */
    1.55 +    private final String password;
    1.56 +
    1.57 +    /** The host the authentication request is originating from.  */
    1.58 +    private final String workstation;
    1.59 +
    1.60 +    /**
    1.61 +     * The constructor with the fully qualified username and password combined
    1.62 +     * string argument.
    1.63 +     *
    1.64 +     * @param usernamePassword the domain/username:password formed string
    1.65 +     */
    1.66 +    public NTCredentials(String usernamePassword) {
    1.67 +        super();
    1.68 +        if (usernamePassword == null) {
    1.69 +            throw new IllegalArgumentException("Username:password string may not be null");
    1.70 +        }
    1.71 +        String username;
    1.72 +        int atColon = usernamePassword.indexOf(':');
    1.73 +        if (atColon >= 0) {
    1.74 +            username = usernamePassword.substring(0, atColon);
    1.75 +            this.password = usernamePassword.substring(atColon + 1);
    1.76 +        } else {
    1.77 +            username = usernamePassword;
    1.78 +            this.password = null;
    1.79 +        }
    1.80 +        int atSlash = username.indexOf('/');
    1.81 +        if (atSlash >= 0) {
    1.82 +            this.principal = new NTUserPrincipal(
    1.83 +                    username.substring(0, atSlash).toUpperCase(Locale.ENGLISH),
    1.84 +                    username.substring(atSlash + 1));
    1.85 +        } else {
    1.86 +            this.principal = new NTUserPrincipal(
    1.87 +                    null,
    1.88 +                    username.substring(atSlash + 1));
    1.89 +        }
    1.90 +        this.workstation = null;
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Constructor.
    1.95 +     * @param userName The user name.  This should not include the domain to authenticate with.
    1.96 +     * For example: "user" is correct whereas "DOMAIN\\user" is not.
    1.97 +     * @param password The password.
    1.98 +     * @param workstation The workstation the authentication request is originating from.
    1.99 +     * Essentially, the computer name for this machine.
   1.100 +     * @param domain The domain to authenticate within.
   1.101 +     */
   1.102 +    public NTCredentials(
   1.103 +            final String userName,
   1.104 +            final String password,
   1.105 +            final String workstation,
   1.106 +            final String domain) {
   1.107 +        super();
   1.108 +        if (userName == null) {
   1.109 +            throw new IllegalArgumentException("User name may not be null");
   1.110 +        }
   1.111 +        this.principal = new NTUserPrincipal(domain, userName);
   1.112 +        this.password = password;
   1.113 +        if (workstation != null) {
   1.114 +            this.workstation = workstation.toUpperCase(Locale.ENGLISH);
   1.115 +        } else {
   1.116 +            this.workstation = null;
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    public Principal getUserPrincipal() {
   1.121 +        return this.principal;
   1.122 +    }
   1.123 +
   1.124 +    public String getUserName() {
   1.125 +        return this.principal.getUsername();
   1.126 +    }
   1.127 +
   1.128 +    public String getPassword() {
   1.129 +        return this.password;
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Retrieves the name to authenticate with.
   1.134 +     *
   1.135 +     * @return String the domain these credentials are intended to authenticate with.
   1.136 +     */
   1.137 +    public String getDomain() {
   1.138 +        return this.principal.getDomain();
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * Retrieves the workstation name of the computer originating the request.
   1.143 +     *
   1.144 +     * @return String the workstation the user is logged into.
   1.145 +     */
   1.146 +    public String getWorkstation() {
   1.147 +        return this.workstation;
   1.148 +    }
   1.149 +
   1.150 +    @Override
   1.151 +    public int hashCode() {
   1.152 +        int hash = LangUtils.HASH_SEED;
   1.153 +        hash = LangUtils.hashCode(hash, this.principal);
   1.154 +        hash = LangUtils.hashCode(hash, this.workstation);
   1.155 +        return hash;
   1.156 +    }
   1.157 +
   1.158 +    @Override
   1.159 +    public boolean equals(Object o) {
   1.160 +        if (this == o) return true;
   1.161 +        if (o instanceof NTCredentials) {
   1.162 +            NTCredentials that = (NTCredentials) o;
   1.163 +            if (LangUtils.equals(this.principal, that.principal)
   1.164 +                    && LangUtils.equals(this.workstation, that.workstation)) {
   1.165 +                return true;
   1.166 +            }
   1.167 +        }
   1.168 +        return false;
   1.169 +    }
   1.170 +
   1.171 +    @Override
   1.172 +    public String toString() {
   1.173 +        StringBuilder buffer = new StringBuilder();
   1.174 +        buffer.append("[principal: ");
   1.175 +        buffer.append(this.principal);
   1.176 +        buffer.append("][workstation: ");
   1.177 +        buffer.append(this.workstation);
   1.178 +        buffer.append("]");
   1.179 +        return buffer.toString();
   1.180 +    }
   1.181 +
   1.182 +}

mercurial