diff --git a/src/main/java/eu/m724/talkpages/auth/MyHttpAuthenticationMechanism.java b/src/main/java/eu/m724/talkpages/auth/MyHttpAuthenticationMechanism.java index 69547be..10baf8d 100644 --- a/src/main/java/eu/m724/talkpages/auth/MyHttpAuthenticationMechanism.java +++ b/src/main/java/eu/m724/talkpages/auth/MyHttpAuthenticationMechanism.java @@ -17,6 +17,8 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Alternative; import jakarta.inject.Inject; +import java.util.function.Supplier; + @Alternative @Priority(1) @ApplicationScoped @@ -26,7 +28,7 @@ public class MyHttpAuthenticationMechanism implements HttpAuthenticationMechanis @Override public Uni authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) { // TODO handle ip accounts - return Uni.createFrom().item(() -> { + return Uni.createFrom().item((Supplier) () -> { Cookie cookie = context.request().getCookie("session-token"); if (cookie != null) { @@ -36,17 +38,18 @@ public class MyHttpAuthenticationMechanism implements HttpAuthenticationMechanis if (session != null) { Account account = session.getAccount(); - QuarkusSecurityIdentity identity = QuarkusSecurityIdentity.builder() + return QuarkusSecurityIdentity.builder() .setPrincipal(new QuarkusPrincipal(account.getName())) .addRoles(account.getRoles()) .addAttribute("session", session) .build(); - - return (SecurityIdentity) identity; } } - return (SecurityIdentity) QuarkusSecurityIdentity.builder().setAnonymous(true).build(); + return QuarkusSecurityIdentity.builder() + .setAnonymous(true) + .addAttribute("address", context.request().remoteAddress().hostAddress()) + .build(); }).runSubscriptionOn(Infrastructure.getDefaultWorkerPool()); } diff --git a/src/main/java/eu/m724/talkpages/template/AuthExtension.java b/src/main/java/eu/m724/talkpages/template/AuthExtension.java index 40809e8..2bba941 100644 --- a/src/main/java/eu/m724/talkpages/template/AuthExtension.java +++ b/src/main/java/eu/m724/talkpages/template/AuthExtension.java @@ -8,10 +8,20 @@ import io.quarkus.security.identity.SecurityIdentity; @TemplateExtension(namespace = "user") public class AuthExtension { + public static boolean loggedIn() { + SecurityIdentity identity = getIdentity(); + return !identity.isAnonymous(); + } + public static String name() { + SecurityIdentity identity = getIdentity(); + return identity.isAnonymous() ? identity.getAttribute("address") : identity.getPrincipal().getName(); + } + + + private static SecurityIdentity getIdentity() { try (InstanceHandle handle = Arc.container().instance(CurrentIdentityAssociation.class)) { - SecurityIdentity identity = handle.get().getIdentity(); - return identity.isAnonymous() ? null : identity.getPrincipal().getName(); + return handle.get().getIdentity(); } } } diff --git a/src/main/resources/templates/IndexResource/index.html b/src/main/resources/templates/IndexResource/index.html index f1ec0f5..c48a406 100644 --- a/src/main/resources/templates/IndexResource/index.html +++ b/src/main/resources/templates/IndexResource/index.html @@ -5,7 +5,7 @@