?? authenticationpanel.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 THE APACHE SOFTWARE FOUNDATION 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: AuthenticationPanel.java,v 1.9 2006/06/09 20:30:38 nano Exp $ */package net.jxta.myjxta.ui;import net.jxta.credential.AuthenticationCredential;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Element;import net.jxta.document.XMLElement;import net.jxta.exception.PeerGroupException;import net.jxta.exception.ProtocolNotSupportedException;import net.jxta.id.ID;import net.jxta.impl.membership.pse.PSEMembershipService;import net.jxta.impl.membership.pse.PSEUtils;import net.jxta.impl.membership.pse.StringAuthenticator;import net.jxta.impl.protocol.PSEConfigAdv;import net.jxta.membership.Authenticator;import net.jxta.membership.MembershipService;import net.jxta.myjxta.View;import net.jxta.myjxta.util.AuthenticationUtil;import net.jxta.myjxta.util.Resources;import net.jxta.peergroup.PeerGroup;import net.jxta.protocol.ConfigParams;import javax.crypto.EncryptedPrivateKeyInfo;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.security.cert.X509Certificate;import java.util.Arrays;import java.util.Iterator;import java.util.NoSuchElementException;import java.util.ResourceBundle;/** * @author james todd [gonzo at jxta dot org] * @version $Id: AuthenticationPanel.java,v 1.9 2006/06/09 20:30:38 nano Exp $ * <p/> * borrowed from net.jxta.impl.membership.pse.DialogAuthenticator */public final class AuthenticationPanel extends JPanel { private static final String AUTHENTICATOR = "StringAuthentication"; private static final ResourceBundle STRINGS = Resources.getStrings(); private View view = null; private PeerGroup pg = null; private X509Certificate cert; private EncryptedPrivateKeyInfo key; private MembershipService membership = null; private Authenticator authenticator = null; private boolean initKeyStore; private JPasswordField storePassword; private JLabel storePasswordLabel; private JLabel identitiesLabel; private JComboBox identities; private JPasswordField identityPassword; private JLabel identityPasswordLabel; private JButton ok; private JButton cancel; private final boolean isCanceled = true; private final class Identity { final ID id; final X509Certificate cert; String name; Identity(ID id, X509Certificate cert) { this.id = id; this.cert = cert; this.name = PSEUtils.getCertSubjectCName(this.cert); if (this.name == null) { this.name = "< no common name >"; } if (this.name.endsWith("-CA")) { this.name = this.name.substring(0, this.name.length() - 3); } } public String toString() { return this.name; } } public AuthenticationPanel(View view, PeerGroup pg) { this.view = view; this.pg = pg; init(); ui(); } public void requestFocus() { super.requestFocus(); if (this.initKeyStore) { this.identityPassword.requestFocusInWindow(); } else { this.storePassword.requestFocusInWindow(); } } public ID getIdentity() { Identity id = null; if (this.identities.isEnabled()) { id = (Identity) this.identities.getSelectedItem(); } return id != null ? id.id : null; } public boolean isCanceled() { return this.isCanceled; } public boolean isReadyForJoin() { return this.authenticator.isReadyForJoin(); } public void join() throws PeerGroupException { this.membership.join(this.authenticator); } public void setVisible(boolean isVisible) { super.setVisible(isVisible); if (isVisible) { isValidInput(); } } private void init() { ConfigParams cp = this.pg.getConfigAdvertisement(); Element pi = cp.getServiceParam(PeerGroup.membershipClassID); if (pi != null) { Advertisement pa = null; try { pa = AdvertisementFactory.newAdvertisement((XMLElement) pi); } catch (NoSuchElementException nse) { } if (pa instanceof PSEConfigAdv) { PSEConfigAdv pse = (PSEConfigAdv) pa; PSEMembershipService membershipService = ((PSEMembershipService) this.pg.getMembershipService()); initKeyStore = !membershipService.getPSEConfig().isInitialized(); this.cert = pse.getCertificate(); this.key = pse.getEncryptedPrivateKey(); } } //this.initKeyStore = (this.cert != null); this.membership = pg.getMembershipService(); try { this.authenticator = this.membership.apply(new AuthenticationCredential(this.pg, AUTHENTICATOR, null)); } catch (ProtocolNotSupportedException pnse) { } catch (PeerGroupException pge) { } } private void ui() { GridBagLayout gb = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gb); setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); this.storePasswordLabel = new JLabel(STRINGS.getString("label.identity.keystore.password"));// this.storePasswordLabel.setLabelFor(this.storePassword); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.anchor = GridBagConstraints.FIRST_LINE_START; gbc.fill = GridBagConstraints.BOTH; gbc.insets = new Insets(3, 3, 3, 3); gbc.ipadx = 0; gbc.ipady = 0; gb.setConstraints(this.storePasswordLabel, gbc); add(this.storePasswordLabel); this.storePassword = new JPasswordField("", 10);// if (! this.initKeyStore) { this.storePassword.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent ke) { final boolean validInput = isValidInput(); ok.setEnabled(validInput); } }); if (! this.initKeyStore) { this.identities = new JComboBox(); } else { Object[] names = {new Identity(this.pg.getPeerID(), this.cert)}; this.identities = new JComboBox(names); this.identities.setMaximumRowCount(1); } gbc.gridx = 1; gb.setConstraints(this.storePassword, gbc); add(this.storePassword); gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.LINE_START; this.identitiesLabel = new JLabel(STRINGS.getString("label.identity"));// this.identitiesLabel.setLabelFor(this.identities); gb.setConstraints(this.identitiesLabel, gbc); add(this.identitiesLabel); gbc.gridx = 1; gbc.fill = GridBagConstraints.BOTH; gb.setConstraints(this.identities, gbc); add(this.identities); gbc.gridx = 0; gbc.gridy = 2; gbc.fill = GridBagConstraints.BOTH; this.identityPasswordLabel = new JLabel(STRINGS.getString("label.identity.password"));// this.identityPasswordLabel.setLabelFor(this.identityPassword); gb.setConstraints(this.identityPasswordLabel, gbc); add(this.identityPasswordLabel); this.identityPassword = new JPasswordField("", 10); this.identityPassword.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent ke) { boolean validInput = isValidInput(); ok.setEnabled(validInput); if (isValidInput()) ok.requestFocusInWindow(); } }); this.identityPassword.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ok.getAction().actionPerformed(null); } }); gbc.gridx = 1; gb.setConstraints(this.identityPassword, gbc); add(this.identityPassword); enableIdentities(initKeyStore ? true : false); JPanel bp = new JPanel(new GridLayout(1, 0)); this.ok = new JButton(new AbstractAction() { public void actionPerformed(ActionEvent ae) { char[] kp = storePassword.isEnabled() ? storePassword.getPassword() : null; char[] ip = identityPassword.isEnabled() ? identityPassword.getPassword() : null; if (AuthenticationUtil.authenticate(view, pg, String.valueOf(kp), String.valueOf(ip))) { exit(); } } }); this.ok.setText(STRINGS.getString("action.ok")); this.ok.setEnabled(isValidInput()); bp.add(this.ok); this.cancel = new JButton(new AbstractAction() { public void actionPerformed(ActionEvent ae) { exit(); } }); this.cancel.setText(STRINGS.getString("action.cancel")); this.cancel.addKeyListener(new AbstractButtonKeyListener(this.cancel) { public void keyReleased(KeyEvent ke) { getButton().getAction().actionPerformed(null); } }); bp.add(this.cancel); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.LAST_LINE_END; gbc.fill = GridBagConstraints.VERTICAL; gb.setConstraints(bp, gbc); add(bp);// getRootPane().setDefaultButton(this.ok); } private void exit() { getRootPane().getParent().setVisible(false); } private boolean isValidInput() { if (this.initKeyStore) { boolean b = PSEUtils.pkcs5_Decrypt_pbePrivateKey(this.identityPassword.getPassword(), this.cert.getPublicKey().getAlgorithm(), this.key) != null; } else { ID[] roots = ((StringAuthenticator) this.authenticator).getIdentities(this.storePassword.getPassword()); boolean enableIdentities = false; if (roots != null) { for (Iterator r = Arrays.asList(roots).iterator(); r.hasNext();) { ID p = (ID) r.next(); try { // xxx: assume pse hack X509Certificate c = ((PSEMembershipService) this.pg.getMembershipService()).getPSEConfig().getTrustedCertificate(p); if (! enableIdentities) { enableIdentities = true; this.identities.removeAllItems(); this.identities.setSelectedIndex(-1); } this.identities.addItem(new Identity(p, c)); this.identities.setSelectedIndex(0); } catch (Exception ignore) { continue; } } } if (enableIdentities) { this.identities.setMaximumRowCount(this.identities.getItemCount()); } else { this.identities.removeAllItems(); this.identities.setSelectedIndex(-1); this.identityPassword.setText(""); } enableIdentities(enableIdentities); if (enableIdentities) { identityPassword.setText(new String(storePassword.getPassword())); identityPassword.requestFocusInWindow(); } } char[] kp = this.storePassword.isEnabled() ? this.storePassword.getPassword() : null; char[] ip = this.identityPassword.isEnabled() ? this.identityPassword.getPassword() : null; return (kp != null && ip != null && AuthenticationUtil.authenticate(this.view, this.pg, String.valueOf(kp), String.valueOf(ip), false)); } private void enableIdentities(boolean p_enableIdentities) { this.identitiesLabel.setEnabled(p_enableIdentities); this.identities.setEnabled(p_enableIdentities); this.identityPasswordLabel.setEnabled(p_enableIdentities); this.identityPassword.setEnabled(p_enableIdentities); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -