display address on editing

and minor related tweaks
This commit is contained in:
Minecon724 2024-09-14 11:55:53 +02:00
parent 9ef47b2833
commit d27b50afa0
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
3 changed files with 21 additions and 8 deletions

View file

@ -17,6 +17,8 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative; import jakarta.enterprise.inject.Alternative;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import java.util.function.Supplier;
@Alternative @Alternative
@Priority(1) @Priority(1)
@ApplicationScoped @ApplicationScoped
@ -26,7 +28,7 @@ public class MyHttpAuthenticationMechanism implements HttpAuthenticationMechanis
@Override @Override
public Uni<SecurityIdentity> authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) { // TODO handle ip accounts public Uni<SecurityIdentity> authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) { // TODO handle ip accounts
return Uni.createFrom().item(() -> { return Uni.createFrom().item((Supplier<SecurityIdentity>) () -> {
Cookie cookie = context.request().getCookie("session-token"); Cookie cookie = context.request().getCookie("session-token");
if (cookie != null) { if (cookie != null) {
@ -36,17 +38,18 @@ public class MyHttpAuthenticationMechanism implements HttpAuthenticationMechanis
if (session != null) { if (session != null) {
Account account = session.getAccount(); Account account = session.getAccount();
QuarkusSecurityIdentity identity = QuarkusSecurityIdentity.builder() return QuarkusSecurityIdentity.builder()
.setPrincipal(new QuarkusPrincipal(account.getName())) .setPrincipal(new QuarkusPrincipal(account.getName()))
.addRoles(account.getRoles()) .addRoles(account.getRoles())
.addAttribute("session", session) .addAttribute("session", session)
.build(); .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()); }).runSubscriptionOn(Infrastructure.getDefaultWorkerPool());
} }

View file

@ -8,10 +8,20 @@ import io.quarkus.security.identity.SecurityIdentity;
@TemplateExtension(namespace = "user") @TemplateExtension(namespace = "user")
public class AuthExtension { public class AuthExtension {
public static boolean loggedIn() {
SecurityIdentity identity = getIdentity();
return !identity.isAnonymous();
}
public static String name() { public static String name() {
SecurityIdentity identity = getIdentity();
return identity.isAnonymous() ? identity.getAttribute("address") : identity.getPrincipal().getName();
}
private static SecurityIdentity getIdentity() {
try (InstanceHandle<CurrentIdentityAssociation> handle = Arc.container().instance(CurrentIdentityAssociation.class)) { try (InstanceHandle<CurrentIdentityAssociation> handle = Arc.container().instance(CurrentIdentityAssociation.class)) {
SecurityIdentity identity = handle.get().getIdentity(); return handle.get().getIdentity();
return identity.isAnonymous() ? null : identity.getPrincipal().getName();
} }
} }
} }

View file

@ -5,7 +5,7 @@
<ul> <ul>
<li><a href="/edit">Create a page</a></li> <li><a href="/edit">Create a page</a></li>
{#if user:name == null} {#if !user:loggedIn}
<li><a href="/auth">Login or register</a></li> <li><a href="/auth">Login or register</a></li>
{#else} {#else}
<li><a href="/auth/logout">Logout ({user:name})</a></li> <li><a href="/auth/logout">Logout ({user:name})</a></li>