strolch/src/ch/eitchnet/privilege/model/Certificate.java

190 lines
5.2 KiB
Java
Raw Normal View History

2010-05-20 21:36:16 +02:00
/*
* Copyright (c) 2010
*
* Robert von Burg
* eitch@eitchnet.ch
*
* All rights reserved.
*
*/
package ch.eitchnet.privilege.model;
import java.io.Serializable;
import java.util.Locale;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
2010-05-24 21:21:46 +02:00
import ch.eitchnet.privilege.i18n.PrivilegeException;
import ch.eitchnet.privilege.model.internal.Session;
2010-05-20 21:36:16 +02:00
/**
* The {@link Certificate} is the object a client keeps when accessing a Privilege enabled system. This object is the
* instance which is always used when performing an access and is returned when a user performs a login through
* {@link PrivilegeHandler#authenticate(String, String)}
2010-05-20 21:36:16 +02:00
*
* @author rvonburg
2010-05-20 21:36:16 +02:00
*/
2010-05-31 21:34:26 +02:00
public final class Certificate implements Serializable {
2010-05-20 21:36:16 +02:00
private static final long serialVersionUID = 1L;
private final String sessionId;
private final String username;
private final String authToken;
private final String authPassword;
private Locale locale;
/**
* Default constructor initializing with all information needed for this certificate
*
* <p>Note, both the authentication token and password are private fields which are generated on login and only known by the {@link PrivilegeHandler}</p>
*
2010-05-20 21:36:16 +02:00
* @param sessionId
* the users session id
2010-05-20 21:36:16 +02:00
* @param username
* the users login name
2010-05-20 21:36:16 +02:00
* @param authToken
* the authentication token defining the users unique session and is a private field of this certificate.
* It corresponds with the authentication token on the {@link Session}
2010-05-20 21:36:16 +02:00
* @param authPassword
* the password to access the authentication token, this is not known to the client but set by the
* {@link PrivilegeHandler} on authentication. It corresponds with the authentication password on the {@link Session}
2010-05-20 21:36:16 +02:00
* @param locale
* the users {@link Locale}
2010-05-20 21:36:16 +02:00
*/
public Certificate(String sessionId, String username, String authToken, String authPassword, Locale locale) {
// validate arguments are not null
if (sessionId == null || username == null || authToken == null || authPassword == null) {
throw new PrivilegeException("One of the arguments is null!");
}
this.sessionId = sessionId;
this.username = username;
this.authToken = authToken;
this.authPassword = authPassword;
// if no locale is given, set default
if (locale == null)
this.locale = Locale.getDefault();
else
this.locale = locale;
}
/**
* @return the locale
*/
public Locale getLocale() {
2010-09-19 20:57:23 +02:00
return this.locale;
2010-05-20 21:36:16 +02:00
}
/**
* @param locale
* the locale to set
*/
public void setLocale(Locale locale) {
this.locale = locale;
}
/**
* @return the sessionId
*/
public String getSessionId() {
2010-09-19 20:57:23 +02:00
return this.sessionId;
2010-05-20 21:36:16 +02:00
}
/**
* @return the username
*/
public String getUsername() {
2010-09-19 20:57:23 +02:00
return this.username;
2010-05-20 21:36:16 +02:00
}
/**
* Returns the authToken if the given authPassword is correct, null otherwise
2010-05-20 21:36:16 +02:00
*
* @param authPassword
* the authentication password with which this certificate was created
2010-05-20 21:36:16 +02:00
*
* @return the authToken if the given authPassword is correct, null otherwise
2010-05-20 21:36:16 +02:00
*/
public String getAuthToken(String authPassword) {
2010-09-19 20:57:23 +02:00
if (this.authPassword.equals(authPassword))
return this.authToken;
return null;
2010-05-20 21:36:16 +02:00
}
2010-05-24 21:21:46 +02:00
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
2010-09-19 20:57:23 +02:00
result = prime * result + ((this.authPassword == null) ? 0 : this.authPassword.hashCode());
result = prime * result + ((this.authToken == null) ? 0 : this.authToken.hashCode());
result = prime * result + ((this.locale == null) ? 0 : this.locale.hashCode());
result = prime * result + ((this.sessionId == null) ? 0 : this.sessionId.hashCode());
result = prime * result + ((this.username == null) ? 0 : this.username.hashCode());
2010-05-24 21:21:46 +02:00
return result;
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Certificate))
return false;
Certificate other = (Certificate) obj;
2010-09-19 20:57:23 +02:00
if (this.authPassword == null) {
2010-05-24 21:21:46 +02:00
if (other.authPassword != null)
return false;
2010-09-19 20:57:23 +02:00
} else if (!this.authPassword.equals(other.authPassword))
2010-05-24 21:21:46 +02:00
return false;
2010-09-19 20:57:23 +02:00
if (this.authToken == null) {
2010-05-24 21:21:46 +02:00
if (other.authToken != null)
return false;
2010-09-19 20:57:23 +02:00
} else if (!this.authToken.equals(other.authToken))
2010-05-24 21:21:46 +02:00
return false;
2010-09-19 20:57:23 +02:00
if (this.locale == null) {
2010-05-24 21:21:46 +02:00
if (other.locale != null)
return false;
2010-09-19 20:57:23 +02:00
} else if (!this.locale.equals(other.locale))
2010-05-24 21:21:46 +02:00
return false;
2010-09-19 20:57:23 +02:00
if (this.sessionId == null) {
2010-05-24 21:21:46 +02:00
if (other.sessionId != null)
return false;
2010-09-19 20:57:23 +02:00
} else if (!this.sessionId.equals(other.sessionId))
2010-05-24 21:21:46 +02:00
return false;
2010-09-19 20:57:23 +02:00
if (this.username == null) {
2010-05-24 21:21:46 +02:00
if (other.username != null)
return false;
2010-09-19 20:57:23 +02:00
} else if (!this.username.equals(other.username))
2010-05-24 21:21:46 +02:00
return false;
return true;
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
2010-06-05 23:33:30 +02:00
StringBuilder builder = new StringBuilder();
builder.append("Certificate [sessionId=");
2010-09-19 20:57:23 +02:00
builder.append(this.sessionId);
2010-06-05 23:33:30 +02:00
builder.append(", username=");
2010-09-19 20:57:23 +02:00
builder.append(this.username);
2010-06-05 23:33:30 +02:00
builder.append(", locale=");
2010-09-19 20:57:23 +02:00
builder.append(this.locale);
2010-06-05 23:33:30 +02:00
builder.append("]");
return builder.toString();
2010-05-24 21:21:46 +02:00
}
2010-05-20 21:36:16 +02:00
}