From 5f6a83aa735040b942fbd4e352731048237a7868 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Sun, 29 Oct 2023 21:26:42 +0000 Subject: [PATCH] Add code from the meeting. --- .../server/handlers/auth/UserHandlers.java | 12 +++++++++++- .../piped/utils/obj/db/OidcUserData.java | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/kavin/piped/utils/obj/db/OidcUserData.java diff --git a/src/main/java/me/kavin/piped/server/handlers/auth/UserHandlers.java b/src/main/java/me/kavin/piped/server/handlers/auth/UserHandlers.java index 40df4df..7fb56ca 100644 --- a/src/main/java/me/kavin/piped/server/handlers/auth/UserHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/auth/UserHandlers.java @@ -19,6 +19,7 @@ import me.kavin.piped.utils.ExceptionHandler; import me.kavin.piped.utils.RequestUtils; import me.kavin.piped.utils.obj.OidcData; import me.kavin.piped.utils.obj.OidcProvider; +import me.kavin.piped.utils.obj.db.OidcUserData; import me.kavin.piped.utils.obj.db.User; import me.kavin.piped.utils.resp.*; import org.apache.commons.codec.digest.DigestUtils; @@ -297,6 +298,14 @@ public class UserHandlers { String hash = user.getPassword(); if (hash.isEmpty()) { + + CriteriaBuilder cb = s.getCriteriaBuilder(); + CriteriaQuery cr = cb.createQuery(OidcUserData.class); + Root root = cr.from(OidcUserData.class); + cr.select(root).where(cb.equal(root.get("user"), user.getId())); + + OidcUserData oidcUserData = s.createQuery(cr).uniqueResult(); + //TODO: Get user from oidc table and lookup provider OidcProvider provider = Constants.OIDC_PROVIDERS.get(0); URI callback = URI.create(String.format("%s/oidc/%s/delete", Constants.PUBLIC_URL, provider.name)); @@ -310,7 +319,8 @@ public class UserHandlers { .state(new State(state)).nonce(data.nonce).maxAge(0).build(); - return String.format("{\"redirect\": \"%s\"}", oidcRequest.toURI().toString()).getBytes(); + return mapper.writeValueAsBytes(mapper.createObjectNode() + .put("redirect", oidcRequest.toURI().toString())); } if (!hashMatch(hash, pass)) ExceptionHandler.throwErrorResponse(new IncorrectCredentialsResponse()); diff --git a/src/main/java/me/kavin/piped/utils/obj/db/OidcUserData.java b/src/main/java/me/kavin/piped/utils/obj/db/OidcUserData.java new file mode 100644 index 0000000..e23d621 --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/obj/db/OidcUserData.java @@ -0,0 +1,19 @@ +package me.kavin.piped.utils.obj.db; + +import jakarta.persistence.*; + +@Entity +@Table(name = "oidc_user_data") +public class OidcUserData { + + @Column(unique = true) + @Id + private String sub; + + @OneToOne + private User user; + + private String provider; + + +}