Skip to content

Commit

Permalink
Added current username to EncryptionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
agrancaric committed Apr 15, 2024
1 parent 71e0bc2 commit ce6913f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public class EncryptionContext {
*/
private final List<Object> methodDecryptedArguments;

/**
* Current username or null if none exist
*/
private final String currentUsername;

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

@RequiredArgsConstructor
Expand Down Expand Up @@ -98,11 +102,20 @@ private DecryptArgument decryptArgumentAnnotation(Annotation[] annotationList) {
private EncryptionContext createEncryptionContext(Signature signature, Object[] arguments) {
List<Object> argumentList = Arrays.asList(arguments);
String methodName = String.format(EncryptConstants.METHOD_NAME_FORMAT, signature.getDeclaringType().getName(), signature.getName());
String currentUsername = currentUsername();

return EncryptionContext.builder()
.fullyQualifiedMethodName(methodName)
.methodArguments(argumentList)
.methodDecryptedArguments(argumentList)
.currentUsername(currentUsername)
.build();
}

private String currentUsername() {
return Optional.ofNullable(SecurityContextHolder.getContext())
.map(SecurityContext::getAuthentication)
.map(Authentication::getName)
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -114,11 +117,20 @@ private EncryptionConfiguration findEncryptionConfigurationForOperation(List<Enc

private EncryptionContext createEncryptionContext(String methodName, Object[] arguments) {
List<Object> argumentList = Arrays.asList(arguments);
String currentUsername = currentUsername();

return EncryptionContext.builder()
.fullyQualifiedMethodName(methodName)
.methodArguments(argumentList)
.methodDecryptedArguments(argumentList)
.currentUsername(currentUsername)
.build();
}

private String currentUsername() {
return Optional.ofNullable(SecurityContextHolder.getContext())
.map(SecurityContext::getAuthentication)
.map(Authentication::getName)
.orElse(null);
}
}

0 comments on commit ce6913f

Please sign in to comment.