Not able to fetch client certificate in AuthenticationFetcher micronaut

Viewed 438

I am trying to fetch client certificate in AuthenticationFetcher implementation to have custom authorization implementation.

@Singleton
public class SslAuthenticationFetcher implements AuthenticationFetcher {
    private static final Logger LOGGER = LoggerFactory.getLogger(SslAuthenticationFetcher.class);
    private static final String AUTHENTICATION = "AUTHENTICATION";

    private final SessionStore<? extends Session> sessionStore;
    private final X509CertificateParser x509CertificateParser;

    @Inject
    public SslAuthenticationFetcher(X509CertificateParser x509CertificateParser,
                                    SessionStore<? extends Session> sessionStore) {
        this.x509CertificateParser = x509CertificateParser;
        this.sessionStore = sessionStore;
    }

    @Override
    public Publisher<Authentication> fetchAuthentication(HttpRequest<?> request) {
        
        // Code to check if user-session already exist
        
        UserDetails userDetails = getAuthentication(request);
        session.put(AUTHENTICATION, userDetails);    

        return Flowable.just(userDetails);
    }

    @NotNull
    private UserDetails getAuthentication(HttpRequest<?> request) {
        Optional<Certificate> certificate = request.getCertificate(); // This always returns empty
        if (!certificate.isPresent()) {
            throw new AuthenticationException("No certificate chain found");
        }
        
        // Code to parse the certificate to create UserDetails
    }
}

I have an integration test which triggers GET request to /hello endpoint

@MicronautTest
class HelloControllerTest {

    @Inject
    @Client("/")
    private HttpClient httpClient;

    @Test
    void testHelloEndpoint(){

        HttpResponse<String> response = httpClient.toBlocking()
                .exchange(HttpRequest.GET("/hello"), String.class);

        assertEquals("Hello", response.body());
    }
}

Below are my client ssl configuration in application-test.yml file

micronaut:
  http:
    client:
      ssl:
        enabled: true
        client-authentication: need
        key-store:
          path: <key-store file path>.p12
          password: <key-store file password>
          type: PKCS12
        trust-store:
          path: <trust-store file path>.jks
          password: <trust-store file password>
          type: JKS
        protocol: TLS

The issue is, in SslAuthenticationFetcher when I try to fetch certificate from request it always returns an Optional.empty. What could be the possible issue here?

  • Micronaut version: 2.0.0
  • Java version: 1.8
0 Answers
Related