001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.auth.user; 020 021import org.apache.commons.lang3.StringUtils; 022 023import jakarta.servlet.http.HttpServletRequest; 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.Date; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030 031 032/** 033 * Default implementation for representing wiki user information, such as the login name, full name, wiki name, and e-mail address. 034 * 035 * @since 2.3 036 */ 037public final class DefaultUserProfile implements UserProfile { 038 039 private static final long serialVersionUID = -5600466893735300647L; 040 private static final String EMPTY_STRING = ""; 041 private static final String WHITESPACE = "\\s"; 042 043 private final Map< String, Serializable > attributes = new HashMap<>(); 044 private Date created; 045 private String email; 046 private String fullname; 047 private Date lockExpiry; 048 private Date passwordChanged; 049 private String loginName; 050 private Date modified; 051 private String password; 052 private String uid; 053 private String wikiname; 054 //oldest should be in position zero, size bounded via configuration 055 private List<String> previousHashedCredentials = new ArrayList<>(); 056 057 public List<String> getPreviousHashedCredentials() { 058 return previousHashedCredentials; 059 } 060 061 /** 062 * Package constructor to allow direct instantiation only from package related classes (i.e., AbstractUserDatabase). 063 */ 064 DefaultUserProfile() {} 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public boolean equals( final Object o ) { 071 if ( o instanceof UserProfile ) { 072 final DefaultUserProfile u = ( DefaultUserProfile )o; 073 return same( fullname, u.fullname ) && 074 //same( password, u.password ) && 075 //Note: this used to compare the password for some reason, but that was causing 076 //issues when the user wanted to change their password. since this is called 077 //from DefaultUserManager#setProfile 078 same( loginName, u.loginName ) && 079 same( StringUtils.lowerCase( email ), StringUtils.lowerCase( u.email ) ) && 080 same( wikiname, u.wikiname ); 081 } 082 083 return false; 084 } 085 086 @Override 087 public int hashCode() { 088 return ( fullname != null ? fullname.hashCode() : 0 ) ^ 089 ( password != null ? password.hashCode() : 0 ) ^ 090 ( loginName != null ? loginName.hashCode() : 0 ) ^ 091 ( wikiname != null ? wikiname.hashCode() : 0 ) ^ 092 ( email != null ? StringUtils.lowerCase( email ).hashCode() : 0 ); 093 } 094 095 /** 096 * Returns the creation date 097 * 098 * @return the creation date 099 * @see org.apache.wiki.auth.user.UserProfile#getCreated() 100 */ 101 @Override 102 public Date getCreated() 103 { 104 return created; 105 } 106 107 /** 108 * Returns the user's e-mail address. 109 * 110 * @return the e-mail address 111 */ 112 @Override 113 public String getEmail() 114 { 115 return email; 116 } 117 118 /** 119 * Returns the user's full name. 120 * 121 * @return the full name 122 */ 123 @Override 124 public String getFullname() 125 { 126 return fullname; 127 } 128 129 /** 130 * Returns the last-modified date. 131 * 132 * @return the last-modified date 133 * @see org.apache.wiki.auth.user.UserProfile#getLastModified() 134 */ 135 @Override 136 public Date getLastModified() 137 { 138 return modified; 139 } 140 141 /** 142 * Returns the user's login name. 143 * @return the login name 144 */ 145 @Override 146 public String getLoginName() 147 { 148 return loginName; 149 } 150 151 /** 152 * Returns the user password for use with custom authentication. Note that the password field is not meaningful for container 153 * authentication; the user's private credentials are generally stored elsewhere. While it depends on the {@link UserDatabase} 154 * implementation, in most cases the value returned by this method will be a password hash, not the password itself. 155 * 156 * @return the password 157 */ 158 @Override 159 public String getPassword() 160 { 161 return password; 162 } 163 164 /** 165 * Returns the user's wiki name. 166 * 167 * @return the wiki name. 168 */ 169 @Override 170 public String getWikiName() 171 { 172 return wikiname; 173 } 174 175 /** 176 * Returns <code>true</code> if the user profile is new. This implementation checks whether {@link #getLastModified()} returns 177 * <code>null</code> to determine the status. 178 * 179 * @see org.apache.wiki.auth.user.UserProfile#isNew() 180 */ 181 @Override 182 public boolean isNew() 183 { 184 return modified == null; 185 } 186 187 /** 188 * @param date the creation date 189 * @see org.apache.wiki.auth.user.UserProfile#setCreated(java.util.Date) 190 */ 191 @Override 192 public void setCreated( final Date date ) 193 { 194 created = date; 195 } 196 197 /** 198 * Sets the user's e-mail address. 199 * 200 * @param email the e-mail address 201 */ 202 @Override 203 public void setEmail( final String email ) 204 { 205 this.email = email; 206 } 207 208 /** 209 * Sets the user's full name. For example, "Janne Jalkanen." 210 * 211 * @param arg the full name 212 */ 213 @Override 214 public void setFullname( final String arg ) { 215 fullname = arg; 216 217 // Compute wiki name 218 if ( fullname != null ) { 219 wikiname = fullname.replaceAll( WHITESPACE, EMPTY_STRING ); 220 } 221 } 222 223 /** 224 * Sets the last-modified date. 225 * 226 * @param date the last-modified date 227 * @see org.apache.wiki.auth.user.UserProfile#setLastModified(java.util.Date) 228 */ 229 @Override 230 public void setLastModified( final Date date ) 231 { 232 modified = date; 233 } 234 235 /** 236 * Sets the name by which the user logs in. The login name is used as the username for custom authentication (see 237 * {@link org.apache.wiki.auth.AuthenticationManager#login(org.apache.wiki.api.core.Session,HttpServletRequest, String, String)}). 238 * The login name is typically a short name ("jannej"). In contrast, the wiki name is typically of type 239 * FirstnameLastName ("JanneJalkanen"). 240 * 241 * @param name the login name 242 */ 243 @Override 244 public void setLoginName( final String name ) 245 { 246 loginName = name; 247 } 248 249 /** 250 * Sets the user's password for use with custom authentication. It is <em>not</em> the responsibility of implementing classes to hash 251 * the password; that responsibility is borne by the UserDatabase implementation during save operations (see 252 * {@link UserDatabase#save(UserProfile)}). Note that the password field is not meaningful for container authentication; the user's 253 * private credentials are generally stored elsewhere. 254 * 255 * @param arg the password 256 */ 257 @Override 258 public void setPassword( final String arg ) 259 { 260 password = arg; 261 } 262 263 /** 264 * Returns a string representation of this user profile. 265 * 266 * @return the string 267 */ 268 @Override 269 public String toString() 270 { 271 return "[DefaultUserProfile: '" + getFullname() + "']"; 272 } 273 274 /** 275 * Private method that compares two objects and determines whether they are equal. Two nulls are considered equal. 276 * 277 * @param arg1 the first object 278 * @param arg2 the second object 279 * @return the result of the comparison 280 */ 281 private boolean same( final Object arg1, final Object arg2 ) { 282 if( arg1 == null && arg2 == null ) { 283 return true; 284 } 285 if( arg1 == null || arg2 == null ) { 286 return false; 287 } 288 return arg1.equals( arg2 ); 289 } 290 291 //--------------------------- Attribute and lock interface implementations --------------------------- 292 293 /** 294 * {@inheritDoc} 295 */ 296 @Override 297 public Map< String, Serializable > getAttributes() 298 { 299 return attributes; 300 } 301 302 /** 303 * {@inheritDoc} 304 */ 305 @Override 306 public Date getLockExpiry() 307 { 308 return isLocked() ? lockExpiry : null; 309 } 310 311 /** 312 * {@inheritDoc} 313 */ 314 @Override 315 public Date getPasswordChanged() { 316 return passwordChanged; 317 } 318 319 /** 320 * {@inheritDoc} 321 */ 322 @Override 323 public void setPasswordChanged( final Date date ) { 324 this.passwordChanged = date; 325 } 326 327 /** 328 * {@inheritDoc} 329 */ 330 @Override 331 public String getUid() 332 { 333 return uid; 334 } 335 336 /** 337 * {@inheritDoc} 338 */ 339 @Override 340 public boolean isLocked() { 341 final boolean locked = lockExpiry != null && System.currentTimeMillis() < lockExpiry.getTime(); 342 // Clear the lock if it's expired already 343 if( !locked && lockExpiry != null ) { 344 lockExpiry = null; 345 } 346 return locked; 347 348 } 349 350 /** 351 * {@inheritDoc} 352 */ 353 @Override 354 public void setLockExpiry( final Date expiry ) 355 { 356 this.lockExpiry = expiry; 357 } 358 359 /** 360 * {@inheritDoc} 361 */ 362 @Override 363 public void setUid( final String uid ) 364 { 365 this.uid = uid; 366 } 367 368}