diff --git a/apiml-security-common/src/main/java/org/zowe/apiml/security/common/content/AbstractSecureContentFilter.java b/apiml-security-common/src/main/java/org/zowe/apiml/security/common/content/AbstractSecureContentFilter.java index cc71b5b821..8f8ec5763b 100644 --- a/apiml-security-common/src/main/java/org/zowe/apiml/security/common/content/AbstractSecureContentFilter.java +++ b/apiml-security-common/src/main/java/org/zowe/apiml/security/common/content/AbstractSecureContentFilter.java @@ -72,8 +72,9 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht Optional authenticationToken = extractContent(request); if (authenticationToken.isPresent()) { + Authentication authentication = null; try { - Authentication authentication = authenticationManager.authenticate(authenticationToken.get()); + authentication = authenticationManager.authenticate(authenticationToken.get()); SecurityContextHolder.getContext().setAuthentication(authentication); filterChain.doFilter(request, response); } catch (AuthenticationException authenticationException) { @@ -81,9 +82,9 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht } catch (RuntimeException e) { resourceAccessExceptionHandler.handleException(request, response, e); } finally { - Authentication authentication = authenticationToken.get(); + // TODO: remove once fixed directly in Spring - org.springframework.security.core.CredentialsContainer#eraseCredentials if (authentication != null) { - Object credentials = authenticationToken.get().getCredentials(); + Object credentials = authentication.getCredentials(); if (credentials instanceof char[]) { Arrays.fill((char[]) credentials, (char) 0); } diff --git a/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/login/GatewayLoginProvider.java b/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/login/GatewayLoginProvider.java index 8f4ce31530..66f450e26e 100644 --- a/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/login/GatewayLoginProvider.java +++ b/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/login/GatewayLoginProvider.java @@ -20,6 +20,7 @@ import org.zowe.apiml.security.common.login.LoginRequest; import org.zowe.apiml.security.common.token.TokenAuthentication; +import java.util.Arrays; import java.util.Optional; import static org.zowe.apiml.security.SecurityUtils.readPassword; @@ -41,26 +42,34 @@ public class GatewayLoginProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { String username = authentication.getPrincipal().toString(); - char[] password; + char[] password = null; char[] newPassword = null; - if (authentication.getCredentials() instanceof LoginRequest) { - LoginRequest credentials = (LoginRequest) authentication.getCredentials(); - password = credentials.getPassword(); - newPassword = LoginRequest.getNewPassword(authentication); - } else { - password = readPassword(authentication.getCredentials()); - } + boolean cleanup = false; + try { + if (authentication.getCredentials() instanceof LoginRequest) { + LoginRequest credentials = (LoginRequest) authentication.getCredentials(); + password = credentials.getPassword(); + newPassword = LoginRequest.getNewPassword(authentication); + } else { + password = readPassword(authentication.getCredentials()); + cleanup = !(authentication.getCredentials() instanceof char[]); + } - Optional token = gatewaySecurityService.login(username, password, newPassword); + Optional token = gatewaySecurityService.login(username, password, newPassword); - if (!token.isPresent()) { - throw new BadCredentialsException("Invalid Credentials"); - } + if (!token.isPresent()) { + throw new BadCredentialsException("Invalid Credentials"); + } - TokenAuthentication tokenAuthentication = new TokenAuthentication(username, token.get()); - tokenAuthentication.setAuthenticated(true); + TokenAuthentication tokenAuthentication = new TokenAuthentication(username, token.get()); + tokenAuthentication.setAuthenticated(true); - return tokenAuthentication; + return tokenAuthentication; + } finally { + if (cleanup) { + Arrays.fill(password, (char) 0); + } + } } @Override diff --git a/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/service/GatewaySecurityService.java b/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/service/GatewaySecurityService.java index 68c4d6e7d0..18b58e8b0b 100644 --- a/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/service/GatewaySecurityService.java +++ b/security-service-client-spring/src/main/java/org/zowe/apiml/security/client/service/GatewaySecurityService.java @@ -88,6 +88,7 @@ public Optional login(String username, char[] password, char[] newPasswo } catch (IOException e) { responseHandler.handleException(e); } finally { + // TODO: remove once fixed directly in Spring - org.springframework.security.core.CredentialsContainer#eraseCredentials loginRequest.evictSensitiveData(); } return Optional.empty();