View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/common/src/java/org/astrogrid/community/common/security/service/SecurityServiceMock.java,v $</cvs:source>
3    * <cvs:author>$Author: dave $</cvs:author>
4    * <cvs:date>$Date: 2004/09/16 23:18:08 $</cvs:date>
5    * <cvs:version>$Revision: 1.11 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: SecurityServiceMock.java,v $
9    *   Revision 1.11  2004/09/16 23:18:08  dave
10   *   Replaced debug logging in Community.
11   *   Added stream close() to FileStore.
12   *
13   *   Revision 1.10.82.1  2004/09/16 09:58:48  dave
14   *   Replaced debug with commons logging ....
15   *
16   *   Revision 1.10  2004/06/18 13:45:20  dave
17   *   Merged development branch, dave-dev-200406081614, into HEAD
18   *
19   *   Revision 1.9.18.3  2004/06/17 14:50:03  dave
20   *   Removed unused imports (PMD report).
21   *
22   *   Revision 1.9.18.2  2004/06/17 13:38:59  dave
23   *   Tidied up old CVS log entries
24   *
25   * </cvs:log>
26   *
27   */
28  package org.astrogrid.community.common.security.service ;
29  
30  import org.apache.commons.logging.Log ;
31  import org.apache.commons.logging.LogFactory ;
32  
33  import java.util.Map ;
34  import java.util.HashMap ;
35  import java.util.Vector ;
36  
37  import org.astrogrid.store.Ivorn ;
38  
39  import java.rmi.server.UID ;
40  
41  import org.astrogrid.community.common.security.data.SecurityToken ;
42  
43  import org.astrogrid.community.common.service.CommunityServiceMock ;
44  
45  import org.astrogrid.community.common.ivorn.CommunityIvornParser ;
46  import org.astrogrid.community.common.ivorn.CommunityAccountIvornFactory ;
47  
48  import org.astrogrid.community.common.exception.CommunitySecurityException ;
49  import org.astrogrid.community.common.exception.CommunityIdentifierException  ;
50  
51  /***
52   * Mock implementation of our SecurityService service.
53   *
54   */
55  public class SecurityServiceMock
56      extends CommunityServiceMock
57      implements SecurityService
58      {
59      /***
60       * Our debug logger.
61       *
62       */
63      private static Log log = LogFactory.getLog(SecurityServiceMock.class);
64  
65      /***
66       * Public constructor.
67       *
68       */
69      public SecurityServiceMock()
70          {
71          super() ;
72          log.debug("") ;
73          log.debug("----\"----") ;
74          log.debug("SecurityServiceMock()") ;
75          }
76  
77      /***
78       * Our test password.
79       *
80       */
81      private static String secret = null ;
82  
83      /***
84       * Access to out test password.
85       *
86       */
87      public static String getPassword()
88          {
89          return secret ;
90          }
91  
92      /***
93       * Access to out test password.
94       *
95       */
96      public static void setPassword(String value)
97          {
98          secret = value ;
99          }
100 
101     /***
102      * Our hash table of tokens.
103      *
104      */
105     protected static Map map = new HashMap() ;
106 
107     /***
108      * Generate a new token.
109      * @param ident - The Account ident.
110      * @throws CommunityIdentifierException If the new Ivorn is invalid
111      *
112      */
113     protected SecurityToken createToken(String ident)
114         throws CommunityIdentifierException
115         {
116         //
117         // Get the Account ident.
118         CommunityIvornParser ivorn = new CommunityIvornParser(
119             ident
120             ) ;
121         log.debug("  Ivorn : " + ivorn) ;
122         //
123         // Generate a new token.
124         return this.createToken(ivorn) ;
125         }
126 
127     /***
128      * Generate a new token.
129      * @param ident - The Account ident.
130      * @throws CommunityIdentifierException If the new Ivorn is invalid
131      * @todo Change this to use the UniqueIdentifier library.
132      *
133      */
134     protected SecurityToken createToken(CommunityIvornParser account)
135         throws CommunityIdentifierException
136         {
137         //
138         // Create a new UID.
139         UID uid = new UID() ;
140         //
141         // Create an Ivorn for the token.
142         Ivorn ivorn = CommunityAccountIvornFactory.createMock(
143             "",
144             uid.toString()
145             ) ;
146         //
147         // Issue a new Security token to the account.
148         SecurityToken token = new SecurityToken(
149             account.getAccountIdent(),
150             ivorn.toString()
151             ) ;
152         //
153         // Mark the token as valid.
154         token.setStatus(SecurityToken.VALID_TOKEN) ;
155         //
156         // Add the token to our map.
157         map.put(token.getToken(), token) ;
158         //
159         // Return the new token.
160         return token ;
161         }
162 
163     /***
164      * Check an Account password.
165      * @param account The account ident.
166      * @param pass The account password.
167      * @return A valid SecurityToken if the ident and password are valid.
168      * @throws CommunityIdentifierException If the new Ivorn is invalid
169      * @todo Actually check the password ?
170      *
171      */
172     public SecurityToken checkPassword(String ident, String value)
173         throws CommunitySecurityException, CommunityIdentifierException
174         {
175         log.debug("") ;
176         log.debug("----\"----") ;
177         log.debug("SecurityServiceMock.checkPassword()") ;
178         log.debug("  Ident : " + ident) ;
179         log.debug("  Value : " + value) ;
180         //
181         // If we have a test password.
182         if (null != secret)
183             {
184             //
185             // Check the password matches.
186             if (secret.equals(value))
187                 {
188                 //
189                 // Return a new token.
190                 return this.createToken(ident) ;
191                 }
192             //
193             // If the passwords don't match.
194             else {
195                 //
196                 // Throw an Exception
197                 throw new CommunitySecurityException(
198                     "Invalid password"
199                     ) ;
200                 }
201             }
202         //
203         // If we don't have a test password.
204         else {
205             //
206             // Just return a new token.
207             return this.createToken(ident) ;
208             }
209         }
210 
211     /***
212      * Validate a SecurityToken.
213      * Validates a token, and creates a new tokens issued to the same account.
214      * Note, this uses the original token, which now becomes invalid.
215      * The client should use the new token for subsequent calls to the service.
216      * @param The token to validate.
217      * @return A new SecurityToken if the original was valid.
218      * @throws CommunitySecurityException If the original token is not valid.
219      * @throws CommunityIdentifierException If the new Ivorn is invalid
220      *
221      */
222     public SecurityToken checkToken(SecurityToken original)
223         throws CommunitySecurityException, CommunityIdentifierException
224         {
225         log.debug("") ;
226         log.debug("----\"----") ;
227         log.debug("SecurityServiceMock.checkToken()") ;
228         log.debug("  Token : " + original) ;
229         //
230         // Mark the original as invalid.
231         original.setStatus(SecurityToken.INVALID_TOKEN) ;
232         //
233         // See if we still have the orginal
234         SecurityToken match = (SecurityToken) map.get(original.getToken()) ;
235         //
236         // If we do have the original.
237         if (null != match)
238             {
239             //
240             // Remove the original from our map
241             map.remove(match.getToken()) ;
242             //
243             // Generate a new token.
244             return this.createToken(original.getAccount()) ;
245             }
246         //
247         // If we don't have the original.
248         else {
249             throw new CommunitySecurityException(
250                 "Original token not valid"
251                 ) ;
252             }
253         }
254 
255     /***
256      * Split a SecurityToken.
257      * Validates a token, and then creates a new set of tokens issued to the same account.
258      * Note, this uses the original token, which now becomes invalid.
259      * The client should use the first token in the array for subsequent calls to the service.
260      * @param The token to validate.
261      * @param The number of new tokens required.
262      * @return An array of new tokens.
263      * @throws CommunitySecurityException If the original token is not valid.
264      * @throws CommunityIdentifierException If the new Ivorn is invalid
265      *
266      */
267     public Object[] splitToken(SecurityToken original, int count)
268         throws CommunitySecurityException, CommunityIdentifierException
269         {
270         log.debug("") ;
271         log.debug("----\"----") ;
272         log.debug("SecurityServiceMock.splitToken()") ;
273         log.debug("  Token : " + original) ;
274         log.debug("  Count : " + count) ;
275         //
276         // Mark the original as invalid.
277         original.setStatus(SecurityToken.INVALID_TOKEN) ;
278         //
279         // See if we still have the orginal
280         SecurityToken match = (SecurityToken) map.get(original.getToken()) ;
281         //
282         // If we do have the original.
283         if (null != match)
284             {
285             //
286             // Remove the original from our map
287             map.remove(match.getToken()) ;
288             //
289             // Generate a new set of tokens.
290             Vector vector = new Vector() ;
291             for (int i = 0 ; i < count ; i++)
292                 {
293                 vector.add(
294                     this.createToken(original.getAccount())
295                     ) ;
296                 }
297             return vector.toArray() ;
298             }
299         //
300         // If we don't have the original.
301         else {
302             throw new CommunitySecurityException(
303                 "Original token not valid"
304                 ) ;
305             }
306         }
307     }