Is there any way in Micronaut, where I can do an authentication to the JWT authentication provider ? #716
Replies: 2 comments 6 replies
-
@dileepnaik I move this to a discussion. I don't think it is an issue. Sorry but I don't fully understand your question. Typically, in Micronaut Security when you receive a request with a JWT Bearer token we validate the signature of the JWT and the claims. We don't do an extra network call against a central service. Is that what you want to achieve? You can always create your own implementation of I explain here the security flow: https://youtu.be/VrnVbAyKSEY?t=336 |
Beta Was this translation helpful? Give feedback.
-
Hi, I have a similar requirement, I am trying to authenticate a Firebase user. I have retrieved the JWT token of the user and checked that it is indeed a valid JWT token. I use the following class: package example.app;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseToken;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.Environment;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.token.validator.TokenValidator;
import jakarta.inject.Singleton;
import java.net.http.HttpRequest;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
@Singleton
//@Requires(notEnv = {Environment.GOOGLE_COMPUTE, Environment.CLOUD})
public class FirebaseTokenValidator implements TokenValidator<HttpRequest> {
public FirebaseTokenValidator() {
FirebaseApp.initializeApp();
}
@Override
public @NonNull Publisher<Authentication> validateToken(
@NonNull String token,
@Nullable HttpRequest request
) {
if (token.isEmpty()) {
return Mono.empty();
}
try {
FirebaseToken firebaseToken = FirebaseAuth.getInstance().verifyIdToken(token);
return Mono.just(Authentication.build(
firebaseToken.getUid(),
firebaseToken.getClaims()
));
} catch (FirebaseAuthException e) {
return Mono.error(e);
}
}
} I have enabled JWT:
However, when I call my service with an authentication header (Authorization), the custom token validator isn't even instantiated (I know this from step debugging). I have enabled the trace for security, and indeed it looks like this:
The request falls straight through the AbstractSecurityRule without ever touching the TokenValidator. This is on Micronaut |
Beta Was this translation helpful? Give feedback.
-
I can Only see the documentation for the authentication mechanism in the same server.
But in my case, I have the JWT auth provider, and the clients will directly call and get the jwt token.
Clients, While sending the request it includes the bearer token, but I have to go and validate against the Auth provider. Is there any way in micronaut I can override the behaviour and make a call to AuthProvider and validate the JWT token ?
Beta Was this translation helpful? Give feedback.
All reactions