Browse Source
This commit adds a new default method to Authentication for the purposes of creating a Builder based on the current authentication, allowing other authentications to be applied to it as a composite. It also adds Builders for each one of the authentication result classes. Issue gh-17861pull/17790/head
27 changed files with 1016 additions and 1 deletions
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
/* |
||||
* Copyright 2004-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.core; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.function.Consumer; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* An adapter implementation of {@link Authentication.Builder} that provides a no-op |
||||
* implementation for the principal, credentials, and authorities |
||||
* |
||||
* @param <A> the type of {@link Authentication} |
||||
* @author Josh Cummings |
||||
* @since 7.0 |
||||
*/ |
||||
class NoopAuthenticationBuilder<A extends Authentication> |
||||
implements Authentication.Builder<A, NoopAuthenticationBuilder<A>> { |
||||
|
||||
private A original; |
||||
|
||||
NoopAuthenticationBuilder(A authentication) { |
||||
Assert.isTrue(authentication.isAuthenticated(), "cannot mutate an unauthenticated token"); |
||||
Assert.notNull(authentication.getPrincipal(), "principal cannot be null"); |
||||
this.original = authentication; |
||||
} |
||||
|
||||
@Override |
||||
public NoopAuthenticationBuilder<A> authorities(Consumer<Collection<GrantedAuthority>> authorities) { |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
public A build() { |
||||
return this.original; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright 2004-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.authentication; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.security.authentication.AbstractAuthenticationToken.AbstractAuthenticationBuilder; |
||||
import org.springframework.security.core.Authentication; |
||||
import org.springframework.security.core.GrantedAuthority; |
||||
import org.springframework.security.core.authority.AuthorityUtils; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
||||
|
||||
class AbstractAuthenticationBuilderTests { |
||||
|
||||
@Test |
||||
void applyWhenUnauthenticatedThenErrors() { |
||||
TestAbstractAuthenticationBuilder builder = new TestAbstractAuthenticationBuilder(); |
||||
TestingAuthenticationToken unauthenticated = new TestingAuthenticationToken("user", "password"); |
||||
assertThatIllegalArgumentException().isThrownBy(() -> builder.apply(unauthenticated)); |
||||
} |
||||
|
||||
@Test |
||||
void applyWhenAuthoritiesThenAdds() { |
||||
TestAbstractAuthenticationBuilder builder = new TestAbstractAuthenticationBuilder(); |
||||
TestingAuthenticationToken factorOne = new TestingAuthenticationToken("user", "pass", "FACTOR_ONE"); |
||||
TestingAuthenticationToken factorTwo = new TestingAuthenticationToken("user", "pass", "FACTOR_TWO"); |
||||
Authentication result = builder.apply(factorOne).apply(factorTwo).build(); |
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities()); |
||||
assertThat(authorities).containsExactlyInAnyOrder("FACTOR_ONE", "FACTOR_TWO"); |
||||
} |
||||
|
||||
private static final class TestAbstractAuthenticationBuilder |
||||
extends AbstractAuthenticationBuilder<Authentication, TestAbstractAuthenticationBuilder> { |
||||
|
||||
@Override |
||||
protected Authentication build(Collection<GrantedAuthority> authorities) { |
||||
return new TestingAuthenticationToken("user", "password", authorities); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright 2004-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.authentication.jaas; |
||||
|
||||
import java.util.Set; |
||||
|
||||
import javax.security.auth.login.LoginContext; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.security.core.authority.AuthorityUtils; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
class JaasAuthenticationTokenTests { |
||||
|
||||
@Test |
||||
void toBuilderWhenApplyThenCopies() { |
||||
JaasAuthenticationToken factorOne = new JaasAuthenticationToken("alice", "pass", |
||||
AuthorityUtils.createAuthorityList("FACTOR_ONE"), mock(LoginContext.class)); |
||||
JaasAuthenticationToken factorTwo = new JaasAuthenticationToken("bob", "ssap", |
||||
AuthorityUtils.createAuthorityList("FACTOR_TWO"), mock(LoginContext.class)); |
||||
JaasAuthenticationToken result = factorOne.toBuilder().apply(factorTwo).build(); |
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities()); |
||||
assertThat(result.getPrincipal()).isSameAs(factorTwo.getPrincipal()); |
||||
assertThat(result.getCredentials()).isSameAs(factorTwo.getCredentials()); |
||||
assertThat(result.getLoginContext()).isSameAs(factorTwo.getLoginContext()); |
||||
assertThat(authorities).containsExactlyInAnyOrder("FACTOR_ONE", "FACTOR_TWO"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2004-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.authentication.ott; |
||||
|
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.security.core.authority.AuthorityUtils; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
class OneTimeTokenAuthenticationTests { |
||||
|
||||
@Test |
||||
void toBuilderWhenApplyThenCopies() { |
||||
OneTimeTokenAuthentication factorOne = new OneTimeTokenAuthentication("alice", |
||||
AuthorityUtils.createAuthorityList("FACTOR_ONE")); |
||||
OneTimeTokenAuthentication factorTwo = new OneTimeTokenAuthentication("bob", |
||||
AuthorityUtils.createAuthorityList("FACTOR_TWO")); |
||||
OneTimeTokenAuthentication result = factorOne.toBuilder().apply(factorTwo).build(); |
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities()); |
||||
assertThat(result.getPrincipal()).isSameAs(factorTwo.getPrincipal()); |
||||
assertThat(authorities).containsExactlyInAnyOrder("FACTOR_ONE", "FACTOR_TWO"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* Copyright 2004-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.saml2.provider.service.authentication; |
||||
|
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.security.core.authority.AuthorityUtils; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
class Saml2AssertionAuthenticationTests { |
||||
|
||||
@Test |
||||
void toBuilderWhenApplyThenCopies() { |
||||
Saml2ResponseAssertion.Builder prototype = Saml2ResponseAssertion.withResponseValue("response"); |
||||
Saml2AssertionAuthentication factorOne = new Saml2AssertionAuthentication("alice", |
||||
prototype.nameId("alice").build(), AuthorityUtils.createAuthorityList("FACTOR_ONE"), "alice"); |
||||
Saml2AssertionAuthentication factorTwo = new Saml2AssertionAuthentication("bob", |
||||
prototype.nameId("alice").build(), AuthorityUtils.createAuthorityList("FACTOR_TWO"), "bob"); |
||||
Saml2AssertionAuthentication result = factorOne.toBuilder().apply(factorTwo).build(); |
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(result.getAuthorities()); |
||||
assertThat(result.getPrincipal()).isSameAs(factorTwo.getPrincipal()); |
||||
assertThat(result.getCredentials()).isSameAs(factorTwo.getCredentials()); |
||||
assertThat(result.getRelyingPartyRegistrationId()).isSameAs(factorTwo.getRelyingPartyRegistrationId()); |
||||
assertThat(authorities).containsExactlyInAnyOrder("FACTOR_ONE", "FACTOR_TWO"); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue