📄 passwdmembershipservice.java
字号:
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
* $Id: PasswdMembershipService.java,v 1.2 2002/03/04 21:42:58 echtcherbina Exp $
*/
package net.jxta.impl.membership;
import java.net.URL;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;
import java.net.MalformedURLException;
import java.net.UnknownServiceException;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import net.jxta.credential.AuthenticationCredential;
import net.jxta.credential.Credential;
import net.jxta.document.Advertisement;
import net.jxta.document.Element;
import net.jxta.document.TextElement;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.membership.Authenticator;
import net.jxta.membership.MembershipService;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.platform.ModuleSpecID;
import net.jxta.service.Service;
import net.jxta.exception.JxtaError;
import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.exception.PeerGroupException;
/**
* The passwd membership service provides a MembershipService implementation which is
* based on a password scheme similar to the unix /etc/passwd system. It is
* intended mostly as an example of a simple MembershipService service and not as
* a practical MembershipService service.
*
* @version $Revision: 1.2 $
* @since JXTA 1.0
*/
public class PasswdMembershipService extends MembershipService {
private static final Category LOG = Category.getInstance(PasswdMembershipService.class.getName());
/** This class provides the sub-class of Credential which is associated
* with the password membership service.
*/
private class PasswdCredential implements Credential {
/** The MembershipService service which generated this credential.
*/
PasswdMembershipService source;
/** The identity associated with this credential
*/
String whoami;
/** The peerid associated with this credential.
*/
ID peerid;
/** The peerid which has been "signed" so that the identity may be verified.
*/
String signedPeerID;
protected PasswdCredential( PasswdMembershipService source, String whoami, String signedPeerID ) {
this.source = (PasswdMembershipService) source;
this.whoami = whoami;
this.peerid = source.getPeerGroup().getPeerID();
this.signedPeerID = signedPeerID;
}
protected PasswdCredential( PasswdMembershipService source, PeerGroupID peergroup, PeerID peer, String whoami, String signedPeerID ) throws PeerGroupException {
this.source = (PasswdMembershipService) source;
if( !source.getPeerGroup().getPeerGroupID().equals( peergroup ) )
throw new PeerGroupException( "Cannot credential for a different peer group." );
this.whoami = whoami;
this.peerid = peer;
this.signedPeerID = signedPeerID;
}
/**
* Returns the service which generated this credential.
* @return the service which generated this credential.
*/
public MembershipService getSourceService() {
return source;
}
/** Returns the PeerGroupID associated with this Credential
* @return the PeerGroupID associated with this Credential
*/
public ID getPeerGroupID() {
return source.getPeerGroup().getPeerGroupID();
}
/** Returns the PeerID assocaited with this credential
* @return the PeerID assocaited with this credential
*/
public ID getPeerID() {
return peerid;
}
/** Write credential into a document. as is a mime media-type specification and
* provides the form of the document which is being requested. Two standard
* document forms are defined. "text/text" encodes the document in a form nice
* for printing out and "text/xml" which provides an XML format.
* @param as The mime media type of the encoding format being requested.
* @throws Exception in the event of errors. this exception spec needs to be narrowed.
* @return the StructuredDocument which represents this credential.
*/
public StructuredDocument getDocument(MimeMediaType as) throws Exception {
StructuredDocument doc =
StructuredDocumentFactory.newStructuredDocument( as,
"PasswdCredential" );
Element e = doc.createElement( "PeerGroupID", peergroup.getPeerGroupID().toString() );
doc.appendChild( e );
e = doc.createElement( "PeerID", peergroup.getPeerID().toString() );
doc.appendChild( e );
e = doc.createElement( "Identity", whoami );
doc.appendChild( e );
// FIXME 20010327 Do some kind of signing here based on password.
e = doc.createElement( "ReallyInsecureSignature", signedPeerID );
doc.appendChild( e );
return doc;
}
/* From here on are the methods specific to this credential type */
/** Returns the identity associated with this credential.
* @return the identity associated with this credential.
*/
public String getIdentity() {
return whoami;
}
}
/** The authenticator associated with the password membership service.
*/
public class PasswdAuthenticator implements Authenticator {
/** The MembershipService service which generated this authenticator.
*/
MembershipService source;
/** The Authentication which was provided to the Apply operation of the membership
* service.
*/
AuthenticationCredential application;
/** the identity which is being claimed
*/
String whoami = null;
/** the password for that identity.
*/
String password = null;
/** Creates an authenticator for the password MembershipService service. The only method
* supported is "PasswdAuthentication". Anything entered into the identity info
* section of the Authentication credential is ignored.
* @param source The instance of the password membership service which created this
* authenticator.
* @param application The Anything entered into the identity info section of the Authentication
* credential is ignored.
*/
PasswdAuthenticator( MembershipService source, AuthenticationCredential application ) {
this.source = source;
this.application = application;
// XXX 20010328 bondolo@jxta.org Could do something with the authentication credential here.
}
/**
* Returns the policy which generated this authenticator.
* @return the service which generated this authenticator.
*/
public MembershipService getSourceService() {
return source;
}
/**
* Returns true if this Authenticator has been satisfied and is ready
* for the join operation. Some authenticators may behave in an
* asynchronously and this method can be used to determine if the
* authentication process has completed. This method makes no
* distinction between incomplete authentication and failed authentication.
* @return true if this Authenticator has been satisfied and is ready
* for the join operation.
*/
synchronized public boolean isReadyForJoin() {
return ( (null != password) && (null != whoami) );
}
/**
* Returns the name of this authentication method. This should be the same
* name which was used in the Authentication credential.
* @return the name of this authentication method.
*/
public String getMethodName() {
return "PasswdAuthentication";
}
public void setAuth1Identity( String who ) {
whoami = who;
}
public String getAuth1Identity() {
return whoami;
}
public void setAuth2_Password( String secret ) {
password = secret;
}
private String getAuth2_Password() {
return password;
}
/** Returns the AuthenticationCredential which is associated with this authenticator
* @return the AuthenticationCredential which is associated with this authenticator
*/
public AuthenticationCredential getAuthenticationCredential() {
return application;
}
}
/**
* An inner class used to create static well-known identifiers.
*/
static class IdMaker {
// This method serves to catch the MalformedURLException.
// Don't know how to catch it from the static init context.
static ID mkID( String s ) {
try {
return IDFactory.fromURL(new URL( "urn", "", "jxta:uuid-" + s));
} catch (MalformedURLException absurd) {
// Fall through.
} catch (UnknownServiceException absurd2) {
// Fall through.
}
throw new JxtaError("Hardcoded Spec and Class IDs are malformed.");
}
}
/**
* Well known service specification identifier: the standard membership
*/
public static final ModuleSpecID passwordMembershipSpecID = (ModuleSpecID)
IdMaker.mkID( "DeadBeefDeafBabaFeedBabe00000005"
+ "02"
+ "06" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -