001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security.spi.impl;
018:
019: import java.io.Serializable;
020: import java.sql.Date;
021: import java.sql.Timestamp;
022: import java.util.Arrays;
023:
024: import org.apache.jetspeed.security.PasswordCredential;
025: import org.apache.jetspeed.security.om.InternalCredential;
026:
027: /**
028: * <p>
029: * Default Password credential implementation. Provides the same mechanism as J2EE
030: * <code>javax.resource.spi.security.PasswordCredential</code>.
031: * </p>
032: *
033: * <p>
034: * Code borrowed from the Geronimo project.
035: * </p>
036: *
037: * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
038: */
039: public class DefaultPasswordCredentialImpl implements
040: PasswordCredential, Serializable {
041:
042: /** The default uid. */
043: private static final long serialVersionUID = -4975305752376365096L;
044:
045: /** The user name. */
046: private String userName;
047:
048: /** The password. */
049: private char[] password;
050:
051: /** The update required state */
052: private boolean updateRequired;
053:
054: /** The enabled state. */
055: private boolean enabled = true;
056:
057: /** The expired state. */
058: private boolean expired;
059:
060: /** The expiration date. */
061: private Date expirationDate;
062:
063: /** The previous authentication in date */
064: private Timestamp previousAuthenticationDate;
065:
066: /** The last authentication in date */
067: private Timestamp lastAuthenticationDate;
068:
069: /** The number of authentication failures */
070: private int authenticationFailures;
071:
072: /**
073: * @param userName
074: * @param password
075: */
076: public DefaultPasswordCredentialImpl(String userName,
077: char[] password) {
078: this .userName = userName;
079: this .password = (char[]) password.clone();
080: }
081:
082: public DefaultPasswordCredentialImpl(String userName,
083: InternalCredential credential) {
084: this (userName, credential.getValue().toCharArray());
085: this .updateRequired = credential.isUpdateRequired();
086: this .enabled = credential.isEnabled();
087: this .expired = credential.isExpired();
088: this .expirationDate = credential.getExpirationDate();
089: this .previousAuthenticationDate = credential
090: .getPreviousAuthenticationDate();
091: this .lastAuthenticationDate = credential
092: .getLastAuthenticationDate();
093: this .authenticationFailures = credential
094: .getAuthenticationFailures();
095: }
096:
097: /**
098: * @return The username.
099: */
100: public String getUserName() {
101: return userName;
102: }
103:
104: /**
105: * @return The password.
106: */
107: public char[] getPassword() {
108: return (char[]) password.clone();
109: }
110:
111: /**
112: * @see org.apache.jetspeed.security.PasswordCredential#isUpdateRequired()
113: */
114: public boolean isUpdateRequired() {
115: return updateRequired;
116: }
117:
118: /**
119: * @see org.apache.jetspeed.security.PasswordCredential#isEnabled()
120: */
121: public boolean isEnabled() {
122: return enabled;
123: }
124:
125: /**
126: * @see org.apache.jetspeed.security.PasswordCredential#isExpired()
127: */
128: public boolean isExpired() {
129: return expired;
130: }
131:
132: /**
133: * @see org.apache.jetspeed.security.PasswordCredential#getExpirationDate()
134: */
135: public Date getExpirationDate() {
136: return expirationDate;
137: }
138:
139: /**
140: * @see org.apache.jetspeed.security.PasswordCredential#getPreviousAuthenticationDate()
141: */
142: public Timestamp getPreviousAuthenticationDate() {
143: return previousAuthenticationDate;
144: }
145:
146: /**
147: * @see org.apache.jetspeed.security.PasswordCredential#getLastAuthenticationDate()
148: */
149: public Timestamp getLastAuthenticationDate() {
150: return lastAuthenticationDate;
151: }
152:
153: /**
154: * @see org.apache.jetspeed.security.PasswordCredential#getAuthenticationFailures()
155: */
156: public int getAuthenticationFailures() {
157: return authenticationFailures;
158: }
159:
160: /**
161: * @see java.lang.Object#equals(java.lang.Object)
162: */
163: public boolean equals(Object o) {
164: if (this == o)
165: return true;
166: if (!(o instanceof DefaultPasswordCredentialImpl))
167: return false;
168:
169: final DefaultPasswordCredentialImpl credential = (DefaultPasswordCredentialImpl) o;
170:
171: if (!Arrays.equals(password, credential.password))
172: return false;
173: if (!userName.equals(credential.userName))
174: return false;
175:
176: return true;
177: }
178:
179: /**
180: * @see java.lang.Object#hashCode()
181: */
182: public int hashCode() {
183: int result = userName.hashCode();
184: for (int i = 0; i < password.length; i++) {
185: result *= password[i];
186: }
187: return result;
188: }
189: }
|