1   /*
2    * Copyright (c) 2002-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.security;
17  
18  import org.springframework.security.Authentication;
19  import org.springframework.security.AuthenticationException;
20  import org.springframework.security.AuthenticationManager;
21  import org.springframework.security.BadCredentialsException;
22  import org.springframework.security.GrantedAuthority;
23  import org.springframework.security.GrantedAuthorityImpl;
24  import org.springframework.security.LockedException;
25  import org.springframework.security.providers.TestingAuthenticationToken;
26  
27  /**
28   * Test implementation giving us control over the authentication results.
29   * 
30   * @author Larry Streepy
31   */
32  public class TestAuthenticationManager implements AuthenticationManager {
33  
34      /** Test role */
35      public static final String ROLE_EXPECTED = "ROLE_EXPECTED";
36      
37      /** Token to use to force a successful authentication. */
38      public static final Authentication VALID_USER1 = new TestingAuthenticationToken( "USER1", "FOO",
39          new GrantedAuthority[] { new GrantedAuthorityImpl( ROLE_EXPECTED ) } );
40  
41      /** Token to use to force a successful authentication. */
42      public static final Authentication VALID_USER2 = new TestingAuthenticationToken( "USER2", "FOO");
43  
44      /** Token to use to force a failed (bad credentials) authentication. */
45      public static final Authentication BAD_CREDENTIALS = new TestingAuthenticationToken( "FAIL", "FOO");
46  
47      /** Token to use to force a LOCKED authentication exception. */
48      public static final Authentication LOCKED = new TestingAuthenticationToken( "LOCKED", "FOO");
49  
50      /**
51       * Construct a token with the given id, password, and role
52       */
53      public static Authentication makeAuthentication( String user, String password, String role ) {
54          return new TestingAuthenticationToken( user, password,
55              new GrantedAuthority[] { new GrantedAuthorityImpl( role ) } );
56      }
57  
58      /**
59       * Authenticate a token
60       */
61      public Authentication authenticate(Authentication authentication) throws AuthenticationException {
62          if( authentication == BAD_CREDENTIALS ) {
63              throw new BadCredentialsException( "Bad credentials" );
64          } else if( authentication == LOCKED ) {
65              throw new LockedException( "Account is locked" );
66          }
67          return authentication;
68      }
69  }