Skip to content

Commit

Permalink
Add logging to GetAuthenticatedUserId for debugging
Browse files Browse the repository at this point in the history
#deploy-idporten-frontend

Added log statements to provide more context about the authentication object and token attributes during execution. This will help in identifying potential issues and understanding how authentication data is being processed. Also updated the default return value for unsupported contexts to an empty string instead of null.
  • Loading branch information
krharum committed Dec 17, 2024
1 parent d7b1a9c commit 0d2d46f
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.testnav.libs.reactivesecurity.action;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
Expand All @@ -11,6 +12,7 @@

import java.util.concurrent.Callable;

@Slf4j
@Component
@RequiredArgsConstructor
public class GetAuthenticatedUserId implements Callable<Mono<String>> {
Expand All @@ -37,14 +39,16 @@ private Mono<String> getTokenAttribute(String attribute) {

private String getTokenAttribute(Authentication context, String attribute) {

log.info("context.authentication {}", context);
if (context instanceof JwtAuthenticationToken jwtAuthenticationToken) {
return jwtAuthenticationToken.getTokenAttributes().get(attribute).toString();

} else if (context instanceof OAuth2AuthenticationToken oauth2AuthenticationToken) {
log.info("oauth2AuthenticationToken {}", oauth2AuthenticationToken.getPrincipal());
return oauth2AuthenticationToken.getPrincipal().getAttributes().get(attribute).toString();

} else {
return null;
return "";
}
}
}

0 comments on commit 0d2d46f

Please sign in to comment.