Save redirect in state

This commit is contained in:
Jeidnx 2023-06-19 16:06:26 +02:00
parent 375ee585c1
commit 143711cb2a
No known key found for this signature in database
GPG Key ID: 0E9E697B7E99DF39
2 changed files with 7 additions and 10 deletions

View File

@ -42,11 +42,9 @@ import com.nimbusds.oauth2.sdk.id.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.util.LinkedList;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress; import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
import static io.activej.http.HttpHeaders.*; import static io.activej.http.HttpHeaders.*;
@ -310,8 +308,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
})).map(GET, "/oidc/:provider/:function", AsyncServlet.ofBlocking(executor, request -> { })).map(GET, "/oidc/:provider/:function", AsyncServlet.ofBlocking(executor, request -> {
try { try {
String function = request.getPathParameter("function"); String function = request.getPathParameter("function");
OidcProvider provider = getOidcProvider(request.getPathParameter("provider"));
OidcProvider provider = findOidcProvider(request.getPathParameter("provider"), Constants.OIDC_PROVIDERS);
if(provider == null) if(provider == null)
return HttpResponse.ofCode(500).withHtml("Can't find the provider on the server."); return HttpResponse.ofCode(500).withHtml("Can't find the provider on the server.");
@ -319,8 +316,9 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
switch (function) { switch (function) {
case "login" -> { case "login" -> {
String redirectUri = request.getQueryParameter("redirect");
State state = new State(); State state = new State(new Identifier(24) + "." + redirectUri);
Nonce nonce = new Nonce(); Nonce nonce = new Nonce();
AuthenticationRequest oidcRequest = new AuthenticationRequest.Builder( AuthenticationRequest oidcRequest = new AuthenticationRequest.Builder(
@ -378,7 +376,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
String sessionId = UserHandlers.oidcCallbackResponse(provider.name, userInfo.getSubject().toString()); String sessionId = UserHandlers.oidcCallbackResponse(provider.name, userInfo.getSubject().toString());
return HttpResponse.redirect302(Constants.FRONTEND_URL + "/login?session=" + sessionId); return HttpResponse.redirect302(sr.getState().toString().split("\\.", 2)[1] + "?session=" + sessionId);
} }
default -> { default -> {
return HttpResponse.ofCode(500).withHtml("Invalid function `" + function + "`."); return HttpResponse.ofCode(500).withHtml("Invalid function `" + function + "`.");
@ -635,9 +633,9 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
return new CustomServletDecorator(router); return new CustomServletDecorator(router);
} }
private static OidcProvider findOidcProvider(String provider, LinkedList<OidcProvider> list){ private static OidcProvider getOidcProvider(String provider){
for(int i = 0; i < list.size(); i++) { for(int i = 0; i < Constants.OIDC_PROVIDERS.size(); i++) {
OidcProvider curr = list.get(i); OidcProvider curr = Constants.OIDC_PROVIDERS.get(i);
if(curr == null || !curr.name.equals(provider)) continue; if(curr == null || !curr.name.equals(provider)) continue;
return curr; return curr;
} }

View File

@ -110,7 +110,6 @@ public class UserHandlers {
public static String oidcCallbackResponse(String provider, String uid) { public static String oidcCallbackResponse(String provider, String uid) {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
String dbName = provider + "-" + uid; String dbName = provider + "-" + uid;
System.out.println(dbName); //TODO:
CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<User> cr = cb.createQuery(User.class); CriteriaQuery<User> cr = cb.createQuery(User.class);
Root<User> root = cr.from(User.class); Root<User> root = cr.from(User.class);