test login

This commit is contained in:
2024-01-23 22:56:17 +01:00
parent ca7790bfdf
commit 9066a51025
8 changed files with 144 additions and 108 deletions

View File

@@ -1,16 +1,90 @@
package fr.titionfire;
import io.quarkus.oidc.IdToken;
import io.quarkus.oidc.RefreshToken;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.jboss.resteasy.reactive.NoCache;
@Path("/hello")
public class ExampleResource {
/*@Inject
@IdToken
JsonWebToken idToken;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello from RESTEasy Reactive";
return "Hello, " + idToken.getClaim("name");
}*/
/**
* Injection point for the ID Token issued by the OpenID Connect Provider
*/
@Inject
@IdToken
JsonWebToken idToken;
/**
* Injection point for the Access Token issued by the OpenID Connect Provider
*/
@Inject
JsonWebToken accessToken;
/**
* Injection point for the Refresh Token issued by the OpenID Connect Provider
*/
@Inject
RefreshToken refreshToken;
@Inject
SecurityIdentity securityIdentity;
/**
* Returns the tokens available to the application. This endpoint exists only for demonstration purposes, you should not
* expose these tokens in a real application.
*
* @return a HTML page containing the tokens available to the application
*/
@GET
@Produces("text/html")
@NoCache
public String getTokens() {
StringBuilder response = new StringBuilder().append("<html>")
.append("<body>")
.append("<ul>");
Object userName = this.idToken.getClaim("preferred_username");
if (userName != null) {
response.append("<li>username: ").append(userName.toString()).append("</li>");
}
if (userName != null) {
response.append("<li>username: ").append(this.idToken.toString()).append("</li>");
}
Object scopes = this.accessToken.getClaim("scope");
if (scopes != null) {
response.append("<li>scopes: ").append(scopes.toString()).append("</li>");
}
if (scopes != null) {
response.append("<li>scopes: ").append(this.accessToken.toString()).append("</li>");
}
if (scopes != null) {
response.append("<li>getRoles: ").append(this.securityIdentity.getRoles()).append("</li>");
}
response.append("<li>refresh_token: ").append(refreshToken.getToken() != null).append("</li>");
return response.append("</ul>").append("</body>").append("</html>").toString();
}
}

View File

@@ -0,0 +1,45 @@
package fr.titionfire.ffsaf.rest;
import io.quarkus.security.Authenticated;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import java.net.URI;
import java.net.URISyntaxException;
@Path("/auth")
public class AuthEndpoints {
@ConfigProperty(name = "login_redirect")
String redirect;
@Inject
SecurityIdentity securityIdentity;
@GET
@Produces(MediaType.TEXT_PLAIN)
public Boolean auth() {
return !securityIdentity.isAnonymous();
}
@GET
@Path("/userinfo")
@Produces(MediaType.TEXT_PLAIN)
public String userinfo() {
return securityIdentity.getPrincipal().getName();
}
@GET
@Path("/login")
@Authenticated
@Produces(MediaType.TEXT_PLAIN)
public Response login() throws URISyntaxException {
return Response.temporaryRedirect(new URI(redirect)).build();
}
}

View File

@@ -87,6 +87,7 @@ public class CombEndpoints {
@GET
@Path("{id}/photo")
@RolesAllowed("federation_admin")
public Uni<Response> getPhoto(@PathParam("id") long id) throws URISyntaxException {
Future<Pair<File, byte[]>> future = CompletableFuture.supplyAsync(() -> {
FilenameFilter filter = (directory, filename) -> filename.startsWith(String.valueOf(id));