Skip to content

Conversation

jzheaux
Copy link
Contributor

@jzheaux jzheaux commented Aug 21, 2025

Applications can use AuthenticationBuilder to apply existing authentications to new ones.

For example, if the current logged in user is represented by:

Authentication firstFactor = ...

And they provide a second set of authenticated credentials, represented by:

Authentication secondFactor = ...

Then the first factor can be applied to the second factor as follows:

secondFactor = secondFactor.toBuilder().apply(firstFactor).build();

This draft PR adds a basic builder to each Authentication implementation that implements Authentication.Builder. In order to simplify upgrades, toBuilder by default returns a no-op implementation of Authentication.Builder that ultimately returns the same authentication unchanged.

@jzheaux jzheaux changed the title Authentication Builder Propagate Authorities From Previous Factors Aug 21, 2025
@jzheaux jzheaux force-pushed the authentication-builder branch 5 times, most recently from 4f62b6b to 6eb00d0 Compare August 22, 2025 21:48
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.
This commit allows looking up the current authentication and applying
it to the latest authentication. This is specifically handy when
collecting authorities gained from each authentication factor.
This commit provides the SecurityContextHolderStrategy bean to
ProviderManager instances that the HttpSecurity DSL constructs.
@jzheaux jzheaux force-pushed the authentication-builder branch from 6eb00d0 to b48b10a Compare August 22, 2025 22:25
Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @jzheaux! I've provided feedback inline, but my main pieces of feedback are:

  • The AuthenticationManager should not be accessing the SecurityContext. Instead, we should have the controller (e.g. Filter) that invokes the AuthenticationManager perform the merging of the two Authentication instances.
  • I think that the builder APIs should function independently of MFA and should work for any properties on the Authentication. Doing this would also allow deprecation of the setAuthenticated method.
  • I don't think we should have an Authentication.apply(Authentication) method. Especially so if it is only applying the authorities and ignoring many other properties that are on the Authentication object.


@Override
public B apply(Authentication token) {
Assert.isTrue(token.isAuthenticated(), "cannot mutate an unauthenticated token");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't an unauthenticated user be mutated? I believe that the builders should be independent of the MFA feature. If we keep this assertion, it would be good to include the why in the error message.

@Override
public B apply(Authentication token) {
Assert.isTrue(token.isAuthenticated(), "cannot mutate an unauthenticated token");
Assert.notNull(token.getPrincipal(), "principal cannot be null");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't a principal be null? If we keep this assertion, it would be good to include the why in the error message.

@@ -185,4 +187,36 @@ public String toString() {
return sb.toString();
}

protected abstract static class AbstractAuthenticationBuilder<A extends Authentication, B extends AbstractAuthenticationBuilder<A, B>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect that this builder's properties aligns with the properties available on AbstractAuthentication.

}

@Override
public B apply(Authentication token) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the comment about only having an authorities member, it is surprising that an Authentication is passed in here, but only the authorities are used from it. There are quite a few other properties that I think should be taken into account.

return (B) this;
}

protected abstract A build(Collection<GrantedAuthority> authorities);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that I'd prefer to make authorities protected and not pass anything into this method. This gives us flexibility of adding additional properties later.

*
* @since 7.0
*/
public static final class Builder extends AbstractAuthenticationBuilder<@NonNull CasAuthenticationToken, Builder> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to explicitly declare @NonNull as this is the default

}

@Override
protected @NonNull CasAuthenticationToken build(Collection<GrantedAuthority> authorities) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to explicitly declare @NonNull as this is the default

if (!current.isAuthenticated()) {
return result;
}
return doAuthenticate(current).map((r) -> r.toBuilder().apply(current).build());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as in the ProviderManager, the combining of these should not be done in the ReactiveAuthenticationManager.

https://github.com/spring-projects/spring-security/pull/17790/files#diff-84d496ceee0087c25a0972331a1e127cc87ca0310c854c76b43e083304e83cb2R274

@@ -54,6 +57,9 @@
*/
public interface Authentication extends Principal, Serializable {

@Serial
long serialVersionUID = -3884394378624019849L;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me to understand why this is specified?

* @since 7.0
*/
public static final class Builder
extends AbstractAuthenticationBuilder<@NonNull Saml2AssertionAuthentication, @NonNull Builder> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to explicitly declare @NonNull as this is the default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants