diff --git a/src/main/java/com/ysoft/geecon/webauthn/MyWebAuthnSetup.java b/src/main/java/com/ysoft/geecon/webauthn/WebAuthnSetup.java similarity index 67% rename from src/main/java/com/ysoft/geecon/webauthn/MyWebAuthnSetup.java rename to src/main/java/com/ysoft/geecon/webauthn/WebAuthnSetup.java index 0c14bb2..86d817b 100644 --- a/src/main/java/com/ysoft/geecon/webauthn/MyWebAuthnSetup.java +++ b/src/main/java/com/ysoft/geecon/webauthn/WebAuthnSetup.java @@ -1,28 +1,35 @@ package com.ysoft.geecon.webauthn; import com.ysoft.geecon.dto.User; +import com.ysoft.geecon.error.OAuthApiException; import com.ysoft.geecon.repo.UsersRepo; +import io.quarkus.runtime.StartupEvent; import io.quarkus.security.webauthn.WebAuthnUserProvider; import io.smallrye.mutiny.Uni; +import io.vertx.core.json.Json; import io.vertx.ext.auth.webauthn.AttestationCertificates; import io.vertx.ext.auth.webauthn.Authenticator; +import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; import java.util.List; +import java.util.Optional; @ApplicationScoped -public class MyWebAuthnSetup implements WebAuthnUserProvider { - public static final String AUTHORIZED_USER = MyWebAuthnSetup.class.getPackageName() + "#AUTHORIZED_USER"; +public class WebAuthnSetup implements WebAuthnUserProvider { @Inject UsersRepo usersRepo; - @Inject - RoutingContext routingContext; - private static List toAuthenticators(List dbs) { - return dbs.stream().map(MyWebAuthnSetup::toAuthenticator).toList(); + return dbs.stream().map(WebAuthnSetup::toAuthenticator).toList(); + } + + private static Uni> loadCredentials(Optional user) { + var authenticators = user.map(u -> toAuthenticators(u.credentials())).filter(l -> !l.isEmpty()); + return Uni.createFrom().item(authenticators.orElse(List.of())); } private static Authenticator toAuthenticator(WebAuthnCredential credential) { @@ -41,20 +48,30 @@ public class MyWebAuthnSetup implements WebAuthnUserProvider { return ret; } + public void init(@Observes StartupEvent e, Router router) { + router.route("/q/webauthn/*").failureHandler((RoutingContext context) -> { + if (context.failure() instanceof OAuthApiException exception) { + context.response() + .setStatusMessage("Forbidden") + .setStatusCode(403) + .end(Json.encodePrettily(exception.getResponse().getEntity())); + } else { + context.response() + .setStatusMessage("Internal Error") + .setStatusCode(500) + .end(context.failure().getMessage()); + } + }); + } + @Override public Uni> findWebAuthnCredentialsByUserName(String userName) { - return Uni.createFrom().item(usersRepo.getUser(userName) - .map((User dbs) -> toAuthenticators(dbs.credentials())) - .orElse(List.of()) - ); + return loadCredentials(usersRepo.getUser(userName)); } @Override public Uni> findWebAuthnCredentialsByCredID(String credID) { - return Uni.createFrom().item(usersRepo.findByCredID(credID) - .map((User dbs) -> toAuthenticators(dbs.credentials())) - .orElse(List.of()) - ); + return loadCredentials(usersRepo.findByCredID(credID)); } @Override diff --git a/src/main/resources/META-INF/resources/js/webauthn-debug.js b/src/main/resources/META-INF/resources/js/webauthn-debug.js index 1a7428b..0fed198 100644 --- a/src/main/resources/META-INF/resources/js/webauthn-debug.js +++ b/src/main/resources/META-INF/resources/js/webauthn-debug.js @@ -126,7 +126,9 @@ if (res.status >= 200 && res.status < 300) { return res; } - throw new Error(res.statusText); + return res.text().then(body => { + throw new Error(body); + }); }) .then(res => res.json()) .then(resp => this.debuggingFunction(stage + "-response", resp));