K - The type of the keys.@ThreadSafe @DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class) public abstract class SafeKeyProvider<K extends SafeKey<K>> extends Object implements KeyProvider<K>
SafeKey).
A clone of this key is returned on each call to getWriteKey()
and getReadKey(boolean).| Modifier and Type | Class and Description |
|---|---|
private static class |
SafeKeyProvider.ThreadLocalLong |
| Modifier and Type | Field and Description |
|---|---|
private ThreadLocal<Long> |
invalidated |
private K |
key |
static int |
MIN_KEY_RETRY_DELAY
The minimum delay between subsequent attempts to verify a key in
milliseconds.
|
| Modifier | Constructor and Description |
|---|---|
protected |
SafeKeyProvider()
Constructs a new safe key provider.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
enforceSuspensionPenalty() |
protected K |
getKey() |
private K |
getNonNullKey() |
K |
getReadKey(boolean invalid)
Retrieves the secret key for the decryption of a protected resource.
|
K |
getWriteKey()
Retrieves the secret key for the encryption of a protected resource.
|
protected abstract void |
retrieveReadKey(boolean invalid)
Retrieves the secret key for the decryption of a protected resource.
|
protected abstract void |
retrieveWriteKey()
Retrieves the secret key for the encryption of a protected resource.
|
void |
setKey(K newKey)
Sets the secret key programmatically.
|
public static final int MIN_KEY_RETRY_DELAY
getReadKey(boolean) by the same thread.private final ThreadLocal<Long> invalidated
public final K getWriteKey() throws UnknownKeyException
Subsequent calls to this method return an object which at least compares
equal to any previously returned object, but is
not necessarily the same.
The implementation in SafeKeyProvider forwards the call to
retrieveWriteKey().
getWriteKey in interface KeyProvider<K extends SafeKey<K>>UnknownKeyException - If retrieveWriteKey throws
this exception or the secret key is still null.protected abstract void retrieveWriteKey()
throws UnknownKeyException
Subsequent calls to this method may return the same object.
UnknownKeyException - If the secret key is unknown.
At the subclasses discretion, this may mean that prompting for
the key has been disabled or cancelled by the user.getWriteKey()public final K getReadKey(boolean invalid) throws UnknownKeyException
Unless invalid is true, subsequent calls to this method
return an object which at least compares equal to
any previously returned object, but is not necessarily the same.
Important: From an application's perspective, a
KeyProvider is not trustworthy!
Hence, the key returned by this method must not only get authenticated,
but the application should also throttle the pace for the return from a
subsequent call to this method if the key is invalid in order to protect
the client application from an exhaustive search for the correct key.
As a rule of thumb, at least three seconds should pass between two
consecutive calls to this method by the same thread.
"Safe" implementations of this interface should enforce this
behaviour in order to protect client applications which do not obey
these considerations against abuses of the key provider implementation.
The implementation in SafeKeyProvider forwards the call to
retrieveReadKey(boolean) and enforces a three seconds suspension penalty
if invalid is true before returning.
Because this method is final, this qualifies the implementation in
this class as a "safe" KeyProvider implementation,
even when subclassed.
getReadKey in interface KeyProvider<K extends SafeKey<K>>invalid - true iff a previous call to this method resulted
in an invalid key.UnknownKeyException - If retrieveReadKey throws
this exception or the secret key is still null.protected abstract void retrieveReadKey(boolean invalid)
throws UnknownKeyException
Subsequent calls to this method may return the same object.
UnknownKeyException - If the secret key is unknown.
At the subclasses discretion, this may mean that prompting for
the key has been disabled or cancelled by the user.getReadKey(boolean)private K getNonNullKey() throws UnknownKeyException
UnknownKeyException@CheckForNull protected K getKey()
public void setKey(@CheckForNull
K newKey)
KeyProvidersetKey in interface KeyProvider<K extends SafeKey<K>>newKey - the secret key.
If this is null, this key provider is set to a state
as if prompting for the secret key had been cancelled.private void enforceSuspensionPenalty()
Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.