Author: [email protected]
Date: Mon Oct 17 09:15:38 2011
New Revision: 1516
Log:
[AMDATUAUTH-106] Fixed proper working of in-memory token store
Modified:
trunk/amdatu-auth/auth-stores/mem-store-token/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/Token.java
Modified:
trunk/amdatu-auth/auth-stores/mem-store-token/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java
==============================================================================
---
trunk/amdatu-auth/auth-stores/mem-store-token/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java
(original)
+++
trunk/amdatu-auth/auth-stores/mem-store-token/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java
Mon Oct 17 09:15:38 2011
@@ -41,22 +41,22 @@
+ m_tenant.getId() + "'");
}
- public void addToken(Token token) {
- m_tokens.put(token.getToken(), token);
+ public synchronized void addToken(Token token) {
+ m_tokens.put(token.getToken(), token.clone());
}
-
- public Token getToken(String token) {
+
+ public synchronized Token getToken(String token) {
if (m_tokens.containsKey(token)) {
- return m_tokens.get(token);
+ return m_tokens.get(token).clone();
}
return null;
}
- public boolean hasToken(final String token) {
+ public synchronized boolean hasToken(final String token) {
return m_tokens.containsKey(token);
}
- public void removeToken(final Token token) {
+ public synchronized void removeToken(final Token token) {
m_tokens.remove(token.getToken());
}
}
Modified:
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/Token.java
==============================================================================
---
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/Token.java
(original)
+++
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/Token.java
Mon Oct 17 09:15:38 2011
@@ -22,29 +22,30 @@
* This class represents a generic token. A token is a unique and randomly
generated String,
* associated with a secret (which is also a unique and randomly generated
String). A timestamp
* is assigned to a token to facilitate token expiration.
- * Arbitrary String properties can be assigned to a token.
+ * Arbitrary String properties can be assigned to a token.
*
* @author ivol
*/
public class Token {
// The token
private String m_token;
-
+
// The secret associated with the token, can be used to claim ownership of
this token
private String m_tokenSecret;
-
+
// Timestamp of when the token was generated, used for tokens that a
limited lifetime
private long m_timestamp;
-
+
// Generic map of token proeprties.
private Map<String, String> m_properties;
-
+
/**
* Constructor. Creates a new token
+ *
* @param token The uniquely random generated String
* @param tokenSecret The secret, that is supposed to authenticate the
owner of the token.
- * The token may be publicly available (i.e. send to the client in a http
response) but
- * the secret is only known by the owner.
+ * The token may be publicly available (i.e. send to the client in
a http response) but
+ * the secret is only known by the owner.
* @param timestamp The time at which the token was generated
*/
public Token(String token, String tokenSecret, long timestamp) {
@@ -55,6 +56,7 @@
/**
* Returns the token. The token is publicly known.
+ *
* @return the token.
*/
public String getToken() {
@@ -63,6 +65,7 @@
/**
* Sets the token.
+ *
* @param token the token to set
*/
public void setToken(String token) {
@@ -72,6 +75,7 @@
/**
* Returns the token secret. The token secret associated with this token
* is only known by the owner.
+ *
* @return the secret that only the token owner knows
*/
public String getTokenSecret() {
@@ -80,6 +84,7 @@
/**
* Sets the token secret.
+ *
* @param tokenSecret the token secret to set
*/
public void setTokenSecret(String tokenSecret) {
@@ -88,6 +93,7 @@
/**
* Returns the timestamp in milliseconds of the time at which the token
was created.
+ *
* @return the timestamp in milliseconds
*/
public long getTimestamp() {
@@ -97,6 +103,7 @@
/**
* Sets the timestamp
+ *
* @param timestamp
*/
public void setTimestamp(long timestamp) {
@@ -105,19 +112,20 @@
/**
* Returns the map of arbitrary properties
+ *
* @return
*/
public Map<String, String> getProperties() {
return m_properties;
}
-
+
public String getProperty(String key) {
if (m_properties == null) {
return null;
}
return m_properties.get(key);
}
-
+
public void setProperties(Map<String, String> properties) {
m_properties = properties;
}
@@ -128,22 +136,40 @@
}
m_properties.put(key, value);
}
-
+
/**
* Verifies if the token is expired comparing its timestamp with the
system time.
* Returns true of the timestamp of this token + the provided validity
duration equals
* or is higher then the current time.
- * @param validityDuration The amount of milliseconds the token is valid.
Can be 0. If the
- * validityDuration is smaller then 0, this method always returns false.
+ *
+ * @param validityDuration The amount of milliseconds the token is valid.
Can be 0. If the
+ * validityDuration is smaller then 0, this method always returns
false.
* @return
*/
public boolean isExpired(long validityDuration) {
if (validityDuration < 0) {
return false;
- } else if (validityDuration == 0) {
+ }
+ else if (validityDuration == 0) {
return true;
}
long expiryDate = m_timestamp + validityDuration;
return System.currentTimeMillis() >= expiryDate;
}
+
+ public Token clone() {
+ Token clone = new Token(m_token, m_tokenSecret, m_timestamp);
+ if (getProperties() != null) {
+ clone.setProperties(clone(getProperties()));
+ }
+ return clone;
+ }
+
+ private Map<String, String> clone(Map<String, String> map) {
+ HashMap<String, String> newMap = new HashMap<String, String>();
+ for (String key : map.keySet()) {
+ newMap.put(key, map.get(key));
+ }
+ return newMap;
+ }
}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits