public class PasswordCache extends Object
TODO move swing stuff into gui, implement cli as well, auto-select cli/gui this can be done by implementing this class with callbacks for asking the user for passwords. Different UIs can then use their own callbacks, and even the tests can use this to test easily without user intervention.
Modifier and Type | Class | Description |
---|---|---|
protected class |
PasswordCache.ForgetTask |
Timer task that removes the cached password after timeout
|
Modifier and Type | Field | Description |
---|---|---|
protected boolean |
alwaysAskForEncrypt |
|
protected static PasswordCache |
instance |
Reference to singleton instance
|
protected JFrame |
parent |
parent frame for dialogs
|
protected HashMap<String,char[]> |
passwords |
the actual passwords
|
protected int |
timeout |
number of seconds after which passwords are forgotten
|
protected HashMap<String,Timer> |
timers |
list of forget-timers for each password
|
protected int |
ui |
user interface backend, one of UI_*
|
static int |
UI_CLI |
TODO UI: console, asks for a password on the console
|
static int |
UI_GUI |
UI: graphical, pops up a dialog when password isn't in cache
|
static int |
UI_NONE |
UI: no questions asked, returns null if password isn't in cache
|
Modifier | Constructor | Description |
---|---|---|
protected |
PasswordCache() |
Modifier and Type | Method | Description |
---|---|---|
void |
clear() |
Completely clear the cache so that no passwords are present.
|
protected nl.nikhef.jgridstart.passwordcache.PasswordCache.CachePasswordFinder |
getDecryptPasswordFinder(String msg,
String loc) |
Return a
PasswordFinder that returns the cached password, or else
asks the user to provide one. |
protected nl.nikhef.jgridstart.passwordcache.PasswordCache.CachePasswordFinder |
getEncryptPasswordFinder(String msg,
String loc) |
Return a
PasswordFinder that asks the user for a password when
encrypting a file. |
char[] |
getForDecrypt(String msg,
String loc) |
Return the password for the specified location to decrypt files
|
char[] |
getForEncrypt(String msg,
String loc) |
Return a new password for the specified location.
|
static PasswordCache |
getInstance() |
Retrieve singleton object
|
int |
getTimeout() |
Return the current password timeout.
|
int |
getUI() |
Return the current user-interface backend.
|
void |
invalidate(String loc) |
Invalidate a cache entry
|
static boolean |
isPasswordCancelledException(Exception e) |
|
static boolean |
isPasswordCancelledException(Throwable e) |
TODO document
|
static boolean |
isPasswordNotSuppliedException(Exception e) |
|
static boolean |
isPasswordNotSuppliedException(Throwable e) |
TODO document
|
static boolean |
isPasswordWrongException(Exception e) |
|
static boolean |
isPasswordWrongException(Throwable e) |
TODO document
|
protected void |
optionPaneSetFocus(JComponent c) |
Helper method to set the focus component of a JOptionPane.
|
void |
set(String loc,
char[] pw) |
Set a password for a location.
|
boolean |
setAlwaysAskForEncrypt(boolean alwaysAsk) |
Set whether or not to always ask a password for encryption.
|
void |
setParent(JFrame parent) |
Set the parent for dialogs
|
void |
setTimeout(int s) |
Set the timeout after which passwords are forgotten again.
|
void |
setUI(int ui) |
Set the user-interface backend.
|
protected void |
touch(String loc) |
Reset the forget timeout for a location.
|
public static final int UI_NONE
public static final int UI_GUI
public static final int UI_CLI
protected int ui
protected JFrame parent
protected int timeout
protected boolean alwaysAskForEncrypt
protected static PasswordCache instance
public static PasswordCache getInstance()
public void setParent(JFrame parent)
public void setTimeout(int s)
Also updates timeout of currently stored passwords.
s
- timeout in number of seconds, or <0 to keep foreverpublic int getTimeout()
setTimeout(int)
public void setUI(int ui)
ui
- One of UI_NONE, UI_GUI and UI_CLIpublic int getUI()
public void invalidate(String loc)
public char[] getForDecrypt(String msg, String loc) throws PasswordCancelledException
Each item has a unique reference to a required password consisting of a location string.
msg
- Message to present to the user when asking for a password.
Actual message is "Enter password to unlock "+msgloc
- Location stringPasswordCancelledException
public char[] getForEncrypt(String msg, String loc) throws PasswordCancelledException
Also ask for a password and require to enter twice for verification. This is for encrypting data. The password is remembered for the case it is read again.
msg
- Description presented to the user when asking for a password.loc
- Location stringPasswordCancelledException
public void set(String loc, char[] pw)
Should not be used normally because the user is the one to give the password. For testing this is convenient.
loc
- Location stringpw
- password to setpublic void clear()
protected void touch(String loc)
public boolean setAlwaysAskForEncrypt(boolean alwaysAsk)
protected nl.nikhef.jgridstart.passwordcache.PasswordCache.CachePasswordFinder getEncryptPasswordFinder(String msg, String loc)
PasswordFinder
that asks the user for a password when
encrypting a file. The password is retrieved using getForEncrypt(java.lang.String, java.lang.String)
.msg
- Description on what this key is aboutloc
- Location stringprotected nl.nikhef.jgridstart.passwordcache.PasswordCache.CachePasswordFinder getDecryptPasswordFinder(String msg, String loc)
PasswordFinder
that returns the cached password, or else
asks the user to provide one. The password is retrieved using
getForDecrypt(java.lang.String, java.lang.String)
.msg
- Description on what this key is aboutloc
- Location stringpublic static boolean isPasswordWrongException(Throwable e)
public static boolean isPasswordWrongException(Exception e)
isPasswordWrongException(Throwable)
public static boolean isPasswordNotSuppliedException(Throwable e)
public static boolean isPasswordNotSuppliedException(Exception e)
public static boolean isPasswordCancelledException(Throwable e)
public static boolean isPasswordCancelledException(Exception e)
isPasswordCancelledException(Throwable)
protected void optionPaneSetFocus(JComponent c)
Thank you, Java bug 5018574.
Copyright © 2010-2018 Nikhef / Stichting FOM. All Rights Reserved.