KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > pgp > PGPPassChecker


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.pgp;
17
18 import java.util.Hashtable JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.columba.mail.gui.util.PGPPassphraseDialog;
23 import org.waffel.jscf.JSCFConnection;
24 import org.waffel.jscf.JSCFException;
25 import org.waffel.jscf.JSCFStatement;
26
27 /**
28  * Checks via a dialog if a passphrase (given from the user over the dialog) can
29  * be used to sign a testmessage. The dialog-test is a while loop, which breaks
30  * only if the user cancels the dialog or the passphrase is correct.
31  *
32  * @author waffel
33  *
34  */

35 public class PGPPassChecker {
36     private static PGPPassChecker myInstance = null;
37
38     private Map JavaDoc passwordMap = new Hashtable JavaDoc();
39
40     /**
41      * Returns the instance of the class. If no instance is created, a new
42      * instance are created.
43      *
44      * @return a instance of this class.
45      */

46     public static PGPPassChecker getInstance() {
47         if (myInstance == null) {
48             myInstance = new PGPPassChecker();
49         }
50
51         return myInstance;
52     }
53
54     /**
55      * Checks with a test string if the test String can be signed. The user is
56      * ask for his passphrase until the passphrase is ok or the user cancels the
57      * dialog. If the user cancels the dialog the method returns false. This
58      * method return normal only if the user give the right passphrase.
59      *
60      * @param con
61      * JSCFConnection used to check a passphrase given by a dialog
62      * @return Returns true if the given passphrase (via a dialog) is correct
63      * and the user can sign a teststring with the passphrase from the
64      * dialog. Returns false if the user cancels the dialog.
65      * @exception JSCFException
66      * if the concrete JSCF implementation has real probelms (for
67      * example a extern tool cannot be found)
68      */

69     public boolean checkPassphrase(JSCFConnection con) throws JSCFException {
70         boolean stmtCheck = false;
71         JSCFStatement stmt = con.createStatement();
72
73         // loop until signing was sucessful or the user cancels the passphrase
74
// dialog
75
Properties JavaDoc props = con.getProperties();
76
77         while (!stmtCheck && (this.checkPassphraseDialog(con) == true)) {
78             stmtCheck = stmt.checkPassphrase();
79
80             if (!stmtCheck) {
81                 this.passwordMap.remove(props.get("USERID"));
82             }
83         }
84
85         return stmtCheck;
86     }
87
88     private boolean checkPassphraseDialog(JSCFConnection con) {
89         String JavaDoc passphrase = "";
90         Properties JavaDoc props = con.getProperties();
91
92         if (this.passwordMap.containsKey(props.get("USERID"))) {
93             passphrase = (String JavaDoc) this.passwordMap.get(props.get("USERID"));
94         }
95
96         props.put("PASSWORD", passphrase);
97
98         boolean ret = true;
99
100         PGPPassphraseDialog dialog = new PGPPassphraseDialog();
101
102         if (passphrase.length() == 0) {
103             dialog.showDialog((String JavaDoc) props.get("USERID"), (String JavaDoc) props
104                     .get("PASSWORD"), false);
105
106             if (dialog.success()) {
107                 passphrase = new String JavaDoc(dialog.getPassword(), 0, dialog
108                         .getPassword().length);
109                 props.put("PASSWORD", passphrase);
110
111                 boolean save = dialog.getSave();
112
113                 // save passphrase in hash map
114
if (save) {
115                     this.passwordMap.put(props.get("USERID"), passphrase);
116                 }
117
118                 ret = true;
119             } else {
120                 ret = false;
121             }
122         }
123
124         con.setProperties(props);
125
126         return ret;
127     }
128 }
129
Popular Tags