Permalink
Browse files

[cfid-669] Add UsernamePasswordExtractingAuthenticationManager

[#45987701] Login server changes to support vmc and LDAP
  • Loading branch information...
1 parent e493ba1 commit d0b830e2d00a098b89153fd64b5ed9c9245c992a @dsyer committed Mar 11, 2013
View
60 .../org/cloudfoundry/identity/uaa/login/UsernamePasswordExtractingAuthenticationManager.java
@@ -0,0 +1,60 @@
+/*
+ * Cloud Foundry 2012.02.03 Beta
+ * Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0 (the "License").
+ * You may not use this product except in compliance with the License.
+ *
+ * This product includes a number of subcomponents with
+ * separate copyright notices and license terms. Your use of these
+ * subcomponents is subject to the terms and conditions of the
+ * subcomponent's license, as noted in the LICENSE file.
+ */
+
+package org.cloudfoundry.identity.uaa.login;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.AuthenticationException;
+
+/**
+ * Authentication filter translating a generic Authentication into a UsernamePasswordAuthenticationToken.
+ *
+ * @author Dave Syer
+ *
+ */
+public class UsernamePasswordExtractingAuthenticationManager implements AuthenticationManager {
+
+ protected final Log logger = LogFactory.getLog(getClass());
+
+ private final AuthenticationManager delegate;
+
+ /**
+ * @param delegate
+ */
+ public UsernamePasswordExtractingAuthenticationManager(AuthenticationManager delegate) {
+ super();
+ this.delegate = delegate;
+ }
+
+ /* (non-Javadoc)
+ * @see org.springframework.security.authentication.AuthenticationManager#authenticate(org.springframework.security.core.Authentication)
+ */
+ @Override
+ public Authentication authenticate(Authentication authentication) throws AuthenticationException {
+
+ if (authentication == null || authentication instanceof UsernamePasswordAuthenticationToken) {
+ return authentication;
+ }
+
+ UsernamePasswordAuthenticationToken output = new UsernamePasswordAuthenticationToken(authentication, authentication.getCredentials(), authentication.getAuthorities());
+ output.setAuthenticated(authentication.isAuthenticated());
+ output.setDetails(authentication.getDetails());
+ return delegate.authenticate(output);
+
+ }
+
+}
View
55 ...cloudfoundry/identity/uaa/login/UsernamePasswordExtractingAuthenticationManagerTests.java
@@ -0,0 +1,55 @@
+/*
+ * Cloud Foundry 2012.02.03 Beta
+ * Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
+ *
+ * This product is licensed to you under the Apache License, Version 2.0 (the "License").
+ * You may not use this product except in compliance with the License.
+ *
+ * This product includes a number of subcomponents with
+ * separate copyright notices and license terms. Your use of these
+ * subcomponents is subject to the terms and conditions of the
+ * subcomponent's license, as noted in the LICENSE file.
+ */
+
+package org.cloudfoundry.identity.uaa.login;
+
+import static org.junit.Assert.assertSame;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.TestingAuthenticationToken;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.authority.AuthorityUtils;
+
+/**
+ * @author Dave Syer
+ *
+ */
+public class UsernamePasswordExtractingAuthenticationManagerTests {
+
+ private AuthenticationManager delegate = Mockito.mock(AuthenticationManager.class);
+
+ private UsernamePasswordExtractingAuthenticationManager manager = new UsernamePasswordExtractingAuthenticationManager(
+ delegate);
+
+ @Test
+ public void testAuthenticate() {
+ Authentication expected = new TestingAuthenticationToken("bar", "foo",
+ AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
+ Mockito.when(delegate.authenticate(Mockito.any(UsernamePasswordAuthenticationToken.class)))
+ .thenReturn(expected);
+ Authentication output = manager.authenticate(new TestingAuthenticationToken("foo", "bar"));
+ assertSame(expected, output);
+ }
+
+ @Test
+ public void testUsernamePassword() {
+ Authentication expected = new UsernamePasswordAuthenticationToken("bar", "foo",
+ AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
+ Authentication output = manager.authenticate(expected);
+ assertSame(expected, output);
+ }
+
+}

0 comments on commit d0b830e

Please sign in to comment.