You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by ru...@apache.org on 2006/03/18 17:18:33 UTC

svn commit: r386862 - in /webservices/axis2/trunk/java/modules/security: src/org/apache/axis2/security/trust/ test/org/apache/axis2/security/trust/

Author: ruchithf
Date: Sat Mar 18 08:18:32 2006
New Revision: 386862

URL: http://svn.apache.org/viewcvs?rev=386862&view=rev
Log:
Added the SimpleTokenStore and a test case

Added:
    webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/SimpleTokenStore.java
    webservices/axis2/trunk/java/modules/security/test/org/apache/axis2/security/trust/SimpleTokenStoreTest.java
Modified:
    webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/Token.java
    webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/TokenStorage.java
    webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/errors.properties

Added: webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/SimpleTokenStore.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/SimpleTokenStore.java?rev=386862&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/SimpleTokenStore.java (added)
+++ webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/SimpleTokenStore.java Sat Mar 18 08:18:32 2006
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.axis2.security.trust;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+/**
+ * In-memory implementation of the token storage
+ */
+public class SimpleTokenStore implements TokenStorage {
+
+    private Hashtable tokens = new Hashtable();
+
+    public void add(Token token) throws TrustException {
+        if (token != null && !"".equals(token.getId()) && 
+                token.getId() != null) {
+            if (this.tokens.keySet().size() == 0
+                    || (this.tokens.keySet().size() > 0 && !this.tokens
+                            .keySet().contains(token.getId()))) {
+                tokens.put(token.getId(), token);
+            } else {
+                throw new TrustException("tokenAlreadyExists",
+                        new String[] { token.getId() });
+            }
+
+        }
+    }
+
+    public void update(Token token) throws TrustException {
+        if (token != null && !"".equals(token.getId()) && 
+                token.getId() != null) {
+            if(this.tokens.keySet().size() == 0 ||
+                    (this.tokens.keySet().size() > 0 && 
+                            !this.tokens.keySet().contains(token.getId()))) {
+                    throw new TrustException("noTokenToUpdate",
+                        new String[] { token.getId() });
+            }
+            this.tokens.remove(this.tokens.get(token.getId()));
+            this.tokens.put(token.getId(), token);
+        }
+    }
+
+    public String[] gettokenIdentifiers() throws TrustException {
+        if (this.tokens.size() == 0) {
+            return null;
+        }
+        String[] ids = new String[this.tokens.size()];
+        Iterator iter = this.tokens.keySet().iterator();
+        for (int i = 0; i < ids.length; i++) {
+            ids[i] = (String) iter.next();
+        }
+        return ids;
+    }
+
+    public ArrayList getExpiredTokens() throws TrustException {
+        return getTokens(Token.EXPIRED);
+    }
+
+    public ArrayList getCancelledTokens() throws TrustException {
+        return getTokens(Token.CANCELLED);
+    }
+    
+    public ArrayList getValidTokens() throws TrustException {
+        ArrayList issued = getTokens(Token.ISSUED);
+        ArrayList renewed = getTokens(Token.RENEWED);
+        Iterator renewedIter = renewed.iterator();
+        while (renewedIter.hasNext()) {
+            issued.add(renewedIter.next());
+        }
+        return issued;
+    }
+
+    public ArrayList getRenewedTokens() throws TrustException {
+        return getTokens(Token.RENEWED);
+    }
+    
+    private ArrayList getTokens(int state) throws TrustException {
+        if (this.tokens.size() == 0) {
+            return null;
+        }
+        Iterator iter = this.tokens.keySet().iterator();
+        ArrayList list = new ArrayList();
+        while (iter.hasNext()) {
+            String id = (String) iter.next();
+            Token tok = (Token)this.tokens.get(id);
+            if(tok.getState() == state) {
+                list.add(tok);
+            }
+        }
+        if(list.size() > 0) {
+            return list;
+        } else {
+            return null;
+        }
+             
+    }
+}

Modified: webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/Token.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/Token.java?rev=386862&r1=386861&r2=386862&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/Token.java (original)
+++ webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/Token.java Sat Mar 18 08:18:32 2006
@@ -20,6 +20,23 @@
 
 import org.apache.ws.commons.om.OMElement;
 
