All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.security.webauthn.WebAuthnIdentityProvider Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
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 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());
                        }
                    }
                });
            }
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy