1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
118 CommunityIvornParser ivorn = new CommunityIvornParser(
119 ident
120 ) ;
121 log.debug(" Ivorn : " + ivorn) ;
122
123
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
139 UID uid = new UID() ;
140
141
142 Ivorn ivorn = CommunityAccountIvornFactory.createMock(
143 "",
144 uid.toString()
145 ) ;
146
147
148 SecurityToken token = new SecurityToken(
149 account.getAccountIdent(),
150 ivorn.toString()
151 ) ;
152
153
154 token.setStatus(SecurityToken.VALID_TOKEN) ;
155
156
157 map.put(token.getToken(), token) ;
158
159
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
182 if (null != secret)
183 {
184
185
186 if (secret.equals(value))
187 {
188
189
190 return this.createToken(ident) ;
191 }
192
193
194 else {
195
196
197 throw new CommunitySecurityException(
198 "Invalid password"
199 ) ;
200 }
201 }
202
203
204 else {
205
206
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
231 original.setStatus(SecurityToken.INVALID_TOKEN) ;
232
233
234 SecurityToken match = (SecurityToken) map.get(original.getToken()) ;
235
236
237 if (null != match)
238 {
239
240
241 map.remove(match.getToken()) ;
242
243
244 return this.createToken(original.getAccount()) ;
245 }
246
247
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
277 original.setStatus(SecurityToken.INVALID_TOKEN) ;
278
279
280 SecurityToken match = (SecurityToken) map.get(original.getToken()) ;
281
282
283 if (null != match)
284 {
285
286
287 map.remove(match.getToken()) ;
288
289
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
301 else {
302 throw new CommunitySecurityException(
303 "Original token not valid"
304 ) ;
305 }
306 }
307 }