Browse Source

working on token endpoint

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
sqlite
Stephan Richter 4 months ago
parent
commit
09e5a52c53
  1. 17
      de.srsoftware.logging/src/main/java/de/srsoftware/logging/ColorLogger.java
  2. 15
      de.srsoftware.logging/src/main/java/de/srsoftware/logging/ConsoleColors.java
  3. 2
      de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/Client.java
  4. 2
      de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/Constants.java
  5. 10
      de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/PathHandler.java
  6. 2
      de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java
  7. 17
      de.srsoftware.oidc.backend/src/main/java/de/srsoftware/oidc/backend/Backend.java
  8. 6
      de.srsoftware.oidc.web/src/main/java/de/srsoftware/oidc/web/Forward.java
  9. 4
      de.srsoftware.oidc.web/src/main/java/de/srsoftware/oidc/web/StaticPages.java
  10. 12
      de.srsoftware.oidc.web/src/main/resources/en/clients.html
  11. 1
      de.srsoftware.oidc.web/src/main/resources/en/login.html

17
de.srsoftware.logging/src/main/java/de/srsoftware/logging/ColorLogger.java

@ -1,15 +1,14 @@
/* © SRSoftware 2024 */ /* © SRSoftware 2024 */
package de.srsoftware.logging; package de.srsoftware.logging;
import static de.srsoftware.logging.ConsoleColors.*;
import static java.lang.System.Logger.Level.*;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import static de.srsoftware.logging.ConsoleColors.*;
import static java.lang.System.Logger.Level.*;
public class ColorLogger implements System.Logger { public class ColorLogger implements System.Logger {
private final String name; private final String name;
private static int rootLevel = INFO.getSeverity(); private static int rootLevel = INFO.getSeverity();
@ -31,7 +30,7 @@ public class ColorLogger implements System.Logger {
@Override @Override
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) { public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
if (isLoggable(level)) { if (isLoggable(level)) {
System.out.println(colorize(msg,level.getSeverity())); System.out.println(colorize(msg, level.getSeverity()));
thrown.printStackTrace(); thrown.printStackTrace();
} }
} }
@ -39,19 +38,19 @@ public class ColorLogger implements System.Logger {
@Override @Override
public void log(Level level, ResourceBundle bundle, String format, Object... params) { public void log(Level level, ResourceBundle bundle, String format, Object... params) {
if (isLoggable(level)) { if (isLoggable(level)) {
System.out.println(colorize(MessageFormat.format(format, params),level.getSeverity())); System.out.println(colorize(MessageFormat.format(format, params), level.getSeverity()));
} }
} }
public ColorLogger setLogLevel(Level level){ public ColorLogger setLogLevel(Level level) {
rootLevel = level.getSeverity(); rootLevel = level.getSeverity();
return this; return this;
} }
private static String colorize(String message,int severity){ private static String colorize(String message, int severity) {
var color = severity >= ERROR.getSeverity() ? RED : severity >= WARNING.getSeverity() ? YELLOW : severity >= INFO.getSeverity() ? WHITE_BRIGHT : WHITE; var color = severity >= ERROR.getSeverity() ? RED : severity >= WARNING.getSeverity() ? YELLOW : severity >= INFO.getSeverity() ? WHITE_BRIGHT : WHITE;
var date = new Date(); var date = new Date();
var FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); var FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
return WHITE+FORMAT.format(date)+" "+color+message+RESET; return WHITE + FORMAT.format(date) + " " + color + message + RESET;
} }
} }

15
de.srsoftware.logging/src/main/java/de/srsoftware/logging/ConsoleColors.java

@ -1,3 +1,4 @@
/* © SRSoftware 2024 */
package de.srsoftware.logging; package de.srsoftware.logging;
public class ConsoleColors { public class ConsoleColors {
@ -58,18 +59,18 @@ public class ConsoleColors {
public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m"; // YELLOW
public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m"; // PURPLE
public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE
// High Intensity backgrounds // High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m"; // BLACK
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m"; // RED
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m"; // GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m"; // YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m"; // BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE

2
de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/Client.java

@ -15,7 +15,7 @@ public record Client(String id, String name, String secret, Set<String> redirect
} }
public String generateCode() { public String generateCode() {
LOG.log(WARNING,"{0}.generateCode() not implemented!", getClass().getSimpleName()); LOG.log(WARNING, "{0}.generateCode() not implemented!", getClass().getSimpleName());
return UUID.randomUUID().toString(); return UUID.randomUUID().toString();
} }
} }

2
de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/Constants.java

