Polish CAS Authentication Builder

Issue gh-17861
This commit is contained in:
Josh Cummings 2025-09-09 14:05:34 -06:00
parent dd50dc0c40
commit 18fbf88993
3 changed files with 20 additions and 7 deletions

View File

@ -23,7 +23,6 @@ import org.apereo.cas.client.validation.Assertion;
import org.jspecify.annotations.Nullable;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;
@ -183,7 +182,7 @@ public class CasAuthenticationToken extends AbstractAuthenticationToken implemen
}
/**
* A builder preserving the concrete {@link Authentication} type
* A builder of {@link CasAuthenticationToken} instances
*
* @since 7.0
*/
@ -208,8 +207,13 @@ public class CasAuthenticationToken extends AbstractAuthenticationToken implemen
this.assertion = token.assertion;
}
public B keyHash(Integer keyHash) {
this.keyHash = keyHash;
/**
* Use this key
* @param key the key to use
* @return the {@link Builder} for further configurations
*/
public B key(String key) {
this.keyHash = key.hashCode();
return (B) this;
}
@ -227,11 +231,21 @@ public class CasAuthenticationToken extends AbstractAuthenticationToken implemen
return (B) this;
}
/**
* Use this {@link UserDetails}
* @param userDetails the {@link UserDetails} to use
* @return the {@link Builder} for further configurations
*/
public B userDetails(UserDetails userDetails) {
this.userDetails = userDetails;
return (B) this;
}
/**
* Use this {@link Assertion}
* @param assertion the {@link Assertion} to use
* @return the {@link Builder} for further configurations
*/
public B assertion(Assertion assertion) {
this.assertion = assertion;
return (B) this;

View File

@ -22,7 +22,6 @@ import java.util.Collection;
import org.jspecify.annotations.Nullable;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;
@ -122,7 +121,7 @@ public class CasServiceTicketAuthenticationToken extends AbstractAuthenticationT
}
/**
* A builder preserving the concrete {@link Authentication} type
* A builder of {@link CasServiceTicketAuthenticationToken} instances
*
* @since 7.0
*/

View File

@ -167,7 +167,7 @@ public class CasAuthenticationTokenTests {
AuthorityUtils.createAuthorityList("FACTOR_TWO"), PasswordEncodedUser.admin(), assertionTwo);
CasAuthenticationToken authentication = factorOne.toBuilder()
.authorities((a) -> a.addAll(factorTwo.getAuthorities()))
.keyHash(factorTwo.getKeyHash())
.key("yek")
.principal(factorTwo.getPrincipal())
.credentials(factorTwo.getCredentials())
.userDetails(factorTwo.getUserDetails())