implemented custom loggin

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-07-24 00:16:55 +02:00
parent a277be5091
commit fe14e81304
21 changed files with 335 additions and 66 deletions

View File

@@ -0,0 +1,13 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.api;
import java.util.Date;
import java.util.List;
public interface AuthorizationService {
AuthorizationService authorize(Client client, User user, Date expiration);
boolean isAuthorized(Client client, User user);
List<User> authorizedUsers(Client client);
List<Client> authorizedClients(User user);
AuthorizationService revoke(Client client, User user);
}

View File

@@ -2,18 +2,20 @@
package de.srsoftware.oidc.api;
import static de.srsoftware.oidc.api.Constants.*;
import static java.lang.System.Logger.Level.WARNING;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public record Client(String id, String name, String secret, Set<String> redirectUris) {
private static System.Logger LOG = System.getLogger(Client.class.getSimpleName());
public Map<String, Object> map() {
return Map.of(CLIENT_ID, id, NAME, name, SECRET, secret, REDIRECT_URIS, redirectUris);
}
public String generateCode() {
System.err.printf("%s.generateCode() not implemented!", getClass().getSimpleName());
LOG.log(WARNING,"{0}.generateCode() not implemented!", getClass().getSimpleName());
return UUID.randomUUID().toString();
}
}

View File

@@ -2,9 +2,10 @@
package de.srsoftware.oidc.api;
public class Constants {
public
static final String CODE = "code";
public static final String CAUSE = "cause";
public static final String CLIENT_ID = "client_id";
public static final String CODE = "code";
public static final String CONFIRMED = "confirmed";
public static final String NAME = "name";
public static final String REDIRECT_URI = "redirect_uri";
public static final String REDIRECT_URIS = "redirect_uris";

View File

@@ -1,6 +1,8 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.api;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.INFO;
import static java.net.HttpURLConnection.*;
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -16,6 +18,7 @@ import java.util.stream.Stream;
import org.json.JSONObject;
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 DELETE = "DELETE";
public static final String GET = "GET";
@@ -54,7 +57,7 @@ public abstract class PathHandler implements HttpHandler {
public void handle(HttpExchange ex) throws IOException {
String path = relativePath(ex);
String method = ex.getRequestMethod();
System.out.printf("%s %s\n", method, path);
LOG.log(INFO, "{0} {1}", method, path);
boolean ignored = switch (method) {
case DELETE -> doDelete(path,ex);
case GET -> doGet(path,ex);
@@ -109,32 +112,38 @@ public abstract class PathHandler implements HttpHandler {
return false;
}
public static boolean sendRedirect(HttpExchange ex, String url) throws IOException {
ex.getResponseHeaders().add("Location", url);
return sendEmptyResponse(HTTP_MOVED_TEMP, ex);
}
public static boolean sendContent(HttpExchange ex, int status, byte[] bytes) throws IOException {
ex.sendResponseHeaders(status, bytes.length);
ex.getResponseBody().write(bytes);
return true;
}
public static boolean sendContent(HttpExchange ex, int status, Object o) throws IOException {
if (o instanceof Map map) o = new JSONObject(map);
if (o instanceof JSONObject) ex.getResponseHeaders().add(CONTENT_TYPE, JSON);
return sendContent(ex, status, o.toString().getBytes(UTF_8));
}
public static boolean sendContent(HttpExchange ex, byte[] bytes) throws IOException {
return sendContent(ex, HTTP_OK, bytes);
}
public static boolean sendContent(HttpExchange ex, Object o) throws IOException {
if (o instanceof Map map) o = new JSONObject(map);
if (o instanceof JSONObject) ex.getResponseHeaders().add(CONTENT_TYPE, JSON);
return sendContent(ex, HTTP_OK, o.toString().getBytes(UTF_8));
return sendContent(ex, HTTP_OK, o);
}
public static boolean sendError(HttpExchange ex, byte[] bytes) throws IOException {
public static boolean badRequest(HttpExchange ex, byte[] bytes) throws IOException {
return sendContent(ex, HTTP_BAD_REQUEST, bytes);
}
public static boolean sendError(HttpExchange ex, Object o) throws IOException {
return sendContent(ex, HTTP_BAD_REQUEST, o.toString().getBytes(UTF_8));
}
public static boolean sendRedirect(HttpExchange ex, String url) throws IOException {
ex.getResponseHeaders().add("Location", url);
return sendEmptyResponse(HTTP_MOVED_TEMP, ex);
public static boolean badRequest(HttpExchange ex, Object o) throws IOException {
return sendContent(ex, HTTP_BAD_REQUEST, o);
}
}