@ -6,9 +6,11 @@ public class Constants {
public static final String CLIENT_ID = "client_id"; public static final String CLIENT_ID = "client_id";
public static final String CODE = "code"; public static final String CODE = "code";
public static final String CONFIRMED = "confirmed"; public static final String CONFIRMED = "confirmed";
public static final String GRANT_TYPE = "grant_type";
public static final String NAME = "name"; public static final String NAME = "name";
public static final String REDIRECT_URI = "redirect_uri"; public static final String REDIRECT_URI = "redirect_uri";
public static final String REDIRECT_URIS = "redirect_uris"; public static final String REDIRECT_URIS = "redirect_uris";
public static final String SECRET = "secret"; public static final String SECRET = "secret";
public static final String STATE = "state"; public static final String STATE = "state";
public static final String ATUH_CODE = "authorization_code";
} }

10
de.srsoftware.oidc.api/src/main/java/de/srsoftware/oidc/api/PathHandler.java

@ -18,11 +18,13 @@ import java.util.stream.Stream;
import org.json.JSONObject; import org.json.JSONObject;
public abstract class PathHandler implements HttpHandler { public abstract class PathHandler implements HttpHandler {
public System.Logger LOG = System.getLogger(getClass().getSimpleName());
public static final String CONTENT_TYPE = "Content-Type"; public static final String CONTENT_TYPE = "Content-Type";
public static final String DELETE = "DELETE"; public static final String DELETE = "DELETE";
private static final String FORWARDED_HOST = "x-forwarded-host";
public static final String GET = "GET"; public static final String GET = "GET";
public static final String HOST = "host";
public static final String JSON = "application/json"; public static final String JSON = "application/json";
public static System.Logger LOG = System.getLogger(PathHandler.class.getSimpleName());
public static final String POST = "POST"; public static final String POST = "POST";
private String[] paths; private String[] paths;
@ -96,7 +98,10 @@ public abstract class PathHandler implements HttpHandler {
} }
public static String hostname(HttpExchange ex) { public static String hostname(HttpExchange ex) {
return "http://%s".formatted(ex.getRequestHeaders().getFirst("Host")); var headers = ex.getRequestHeaders();
var host = headers.getFirst(FORWARDED_HOST);
if (host == null) host = headers.getFirst(HOST);
return host == null ? null : "https://"+host;
} }
public static JSONObject json(HttpExchange ex) throws IOException { public static JSONObject json(HttpExchange ex) throws IOException {
@ -118,6 +123,7 @@ public abstract class PathHandler implements HttpHandler {
} }
public static boolean sendContent(HttpExchange ex, int status, byte[] bytes) throws IOException { public static boolean sendContent(HttpExchange ex, int status, byte[] bytes) throws IOException {
LOG.log(DEBUG, "sending {0} response…", status);
ex.sendResponseHeaders(status, bytes.length); ex.sendResponseHeaders(status, bytes.length);
ex.getResponseBody().write(bytes); ex.getResponseBody().write(bytes);
return true; return true;

2
de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java

@ -60,7 +60,7 @@ public class Application {
map.put(BASE_PATH, Path.of(tokens.remove(0))); map.put(BASE_PATH, Path.of(tokens.remove(0)));
break; break;
default: default:
LOG.log(ERROR,"Unknown option: {0}", token); LOG.log(ERROR, "Unknown option: {0}", token);
} }
} }

17
de.srsoftware.oidc.backend/src/main/java/de/srsoftware/oidc/backend/Backend.java

@ -12,9 +12,11 @@ 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.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import org.json.JSONObject; import org.json.JSONObject;
public class Backend extends PathHandler { public class Backend extends PathHandler {
@ -126,7 +128,7 @@ public class Backend extends PathHandler {
return logout(ex, session); return logout(ex, session);
} }
LOG.log(WARNING,"not implemented"); LOG.log(WARNING, "not implemented");
return sendEmptyResponse(HTTP_NOT_FOUND, ex); return sendEmptyResponse(HTTP_NOT_FOUND, ex);
} }
@ -160,7 +162,7 @@ public class Backend extends PathHandler {
case "/user": case "/user":
return sendUserAndCookie(ex, session); return sendUserAndCookie(ex, session);
} }
LOG.log(WARNING,"not implemented"); LOG.log(WARNING, "not implemented");
return sendEmptyResponse(HTTP_NOT_FOUND, ex); return sendEmptyResponse(HTTP_NOT_FOUND, ex);
} }
@ -186,11 +188,18 @@ public class Backend extends PathHandler {
} }
private boolean provideToken(HttpExchange ex) throws IOException { private boolean provideToken(HttpExchange ex) throws IOException {
LOG.log(ERROR,"{0}.provideToken(ex) not implemented!\n", getClass().getSimpleName()); var map = deserialize(body(ex));
LOG.log(WARNING,json(ex)); LOG.log(WARNING, "map: {0}", map);
LOG.log(ERROR, "{0}.provideToken(ex) not implemented!", getClass().getSimpleName());
var grantType = map.get(GRANT_TYPE);
if (!ATUH_CODE.equals(grantType)) sendContent(ex, HTTP_BAD_REQUEST, Map.of(ERROR, "unknown grant type", GRANT_TYPE, grantType));
return sendEmptyResponse(HTTP_NOT_FOUND, ex); return sendEmptyResponse(HTTP_NOT_FOUND, ex);
} }
private Map<String, String> deserialize(String body) {
return Arrays.stream(body.split("&")).map(s -> s.split("=")).collect(Collectors.toMap(arr -> arr[0], arr -> arr[1]));
}
private boolean openidConfig(HttpExchange ex) throws IOException { private boolean openidConfig(HttpExchange ex) throws IOException {
var host = hostname(ex); var host = hostname(ex);
return sendContent(ex, Map.of("token_endpoint", host + "/api/token", "authorization_endpoint", host + "/web/authorization.html", "userinfo_endpoint", host + "/api/userinfo", "jwks_uri", host + "/api/jwks")); return sendContent(ex, Map.of("token_endpoint", host + "/api/token", "authorization_endpoint", host + "/web/authorization.html", "userinfo_endpoint", host + "/api/userinfo", "jwks_uri", host + "/api/jwks"));

6
de.srsoftware.oidc.web/src/main/java/de/srsoftware/oidc/web/Forward.java

@ -1,12 +1,12 @@
/* © SRSoftware 2024 */ /* © SRSoftware 2024 */
package de.srsoftware.oidc.web; package de.srsoftware.oidc.web;
import static java.lang.System.Logger.Level.INFO;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.oidc.api.PathHandler; import de.srsoftware.oidc.api.PathHandler;
import java.io.IOException; import java.io.IOException;
import static java.lang.System.Logger.Level.INFO;
public class Forward extends PathHandler { public class Forward extends PathHandler {
private final int CODE = 302; private final int CODE = 302;
private final String toPath; private final String toPath;
@ -17,7 +17,7 @@ public class Forward extends PathHandler {
@Override @Override
public boolean doGet(String path, HttpExchange ex) throws IOException { public boolean doGet(String path, HttpExchange ex) throws IOException {
LOG.log(INFO,"Forwarding ({0}}) {1} to {2}…", CODE, path, toPath); LOG.log(INFO, "Forwarding ({0}}) {1} to {2}…", CODE, path, toPath);
return sendRedirect(ex, toPath); return sendRedirect(ex, toPath);
} }
} }

4
de.srsoftware.oidc.web/src/main/java/de/srsoftware/oidc/web/StaticPages.java

@ -39,10 +39,10 @@ public class StaticPages extends PathHandler {
try { try {
Response response = loadFile(lang, relativePath).orElseThrow(() -> new FileNotFoundException()); Response response = loadFile(lang, relativePath).orElseThrow(() -> new FileNotFoundException());
ex.getResponseHeaders().add(CONTENT_TYPE, response.contentType); ex.getResponseHeaders().add(CONTENT_TYPE, response.contentType);
LOG.log(DEBUG,"Loaded {0} for language {1}…success.", relativePath, lang); LOG.log(DEBUG, "Loaded {0} for language {1}…success.", relativePath, lang);
return sendContent(ex, response.content); return sendContent(ex, response.content);
} catch (FileNotFoundException fnf) { } catch (FileNotFoundException fnf) {
LOG.log(WARNING,"Loaded {0} for language {1}…failed.", relativePath, lang); LOG.log(WARNING, "Loaded {0} for language {1}…failed.", relativePath, lang);
return sendEmptyResponse(HTTP_NOT_FOUND, ex); return sendEmptyResponse(HTTP_NOT_FOUND, ex);
} }
} }

12
de.srsoftware.oidc.web/src/main/resources/en/clients.html

@ -8,11 +8,13 @@
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="style.css" />
</head> </head>
<body> <body>
<nav> <nav>
<a id="clients" href="clients.html">Clients</a> <a id="clients" href="clients.html">Clients</a>
</nav> </nav>
<h1>Clients</h1> <h1>Clients</h1>
<fieldset> <a href="https://umbrella.srsoftware.de/user/login">Umbrella</a>
<fieldset>
<legend>These are clients that are registered with LightOIDC:</legend> <legend>These are clients that are registered with LightOIDC:</legend>
<table> <table>
<tr> <tr>
@ -31,6 +33,6 @@
</tr> </tr>
</table> </table>
<span class="hidden" id="message">Really remove client "{}"?</span> <span class="hidden" id="message">Really remove client "{}"?</span>
</fieldset> </fieldset>
</body> </body>
</html> </html>

1
de.srsoftware.oidc.web/src/main/resources/en/login.html

@ -29,6 +29,5 @@
</tr> </tr>
</table> </table>
</fieldset> </fieldset>
<a href="https://umbrella.srsoftware.de/user/login">Umbrella</a>
</body> </body>
</html> </html>
Loading…
Cancel
Save