Skip to content

Commit

Permalink
#378 - AuthenticationManagement now allows to update the authentication.
Browse files Browse the repository at this point in the history
Introduced AuthenticationManagement.updateAuthentication(…) so that changes to the role arrangement of a user can be made effective immediately. The implementation updates SpringSecurity's Authentication to the UserAccount given.
  • Loading branch information
odrotbohm committed Dec 6, 2021
1 parent f2cebf3 commit b97514c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 the original author or authors.
* Copyright 2017-2021 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.
Expand Down Expand Up @@ -45,4 +45,12 @@ public interface AuthenticationManagement {
* @return
*/
boolean matches(UnencryptedPassword candidate, EncryptedPassword existing);

/**
* Updates the current authentication to the given {@link UserAccount}.
*
* @param account must not be {@literal null}.
* @since 7.5
*/
void updateAuthentication(UserAccount account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.salespointframework.useraccount.Password.EncryptedPassword;
import org.salespointframework.useraccount.Password.UnencryptedPassword;
import org.salespointframework.useraccount.UserAccount.UserAccountIdentifier;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand Down Expand Up @@ -79,6 +80,19 @@ public boolean matches(UnencryptedPassword candidate, EncryptedPassword existing
orElse(false);
}

/*
* (non-Javadoc)
* @see org.salespointframework.useraccount.AuthenticationManagement#updateAuthentication(org.salespointframework.useraccount.UserAccount)
*/
@Override
public void updateAuthentication(UserAccount account) {

var details = new UserAccountDetails(account);
var token = new UsernamePasswordAuthenticationToken(details, details.getPassword(), details.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(token);
}

/*
* (non-Javadoc)
* @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ void exposesRolesAsRoleUnderscorePrefixedAuthorities() {
.allMatch(it -> it.getAuthority().startsWith("ROLE_"));
}

@Test // #379
void updatesAuthentication() {

assertThat(authenticationManager.getCurrentUser()).isEmpty();

var identifier = UserAccountIdentifier.of("4711");
var account = new UserAccount(identifier, EncryptedPassword.of("encrypted"), Role.of("ADMIN"));

doReturn(Optional.of(account)).when(repository).findById(identifier);

authenticationManager.updateAuthentication(account);

assertThat(authenticationManager.getCurrentUser()).hasValue(account);
}

private static void authenticate(UserAccount account) {

UserAccountDetails accountDetails = new UserAccountDetails(account);
Expand Down

0 comments on commit b97514c

Please sign in to comment.