View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.jsch;
17  
18  import java.awt.Container;
19  import java.awt.GridLayout;
20  import java.awt.Panel;
21  import java.awt.event.ActionEvent;
22  import java.awt.event.ActionListener;
23  import java.util.Arrays;
24  
25  import javax.swing.JButton;
26  import javax.swing.JDialog;
27  import javax.swing.JFrame;
28  import javax.swing.JLabel;
29  import javax.swing.JPanel;
30  import javax.swing.JPasswordField;
31  
32  /** Retrieves a password or a passphrase using standard Swing */
33  public class SwingUserInfo extends SimpleUserInfo {
34  
35  	private Boolean alwaysPrompt = false;
36  
37  	public boolean promptPassphrase(String message) {
38  		if (passphrase != null)
39  			return true;
40  
41  		if (!alwaysPrompt && passphraseSafe != null)
42  			return true;
43  
44  		PasswordDialog dialog = new PasswordDialog(message) {
45  			private static final long serialVersionUID = 3266299327166418364L;
46  
47  			@Override
48  			protected void useCredentials(char[] password) {
49  				passphraseSafe = new char[password.length];
50  				System.arraycopy(password, 0, passphraseSafe, 0,
51  						password.length);
52  				// passphraseSafe = Arrays.copyOf(password, password.length);
53  			}
54  		};
55  		dialog.setVisible(true);
56  		return dialog.getWasProvided();
57  	}
58  
59  	public boolean promptPassword(String message) {
60  		if (password != null)
61  			return true;
62  
63  		if (!alwaysPrompt && passwordSafe != null)
64  			return true;
65  
66  		PasswordDialog dialog = new PasswordDialog(message) {
67  			private static final long serialVersionUID = 3266299327166418364L;
68  
69  			@Override
70  			protected void useCredentials(char[] password) {
71  				// passwordSafe = Arrays.copyOf(password, password.length);
72  				passwordSafe = new char[password.length];
73  				System.arraycopy(password, 0, passwordSafe, 0, password.length);
74  			}
75  		};
76  		dialog.setVisible(true);
77  		return dialog.getWasProvided();
78  	}
79  
80  	public void setAlwaysPrompt(Boolean alwaysPrompt) {
81  		this.alwaysPrompt = alwaysPrompt;
82  	}
83  
84  	protected static class PasswordDialog extends JDialog implements
85  			ActionListener {
86  		private static final long serialVersionUID = 3399155607980846207L;
87  
88  		private static final String OK = "ok";
89  
90  		private JPasswordField password = new JPasswordField("", 10);
91  
92  		private JButton okButton;
93  		private JButton cancelButton;
94  
95  		private Boolean wasProvided = false;
96  
97  		public PasswordDialog(String title) {
98  			setTitle(title);
99  			setModal(true);
100 			setLocationRelativeTo(null);
101 			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
102 
103 			JPanel p1 = new JPanel(new GridLayout(1, 2, 3, 3));
104 			p1.add(new JLabel("Password"));
105 			password.setActionCommand(OK);
106 			password.addActionListener(this);
107 			p1.add(password);
108 			add("Center", p1);
109 
110 			Panel p2 = new Panel();
111 			okButton = addButton(p2, "OK");
112 			okButton.setActionCommand(OK);
113 			cancelButton = addButton(p2, "Cancel");
114 			add("South", p2);
115 			setSize(240, 120);
116 
117 			pack();
118 		}
119 
120 		/** To be overridden */
121 		protected void useCredentials(char[] password) {
122 			// does nothing
123 		}
124 
125 		private JButton addButton(Container c, String name) {
126 			JButton button = new JButton(name);
127 			button.addActionListener(this);
128 			c.add(button);
129 			return button;
130 		}
131 
132 		public final void actionPerformed(ActionEvent evt) {
133 			Object source = evt.getSource();
134 			if (source == okButton || evt.getActionCommand().equals(OK)) {
135 				char[] p = password.getPassword();
136 				useCredentials(p);
137 				wasProvided = true;
138 				Arrays.fill(p, '0');
139 				cleanUp();
140 			} else if (source == cancelButton)
141 				cleanUp();
142 		}
143 
144 		private void cleanUp() {
145 			password.setText("");
146 			dispose();
147 		}
148 
149 		public Boolean getWasProvided() {
150 			return wasProvided;
151 		}
152 
153 	}
154 
155 }