+/**
+ * This represents a security token which can have either one of 4 states.
+ * <ul>
+ * <li>ISSUED</li>
+ * <li>EXPIRED</li>
+ * <li>CACELLED</li>
+ * <li>RENEWED</li>
+ * </ul>
+ * Also this holds the <code>OMElement</code>s representing the token in its 
+ * present state and the previous state.
+ * 
+ * These tokens are stired using the storage mechanism provided via the 
+ * <code>TokenStorage</code> interface.
+ * @see org.apache.axis2.security.trust.TokenStorage
+ * 
+ * @author Ruchith Fernando (ruchith.fernando@gmail.com)
+ */
 public class Token {
     
     public final static int ISSUED = 1;
@@ -43,11 +60,9 @@
     private OMElement token;
     
     /**
-     * The original token
-     * This will be differnet from the <code>token</code> if and only
-     * if there was a change in the properties of the token it self
+     * The token in its previous state
      */
-    private OMElement originalToken;
+    private OMElement presivousToken;
     
     /**
      * A bag to hold anyother properties
@@ -83,21 +98,7 @@
     
     public Token(String id, OMElement tokenElem) {
         this.id = id;
-        this.originalToken = tokenElem; 
-    }
-    
-    /**
-     * @return Returns the originalToken.
-     */
-    protected OMElement getOriginalToken() {
-        return originalToken;
-    }
-
-    /**
-     * @param originalToken The originalToken to set.
-     */
-    protected void setOriginalToken(OMElement originalToken) {
-        this.originalToken = originalToken;
+        this.token = tokenElem; 
     }
 
     /**
@@ -148,4 +149,20 @@
     protected String getId() {
         return id;
     }
+
+    /**
+     * @return Returns the presivousToken.
+     */
+    protected OMElement getPresivousToken() {
+        return presivousToken;
+    }
+
+    /**
+     * @param presivousToken The presivousToken to set.
+     */
+    protected void setPresivousToken(OMElement presivousToken) {
+        this.presivousToken = presivousToken;
+    }
+    
+    
 }

Modified: webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/TokenStorage.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/TokenStorage.java?rev=386862&r1=386861&r2=386862&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/TokenStorage.java (original)
+++ webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/TokenStorage.java Sat Mar 18 08:18:32 2006
@@ -16,14 +16,56 @@
 
 package org.apache.axis2.security.trust;
 
+import java.util.ArrayList;
+
+/**
+ * The storage interface to store security tokens and
+ * manipulate them  
+ */
 public interface TokenStorage {
     
+    /**
+     * Add the given token to the list.
+     * @param token The token to be added
+     * @throws TrustException
+     */
     public void add(Token token) throws TrustException;
     
+    /**
+     * Update an existing token.
+     * @param token
+     * @throws TrustException
+     */
     public void update(Token token) throws TrustException;
     
-    //Utility methods
+    /**
+     * Return the list of all token identifiers.
+     * @return
+     * @throws TrustException
+     */
     public String[] gettokenIdentifiers() throws TrustException;
 
+    /**
+     * Return the list of <code>EXPIRED</code> tokens.
+     * If there are no <code>EXPIRED</code> tokens <code>null</code> will be 
+     * returned
+     * @return
+     * @throws TrustException
+     */
+    public ArrayList getExpiredTokens() throws TrustException;
+    
+    /**
+     * Return the list of ISSUED and RENEWED tokens.
+     * @return
+     * @throws TrustException
+     */
+    public ArrayList getValidTokens() throws TrustException;
+    
+    /**
+     * Return the list of RENEWED tokens.
+     * @return
+     * @throws TrustException
+     */
+    public ArrayList getRenewedTokens() throws TrustException;
     
 }

Modified: webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/errors.properties
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/errors.properties?rev=386862&r1=386861&r2=386862&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/errors.properties (original)
+++ webservices/axis2/trunk/java/modules/security/src/org/apache/axis2/security/trust/errors.properties Sat Mar 18 08:18:32 2006
@@ -16,4 +16,6 @@
 cannotLoadClass = Error in loading and instanciating the class \"{0}\"
 invalidTokenTypeDefinition = Invalid 'tokenType' definition in \"{0}\" : \"{1}\"
 errorLoadingConfigFile = Error in loading configuration file : \"{0}\"
-defaultIssuerMissing = The default issuer must be specified
\ No newline at end of file
+defaultIssuerMissing = The default issuer must be specified
+tokenAlreadyExists = "The token \"{0}\" already exists in the store
+noTokenToUpdate = Canot find token : \"{0}\"to update 
\ No newline at end of file

