io.quarkus.security.webauthn.WebAuthnIdentityProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-security-webauthn Show documentation
Show all versions of quarkus-security-webauthn Show documentation
Secure your applications with WebAuthn
package io.quarkus.security.webauthn;
import java.util.function.Consumer;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import io.quarkus.security.identity.AuthenticationRequestContext;
import io.quarkus.security.identity.IdentityProvider;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.security.runtime.QuarkusPrincipal;
import io.quarkus.security.runtime.QuarkusSecurityIdentity;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.subscription.UniEmitter;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.ext.auth.User;
/**
* WebAuthn IdentityProvider
*/
@ApplicationScoped
public class WebAuthnIdentityProvider implements IdentityProvider {
@Inject
WebAuthnSecurity security;
@Override
public Class getRequestType() {
return WebAuthnAuthenticationRequest.class;
}
@Override
public Uni authenticate(WebAuthnAuthenticationRequest request, AuthenticationRequestContext context) {
return Uni.createFrom().emitter(new Consumer>() {
@Override
public void accept(UniEmitter super SecurityIdentity> emitter) {
security.getWebAuthn().authenticate(request.getCredentials(), new Handler>() {
@Override
public void handle(AsyncResult event) {
if (event.failed()) {
emitter.fail(event.cause());
} else {
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder();
// only the username matters, because when we auth we create a session cookie with it
// and we reply instantly so the roles are never used
builder.setPrincipal(new QuarkusPrincipal(request.getCredentials().getUsername()));
emitter.complete(builder.build());
}
}
});
}
});
}
}