001 /*
002 * Copyright (c) 2002-2005 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.security;
017
018 import org.springframework.security.Authentication;
019 import org.springframework.security.AuthenticationException;
020 import org.springframework.security.AuthenticationManager;
021 import org.springframework.security.BadCredentialsException;
022 import org.springframework.security.GrantedAuthority;
023 import org.springframework.security.GrantedAuthorityImpl;
024 import org.springframework.security.LockedException;
025 import org.springframework.security.providers.TestingAuthenticationToken;
026
027 /**
028 * Test implementation giving us control over the authentication results.
029 *
030 * @author Larry Streepy
031 */
032 public class TestAuthenticationManager implements AuthenticationManager {
033
034 /** Test role */
035 public static final String ROLE_EXPECTED = "ROLE_EXPECTED";
036
037 /** Token to use to force a successful authentication. */
038 public static final Authentication VALID_USER1 = new TestingAuthenticationToken( "USER1", "FOO",
039 new GrantedAuthority[] { new GrantedAuthorityImpl( ROLE_EXPECTED ) } );
040
041 /** Token to use to force a successful authentication. */
042 public static final Authentication VALID_USER2 = new TestingAuthenticationToken( "USER2", "FOO");
043
044 /** Token to use to force a failed (bad credentials) authentication. */
045 public static final Authentication BAD_CREDENTIALS = new TestingAuthenticationToken( "FAIL", "FOO");
046
047 /** Token to use to force a LOCKED authentication exception. */
048 public static final Authentication LOCKED = new TestingAuthenticationToken( "LOCKED", "FOO");
049
050 /**
051 * Construct a token with the given id, password, and role
052 */
053 public static Authentication makeAuthentication( String user, String password, String role ) {
054 return new TestingAuthenticationToken( user, password,
055 new GrantedAuthority[] { new GrantedAuthorityImpl( role ) } );
056 }
057
058 /**
059 * Authenticate a token
060 */
061 public Authentication authenticate(Authentication authentication) throws AuthenticationException {
062 if( authentication == BAD_CREDENTIALS ) {
063 throw new BadCredentialsException( "Bad credentials" );
064 } else if( authentication == LOCKED ) {
065 throw new LockedException( "Account is locked" );
066 }
067 return authentication;
068 }
069 }