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; 031 032import org.apache.http.annotation.Contract; 033import org.apache.http.annotation.ThreadingBehavior; 034import org.apache.http.util.Args; 035import org.apache.http.util.LangUtils; 036 037/** 038 * Simple {@link Credentials} implementation based on a user name / password 039 * pair. 040 * 041 * @since 4.0 042 */ 043@Contract(threading = ThreadingBehavior.IMMUTABLE) 044public class UsernamePasswordCredentials implements Credentials, Serializable { 045 046 private static final long serialVersionUID = 243343858802739403L; 047 048 private final BasicUserPrincipal principal; 049 private final String password; 050 051 /** 052 * The constructor with the username and password combined string argument. 053 * 054 * @param usernamePassword the username:password formed string 055 * @see #toString 056 * @deprecated (4.5) will be replaced with {@code String}, {@code char[]} in 5.0 057 */ 058 @Deprecated 059 public UsernamePasswordCredentials(final String usernamePassword) { 060 super(); 061 Args.notNull(usernamePassword, "Username:password string"); 062 final int atColon = usernamePassword.indexOf(':'); 063 if (atColon >= 0) { 064 this.principal = new BasicUserPrincipal(usernamePassword.substring(0, atColon)); 065 this.password = usernamePassword.substring(atColon + 1); 066 } else { 067 this.principal = new BasicUserPrincipal(usernamePassword); 068 this.password = null; 069 } 070 } 071 072 073 /** 074 * The constructor with the username and password arguments. 075 * 076 * @param userName the user name 077 * @param password the password 078 */ 079 public UsernamePasswordCredentials(final String userName, final String password) { 080 super(); 081 Args.notNull(userName, "Username"); 082 this.principal = new BasicUserPrincipal(userName); 083 this.password = password; 084 } 085 086 @Override 087 public Principal getUserPrincipal() { 088 return this.principal; 089 } 090 091 public String getUserName() { 092 return this.principal.getName(); 093 } 094 095 @Override 096 public String getPassword() { 097 return password; 098 } 099 100 @Override 101 public int hashCode() { 102 return this.principal.hashCode(); 103 } 104 105 @Override 106 public boolean equals(final Object o) { 107 if (this == o) { 108 return true; 109 } 110 if (o instanceof UsernamePasswordCredentials) { 111 final UsernamePasswordCredentials that = (UsernamePasswordCredentials) o; 112 if (LangUtils.equals(this.principal, that.principal)) { 113 return true; 114 } 115 } 116 return false; 117 } 118 119 @Override 120 public String toString() { 121 return this.principal.toString(); 122 } 123 124} 125