@Override public String getDisplayName() { return provider.getDisplayName(); }
public <ReturnType, CreateArgType, ConsumedType> AuthenticationPerformer<ReturnType, CreateArgType> getAuthenticationPerformer( Class<ReturnType> returnType, Class<CreateArgType> createArgType, String providerId ) { List<AuthenticationPerformer<ReturnType, CreateArgType>> performers = getSupportedAuthenticationPerformers( returnType, createArgType ); for ( AuthenticationPerformer<ReturnType, CreateArgType> candidatePerformer : performers ) { if ( candidatePerformer.getAuthenticationProvider().getId().equals( providerId ) ) { return candidatePerformer; } } return null; }
@SuppressWarnings( { "rawtypes", "unchecked" } ) @Test public void testRegisterConsumerFactory() throws AuthenticationConsumptionException, AuthenticationFactoryException { AuthenticationConsumer<Object, KerberosAuthenticationProvider> authConsumer = mock( AuthenticationConsumer.class ); AuthenticationConsumerFactory<Object, AuthenticationConsumer, KerberosAuthenticationProvider> factory = mock( AuthenticationConsumerFactory.class ); when( factory.getReturnType() ).thenReturn( Object.class ); when( factory.getCreateArgType() ).thenReturn( AuthenticationConsumer.class ); when( factory.getConsumedType() ).thenReturn( KerberosAuthenticationProvider.class ); when( factory.create( authConsumer ) ).thenReturn( authConsumer ); KerberosAuthenticationProvider kerberosAuthenticationProvider = new KerberosAuthenticationProvider( "kerb", "kerb", true, "pass", true, "none" ); manager.registerAuthenticationProvider( kerberosAuthenticationProvider ); manager.registerConsumerFactory( factory ); manager.getAuthenticationPerformer( Object.class, AuthenticationConsumer.class, kerberosAuthenticationProvider.getId() ).perform( authConsumer ); verify( authConsumer ).consume( kerberosAuthenticationProvider ); }
@SuppressWarnings( "unchecked" ) @Test public void testNoAuthProviderAndConsumer() throws AuthenticationConsumptionException, AuthenticationFactoryException { manager.registerConsumerClass( DelegatingNoAuthConsumer.class ); AuthenticationConsumer<Object, NoAuthenticationAuthenticationProvider> consumer = mock( AuthenticationConsumer.class ); manager.getAuthenticationPerformer( Object.class, AuthenticationConsumer.class, NoAuthenticationAuthenticationProvider.NO_AUTH_ID ).perform( consumer ); verify( consumer ).consume( noAuthenticationAuthenticationProvider ); }
@SuppressWarnings( "rawtypes" ) @Test public void testGetSupportedPerformers() throws AuthenticationConsumptionException, AuthenticationFactoryException { manager.registerConsumerClass( DelegatingNoAuthConsumer.class ); manager.registerConsumerClass( DelegatingUsernamePasswordConsumer.class ); manager.registerConsumerClass( DelegatingKerberosConsumer.class ); UsernamePasswordAuthenticationProvider usernamePasswordAuthenticationProvider = new UsernamePasswordAuthenticationProvider( "upass", "u", "pass" ); manager.registerAuthenticationProvider( usernamePasswordAuthenticationProvider ); KerberosAuthenticationProvider kerberosAuthenticationProvider = new KerberosAuthenticationProvider( "kerb", "kerb", true, "pass", true, "none" ); manager.registerAuthenticationProvider( kerberosAuthenticationProvider ); List<AuthenticationPerformer<Object, AuthenticationConsumer>> performers = manager.getSupportedAuthenticationPerformers( Object.class, AuthenticationConsumer.class ); assertEquals( 3, performers.size() ); Set<String> ids = new HashSet<String>( Arrays.asList( NoAuthenticationAuthenticationProvider.NO_AUTH_ID, usernamePasswordAuthenticationProvider.getId(), kerberosAuthenticationProvider.getId() ) ); for ( AuthenticationPerformer<Object, AuthenticationConsumer> performer : performers ) { ids.remove( performer.getAuthenticationProvider().getId() ); } assertEquals( 0, ids.size() ); }
public <ReturnType, CreateArgType, ConsumedType> void registerConsumerFactory( AuthenticationConsumerFactory<ReturnType, CreateArgType, ConsumedType> factory ) throws AuthenticationFactoryException { if ( !factory.getConsumedType().isInterface() && !AuthenticationProvider.class.isAssignableFrom( factory.getConsumedType() ) ) { throw new AuthenticationFactoryException( BaseMessages.getString( PKG, "AuthenticationManager.ConsumedTypeError", factory ) ); } Map<Class<?>, AuthenticationConsumerFactory<?, ?, ?>> createTypeMap = getRelevantConsumerFactoryMap( factory.getReturnType(), factory.getCreateArgType() ); synchronized ( createTypeMap ) { createTypeMap.put( factory.getConsumedType(), factory ); } }
@SuppressWarnings( "unchecked" ) @Override public ReturnType perform( CreateArgType consumerCreateArg ) throws AuthenticationConsumptionException { AuthenticationConsumer<ReturnType, ConsumedType> consumer = authenticationConsumerFactory.create( consumerCreateArg ); ConsumedType providerProxy = (ConsumedType) Proxy.newProxyInstance( consumer.getClass().getClassLoader(), new Class[] { authenticationConsumerFactory.getConsumedType() }, new AuthenticationConsumerInvocationHandler( provider ) ); return consumer.consume( providerProxy ); }
@SuppressWarnings( { "rawtypes", "unchecked" } ) @Override public <ReturnType, CreateArgType, ConsumedType> AuthenticationPerformer<ReturnType, CreateArgType> create( AuthenticationProvider authenticationProvider, AuthenticationConsumerFactory<ReturnType, CreateArgType, ConsumedType> authenticationConsumerFactory ) { if ( authenticationConsumerFactory.getConsumedType().isInstance( authenticationProvider ) ) { return new DefaultAuthenticationPerformer( authenticationProvider, authenticationConsumerFactory ); } else if ( AuthenticationConsumerInvocationHandler.isCompatible( authenticationConsumerFactory.getConsumedType(), authenticationProvider ) ) { return new ClassloaderBridgingAuthenticationPerformer<ReturnType, CreateArgType, ConsumedType>( authenticationProvider, authenticationConsumerFactory ); } return null; } }
@Override public <ReturnType, CreateArgType, ConsumedType> AuthenticationPerformer<ReturnType, CreateArgType> create( AuthenticationProvider authenticationProvider, AuthenticationConsumerFactory<ReturnType, CreateArgType, ConsumedType> authenticationConsumer ) { if ( AuthenticationConsumerInvocationHandler.isCompatible( authenticationConsumer.getConsumedType(), authenticationProvider ) ) { return new ClassloaderBridgingAuthenticationPerformer<ReturnType, CreateArgType, ConsumedType>( authenticationProvider, authenticationConsumer ); } return null; } } );
@Before public void setup() { manager = new AuthenticationManager(); noAuthenticationAuthenticationProvider = new NoAuthenticationAuthenticationProvider(); manager.registerAuthenticationProvider( noAuthenticationAuthenticationProvider ); }
public <ReturnType, ConsumedType> void registerConsumerClass( Class<? extends AuthenticationConsumer<? extends ReturnType, ? extends ConsumedType>> consumerClass ) throws AuthenticationFactoryException { registerConsumerFactory( new DefaultAuthenticationConsumerFactory( consumerClass ) ); }
@Override public Object consume( KerberosAuthenticationProvider authenticationProvider ) throws AuthenticationConsumptionException { return delegate.consume( authenticationProvider ); } }
@Override public int compare( AuthenticationPerformer<ReturnType, CreateArgType> o1, AuthenticationPerformer<ReturnType, CreateArgType> o2 ) { return o1.getDisplayName().toUpperCase().compareTo( o2.getDisplayName().toUpperCase() ); } } );
@SuppressWarnings( "unchecked" ) @Test public void testKerberosProviderConsumer() throws AuthenticationConsumptionException, AuthenticationFactoryException { manager.registerConsumerClass( DelegatingNoAuthConsumer.class ); manager.registerConsumerClass( DelegatingUsernamePasswordConsumer.class ); manager.registerConsumerClass( DelegatingKerberosConsumer.class ); KerberosAuthenticationProvider kerberosAuthenticationProvider = new KerberosAuthenticationProvider( "kerb", "kerb", true, "pass", true, "none" ); manager.registerAuthenticationProvider( kerberosAuthenticationProvider ); AuthenticationConsumer<Object, KerberosAuthenticationProvider> consumer = mock( AuthenticationConsumer.class ); manager.getAuthenticationPerformer( Object.class, AuthenticationConsumer.class, kerberosAuthenticationProvider.getId() ).perform( consumer ); verify( consumer ).consume( kerberosAuthenticationProvider ); }
@Override public Object consume( UsernamePasswordAuthenticationProvider authenticationProvider ) throws AuthenticationConsumptionException { return delegate.consume( authenticationProvider ); } }
@Override public String getDisplayName() { return provider.getDisplayName(); }
@SuppressWarnings( "unchecked" ) @Test public void testUsernamePasswordProviderConsumer() throws AuthenticationConsumptionException, AuthenticationFactoryException { manager.registerConsumerClass( DelegatingNoAuthConsumer.class ); manager.registerConsumerClass( DelegatingUsernamePasswordConsumer.class ); UsernamePasswordAuthenticationProvider usernamePasswordAuthenticationProvider = new UsernamePasswordAuthenticationProvider( "upass", "u", "pass" ); manager.registerAuthenticationProvider( usernamePasswordAuthenticationProvider ); AuthenticationConsumer<Object, UsernamePasswordAuthenticationProvider> consumer = mock( AuthenticationConsumer.class ); manager.getAuthenticationPerformer( Object.class, AuthenticationConsumer.class, usernamePasswordAuthenticationProvider.getId() ).perform( consumer ); verify( consumer ).consume( usernamePasswordAuthenticationProvider ); }
@Override public Object consume( KerberosAuthenticationProviderProxyInterface authenticationProvider ) throws AuthenticationConsumptionException { return delegate.consume( authenticationProvider ); } }
@Override public Object consume( NoAuthenticationAuthenticationProvider authenticationProvider ) throws AuthenticationConsumptionException { return delegate.consume( authenticationProvider ); } }