CredentialedUserTokenImpl.java
/***************************************************************************
Copyright 2012 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.authident;
/** This is a utility class provided for credential provider's to use when creating an
instance of CredentialedUserToken's. It assumes the Credential is a String(like a username)
and is an immutable object.
@param <P> This default implementation of a credentialed user token assumes the credential is
a string, however, we have left the credential provider unspecified, this parameter represents
the type of the provider associated with this token instance.
*/
public final class CredentialedUserTokenImpl<P> implements CredentialedUserToken<P,String> {
/** The plain UserToken instance backing this credentialed token. */
private final UserToken ut;
/** The credential associated with this token is a string we'll refer to as a user name. */
private final String username;
/** The authentication provider instance that authenticated this token using the credential. */
private final P credentialProvider;
/** Create an instance with constant values.
@param ut The UserToken provided by an instance of Realm.
@param username The credential for this token.
@param credentialProvider The credential provider which authenticated this token.
*/
public CredentialedUserTokenImpl(final UserToken ut, final String username, final P credentialProvider) {
this.ut = ut;
this.username = username;
this.credentialProvider = credentialProvider;
}
// UserToken methods
@Override public long getID() { return ut.getID(); }
@Override public void delete() { ut.delete(); }
@Override public Realm realm() { return ut.realm(); }
// CredentialedUserTokenMethods
@Override public P credentialProvider() { return credentialProvider; }
@Override public String getCredential() { return username; }
// Object methods.
@Override public boolean equals(final Object obj) {
if(obj == this) { return true; }
if(!(obj instanceof UserToken)) { return false; }
final UserToken objUT = (UserToken) obj;
if(!(ut.realm().equals(objUT.realm()))) { return false; }
return ut.getID() == objUT.getID();
}
@Override public int hashCode() {
return ut.realm().hashCode() + ((int) (ut.getID() % ((long) Integer.MAX_VALUE)));
}
}