001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * docs/licenses/cddl.txt
011 * or http://www.opensource.org/licenses/cddl1.php.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * docs/licenses/cddl.txt. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2011-2014 UnboundID Corp.
026 */
027
028 package com.unboundid.directory.sdk.http.types;
029
030 import javax.servlet.http.HttpServletRequest;
031
032 /**
033 * A {@link com.unboundid.directory.sdk.http.types.AuthenticationRequest}
034 * implementation where the user's credentials is a simple password.
035 */
036 public class UsernamePasswordAuthenticationRequest
037 implements AuthenticationRequest
038 {
039 private final String principal;
040 private final String credentials;
041 private final HttpServletRequest httpServletRequest;
042
043 /**
044 * Constructs a new UsernamePasswordAuthenticationRequest with the provided
045 * information.
046 *
047 * @param principal The user resource to authentication against.
048 * @param credentials The password credential provided by the user.
049 * @param httpServletRequest The httpServletRequest that initiated the
050 * authentication request.
051 */
052 public UsernamePasswordAuthenticationRequest(
053 final String principal, final String credentials,
054 final HttpServletRequest httpServletRequest)
055 {
056 this.credentials = credentials;
057 this.principal = principal;
058 this.httpServletRequest = httpServletRequest;
059 }
060
061 /**
062 * Retrieves the password credential provided by the user.
063 *
064 * @return The password credential provided by the user.
065 */
066 public String getCredentials()
067 {
068 return credentials;
069 }
070
071 /**
072 * Retrieves the username provided by the user.
073 *
074 * @return The username provided by the user.
075 */
076 public String getPrincipal()
077 {
078 return principal;
079 }
080
081 /**
082 * {@inheritDoc}
083 */
084 public HttpServletRequest getHttpServletRequest()
085 {
086 return httpServletRequest;
087 }
088
089 /**
090 * {@inheritDoc}
091 */
092 @Override
093 public boolean equals(final Object o)
094 {
095 if (this == o)
096 {
097 return true;
098 }
099 if (!(o instanceof UsernamePasswordAuthenticationRequest))
100 {
101 return false;
102 }
103
104 UsernamePasswordAuthenticationRequest that =
105 (UsernamePasswordAuthenticationRequest) o;
106
107 if (!credentials.equals(that.credentials))
108 {
109 return false;
110 }
111 if (httpServletRequest != null ?
112 !httpServletRequest.equals(that.httpServletRequest) :
113 that.httpServletRequest != null)
114 {
115 return false;
116 }
117 if (!principal.equals(that.principal))
118 {
119 return false;
120 }
121
122 return true;
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 @Override
129 public int hashCode()
130 {
131 int result = principal.hashCode();
132 result = 31 * result + credentials.hashCode();
133 result = 31 * result + (httpServletRequest != null ?
134 httpServletRequest.hashCode() : 0);
135 return result;
136 }
137
138 /**
139 * {@inheritDoc}
140 */
141 @Override
142 public String toString()
143 {
144 final StringBuilder sb =
145 new StringBuilder("UsernamePasswordAuthenticationRequest{");
146 sb.append("principal='").append(principal).append('\'');
147 sb.append(", credentials='****'");
148 sb.append(", httpServletRequest=").append(httpServletRequest);
149 sb.append('}');
150 return sb.toString();
151 }
152 }