|
|
@ -3,23 +3,19 @@ package de.srsoftware.oidc.backend; |
|
|
|
|
|
|
|
|
|
|
|
import static de.srsoftware.oidc.api.Permission.MANAGE_CLIENTS; |
|
|
|
import static de.srsoftware.oidc.api.Permission.MANAGE_CLIENTS; |
|
|
|
import static de.srsoftware.oidc.api.User.*; |
|
|
|
import static de.srsoftware.oidc.api.User.*; |
|
|
|
|
|
|
|
import static de.srsoftware.oidc.api.Constants.*; |
|
|
|
import static java.net.HttpURLConnection.*; |
|
|
|
import static java.net.HttpURLConnection.*; |
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.sun.net.httpserver.HttpExchange; |
|
|
|
import com.sun.net.httpserver.HttpExchange; |
|
|
|
import de.srsoftware.cookies.SessionToken; |
|
|
|
import de.srsoftware.cookies.SessionToken; |
|
|
|
import de.srsoftware.oidc.api.*; |
|
|
|
import de.srsoftware.oidc.api.*; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray; |
|
|
|
|
|
|
|
import org.json.JSONObject; |
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
|
|
|
|
public class Backend extends PathHandler { |
|
|
|
public class Backend extends PathHandler { |
|
|
|
private static final String CLIENT_ID = "client_id"; |
|
|
|
|
|
|
|
private static final String REDIRECT_URI = "redirect_uri"; |
|
|
|
|
|
|
|
private final SessionService sessions; |
|
|
|
private final SessionService sessions; |
|
|
|
private final UserService users; |
|
|
|
private final UserService users; |
|
|
|
private final ClientService clients; |
|
|
|
private final ClientService clients; |
|
|
@ -30,20 +26,31 @@ public class Backend extends PathHandler { |
|
|
|
users = userService; |
|
|
|
users = userService; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean addClient(HttpExchange ex, Session session) throws IOException { |
|
|
|
|
|
|
|
var json = json(ex); |
|
|
|
|
|
|
|
var redirects = new HashSet<String>(); |
|
|
|
|
|
|
|
for (Object o : json.getJSONArray(REDIRECT_URI)){ |
|
|
|
|
|
|
|
if (o instanceof String s) redirects.add(s); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var client = new Client(json.getString(CLIENT_ID),json.getString(NAME),json.getString(SECRET),redirects); |
|
|
|
|
|
|
|
clients.add(client); |
|
|
|
|
|
|
|
return sendContent(ex,client); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean authorize(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean authorize(HttpExchange ex, Session session) throws IOException { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
var clientId = json.getString(CLIENT_ID); |
|
|
|
var clientId = json.getString(CLIENT_ID); |
|
|
|
var redirect = json.getString(REDIRECT_URI); |
|
|
|
var redirect = json.getString(REDIRECT_URI); |
|
|
|
System.out.println(json); |
|
|
|
System.out.println(json); |
|
|
|
return sendEmptyResponse(HTTP_NOT_FOUND,ex); |
|
|
|
return sendEmptyResponse(HTTP_NOT_FOUND, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean clients(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean clients(HttpExchange ex, Session session) throws IOException { |
|
|
|
var user = session.user(); |
|
|
|
var user = session.user(); |
|
|
|
if (!user.hasPermission(MANAGE_CLIENTS)) return sendEmptyResponse(HTTP_FORBIDDEN,ex); |
|
|
|
if (!user.hasPermission(MANAGE_CLIENTS)) return sendEmptyResponse(HTTP_FORBIDDEN, ex); |
|
|
|
var json = new JSONObject(); |
|
|
|
var json = new JSONObject(); |
|
|
|
clients.listClients().forEach(client -> json.put(client.id(), Map.of("name",client.name(),"redirect_uris",client.redirectUris()))); |
|
|
|
clients.listClients().forEach(client -> json.put(client.id(), Map.of("name", client.name(), "redirect_uris", client.redirectUris()))); |
|
|
|
return sendContent(ex,json); |
|
|
|
return sendContent(ex, json); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean doLogin(HttpExchange ex) throws IOException { |
|
|
|
private boolean doLogin(HttpExchange ex) throws IOException { |
|
|
@ -72,7 +79,7 @@ public class Backend extends PathHandler { |
|
|
|
var session = optSession.get(); |
|
|
|
var session = optSession.get(); |
|
|
|
switch (path) { |
|
|
|
switch (path) { |
|
|
|
case "/logout": |
|
|
|
case "/logout": |
|
|
|
return logout(ex,session); |
|
|
|
return logout(ex, session); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
System.err.println("not implemented"); |
|
|
|
System.err.println("not implemented"); |
|
|
@ -92,14 +99,16 @@ public class Backend extends PathHandler { |
|
|
|
// post-login paths
|
|
|
|
// post-login paths
|
|
|
|
var session = optSession.get(); |
|
|
|
var session = optSession.get(); |
|
|
|
switch (path) { |
|
|
|
switch (path) { |
|
|
|
|
|
|
|
case "/add/client": |
|
|
|
|
|
|
|
return addClient(ex,session); |
|
|
|
case "/authorize": |
|
|
|
case "/authorize": |
|
|
|
return authorize(ex,session); |
|
|
|
return authorize(ex, session); |
|
|
|
case "/clients": |
|
|
|
case "/clients": |
|
|
|
return clients(ex,session); |
|
|
|
return clients(ex, session); |
|
|
|
case "/update/password": |
|
|
|
case "/update/password": |
|
|
|
return updatePassword(ex,session); |
|
|
|
return updatePassword(ex, session); |
|
|
|
case "/update/user": |
|
|
|
case "/update/user": |
|
|
|
return updateUser(ex,session); |
|
|
|
return updateUser(ex, session); |
|
|
|
case "/user": |
|
|
|
case "/user": |
|
|
|
return sendUserAndCookie(ex, session); |
|
|
|
return sendUserAndCookie(ex, session); |
|
|
|
} |
|
|
|
} |
|
|
@ -114,7 +123,7 @@ public class Backend extends PathHandler { |
|
|
|
private boolean logout(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean logout(HttpExchange ex, Session session) throws IOException { |
|
|
|
sessions.dropSession(session.id()); |
|
|
|
sessions.dropSession(session.id()); |
|
|
|
new SessionToken("").addTo(ex); |
|
|
|
new SessionToken("").addTo(ex); |
|
|
|
return sendEmptyResponse(HTTP_OK,ex); |
|
|
|
return sendEmptyResponse(HTTP_OK, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean openidConfig(HttpExchange ex) throws IOException { |
|
|
|
private boolean openidConfig(HttpExchange ex) throws IOException { |
|
|
@ -128,39 +137,39 @@ public class Backend extends PathHandler { |
|
|
|
|
|
|
|
|
|
|
|
private boolean sendUserAndCookie(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean sendUserAndCookie(HttpExchange ex, Session session) throws IOException { |
|
|
|
new SessionToken(session.id()).addTo(ex); |
|
|
|
new SessionToken(session.id()).addTo(ex); |
|
|
|
return sendContent(ex,new JSONObject(session.user().map(false))); |
|
|
|
return sendContent(ex, new JSONObject(session.user().map(false))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean updatePassword(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean updatePassword(HttpExchange ex, Session session) throws IOException { |
|
|
|
var user =session.user(); |
|
|
|
var user = session.user(); |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
var uuid = json.getString(UUID); |
|
|
|
var uuid = json.getString(UUID); |
|
|
|
if (!uuid.equals(user.uuid())) { |
|
|
|
if (!uuid.equals(user.uuid())) { |
|
|
|
return sendEmptyResponse(HTTP_FORBIDDEN, ex); |
|
|
|
return sendEmptyResponse(HTTP_FORBIDDEN, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
var oldPass = json.getString("oldpass"); |
|
|
|
var oldPass = json.getString("oldpass"); |
|
|
|
if (!users.passwordMatches(oldPass,user.hashedPassword())) return sendError(ex,"wrong password"); |
|
|
|
if (!users.passwordMatches(oldPass, user.hashedPassword())) return sendError(ex, "wrong password"); |
|
|
|
|
|
|
|
|
|
|
|
var newpass = json.getJSONArray("newpass"); |
|
|
|
var newpass = json.getJSONArray("newpass"); |
|
|
|
var newPass1 = newpass.getString(0); |
|
|
|
var newPass1 = newpass.getString(0); |
|
|
|
if (!newPass1.equals(newpass.getString(1))){ |
|
|
|
if (!newPass1.equals(newpass.getString(1))) { |
|
|
|
return sendError(ex,"password mismatch"); |
|
|
|
return sendError(ex, "password mismatch"); |
|
|
|
} |
|
|
|
} |
|
|
|
users.updatePassword(user,newPass1); |
|
|
|
users.updatePassword(user, newPass1); |
|
|
|
return sendContent(ex,new JSONObject(user.map(false))); |
|
|
|
return sendContent(ex, new JSONObject(user.map(false))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean updateUser(HttpExchange ex, Session session) throws IOException { |
|
|
|
private boolean updateUser(HttpExchange ex, Session session) throws IOException { |
|
|
|
var user =session.user(); |
|
|
|
var user = session.user(); |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
var uuid = json.getString(UUID); |
|
|
|
var uuid = json.getString(UUID); |
|
|
|
if (!uuid.equals(user.uuid())){ |
|
|
|
if (!uuid.equals(user.uuid())) { |
|
|
|
return sendEmptyResponse(HTTP_FORBIDDEN,ex); |
|
|
|
return sendEmptyResponse(HTTP_FORBIDDEN, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
user.username(json.getString(USERNAME)); |
|
|
|
user.username(json.getString(USERNAME)); |
|
|
|
user.email(json.getString(EMAIL)); |
|
|
|
user.email(json.getString(EMAIL)); |
|
|
|
users.save(user); |
|
|
|
users.save(user); |
|
|
|
JSONObject response = new JSONObject(user.map(false)); |
|
|
|
JSONObject response = new JSONObject(user.map(false)); |
|
|
|
return sendContent(ex,response); |
|
|
|
return sendContent(ex, response); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|