Added: webservices/axis2/trunk/java/modules/security/test/org/apache/axis2/security/trust/SimpleTokenStoreTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/security/test/org/apache/axis2/security/trust/SimpleTokenStoreTest.java?rev=386862&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/security/test/org/apache/axis2/security/trust/SimpleTokenStoreTest.java (added)
+++ webservices/axis2/trunk/java/modules/security/test/org/apache/axis2/security/trust/SimpleTokenStoreTest.java Sat Mar 18 08:18:32 2006
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.axis2.security.trust;
+
+import java.util.ArrayList;
+
+import junit.framework.TestCase;
+
+public class SimpleTokenStoreTest extends TestCase {
+
+    public void testAdd() {
+        Token token = new Token("id-1");
+        SimpleTokenStore store = new SimpleTokenStore();
+        try {
+            store.add(token);
+        } catch (TrustException e) {
+            fail("Adding a new token to an empty store should not fail, " +
+                    "message : " + e.getMessage());
+        }
+        try {
+            store.add(token);
+            fail("Adding an existing token must throw an exception");
+        } catch (TrustException e) {
+            assertEquals("Incorrect exception message", 
+                    TrustException.getMessage("tokenAlreadyExists", 
+                    new String[] {token.getId()}), e.getMessage());
+        }
+    }
+    
+    public void testGettokenIdentifiers() {
+        SimpleTokenStore store = new SimpleTokenStore();
+        try {
+            String[] ids = store.gettokenIdentifiers();
+            assertNull("There should not be any token ids at this point", ids);
+        } catch (TrustException e) {
+            fail(e.getMessage());
+        }
+        try {
+            store.add(new Token("id-1"));
+            store.add(new Token("id-2"));
+            store.add(new Token("id-3"));
+            String[] ids = store.gettokenIdentifiers();
+            assertEquals("Incorrect number fo token ids", 3, ids.length);
+        } catch (TrustException e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    public void testUpdate() {
+        SimpleTokenStore store = new SimpleTokenStore();
+        Token token1 = new Token("id-1");
+        try {
+            store.update(token1);
+            fail("An exception must be thrown at this point : noTokenToUpdate");
+        } catch (TrustException e) {
+            assertEquals("Incorrect exception message", TrustException
+                    .getMessage("noTokenToUpdate", new String[] { token1
+                            .getId() }), e.getMessage());
+        }
+        try {
+            Token token = token1;
+            store.add(token);
+            store.add(new Token("id-2"));
+            store.add(new Token("id-3"));
+            token.setState(Token.EXPIRED);
+            store.update(token);
+        } catch (TrustException e) {
+            fail(e.getMessage());
+        }
+    }
+    
+    public void testGetValidExpiredRenewedTokens() {
+        SimpleTokenStore store = new SimpleTokenStore();
+        
+        Token token1 = new Token("id-1");
+        Token token2 = new Token("id-2");
+        Token token3 = new Token("id-3");
+        Token token4 = new Token("id-4");
+        Token token5 = new Token("id-5");
+        Token token6 = new Token("id-6");
+        Token token7 = new Token("id-7");
+        
+        token1.setState(Token.ISSUED);
+        token2.setState(Token.ISSUED);
+        token3.setState(Token.ISSUED);
+        token4.setState(Token.RENEWED);
+        token5.setState(Token.RENEWED);
+        token6.setState(Token.EXPIRED);
+        token7.setState(Token.CANCELLED);
+        
+        try {
+            store.add(token1);
+            store.add(token2);
+            store.add(token3);
+            store.add(token4);
+            store.add(token5);
+            store.add(token6);
+            store.add(token7);
+            
+            ArrayList list = store.getValidTokens();
+            ArrayList list2 = store.getExpiredTokens();
+            ArrayList list3 = store.getRenewedTokens();
+            ArrayList list4 = store.getCancelledTokens();
+            
+            assertEquals("Incorrect number of valid tokens", 5, list.size());
+            assertEquals("Incorrect number of expired tokens", 1, 
+                    list2.size());
+            assertEquals("Incorrect number of newed tokens", 2, list3.size());
+            assertEquals("Incorrect number of newed tokens", 1, list4.size());
+            
+        } catch (TrustException e) {
+            fail(e.getMessage());
+        }
+    }
+}