minor code improvements

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-08-03 10:58:15 +02:00
parent 93f6c2d603
commit 2752d80222
13 changed files with 42 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ package de.srsoftware.oidc.backend;
import static de.srsoftware.oidc.api.Constants.*;
import static de.srsoftware.oidc.api.Permission.MANAGE_CLIENTS;
import static de.srsoftware.utils.Strings.uuid;
import static java.lang.System.Logger.Level.ERROR;
import static java.net.HttpURLConnection.*;
@@ -14,7 +15,6 @@ import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;
import org.json.JSONObject;
public class ClientController extends Controller {
@@ -54,7 +54,7 @@ public class ClientController extends Controller {
}
}
var state = json.getString(STATE);
var code = UUID.randomUUID().toString();
var code = uuid();
authorizations.addCode(client, session.user(), code);
return sendContent(ex, Map.of(CONFIRMED, true, CODE, code, REDIRECT_URI, redirect, STATE, state));
}

View File

@@ -1,12 +1,12 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.backend;
import static de.srsoftware.utils.Strings.uuid;
import static org.jose4j.jws.AlgorithmIdentifiers.RSA_USING_SHA256;
import de.srsoftware.oidc.api.KeyManager;
import de.srsoftware.oidc.api.KeyStorage;
import java.io.IOException;
import java.util.UUID;
import org.jose4j.jwk.PublicJsonWebKey;
import org.jose4j.jwk.RsaJwkGenerator;
import org.jose4j.lang.JoseException;
@@ -29,7 +29,7 @@ public class RotatingKeyManager implements KeyManager {
try {
var key = RsaJwkGenerator.generateJwk(2048);
key.setAlgorithm(RSA_USING_SHA256);
key.setKeyId(UUID.randomUUID().toString());
key.setKeyId(uuid());
store.store(key);
return key;
} catch (JoseException e) {

View File

@@ -2,7 +2,7 @@
package de.srsoftware.oidc.backend;
import static de.srsoftware.oidc.api.Constants.*;
import static de.srsoftware.utils.Optionals.optional;
import static de.srsoftware.utils.Optionals.nullable;
import static java.lang.System.Logger.Level.*;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
@@ -71,19 +71,7 @@ public class TokenController extends PathHandler {
if (!client.redirectUris().contains(uri)) sendContent(ex, HTTP_BAD_REQUEST, Map.of(ERROR, "unknown redirect uri", REDIRECT_URI, uri));
if (client.secret() != null) {
String clientSecret = optional(ex.getRequestHeaders().get(AUTHORIZATION))
.map(list -> list.get(0))
.filter(s -> s.startsWith("Basic "))
.map(s -> s.substring(6))
.map(s -> Base64.getDecoder().decode(s))
.map(bytes -> new String(bytes, StandardCharsets.UTF_8))
.filter(s -> s.startsWith("%s:".formatted(client.id())))
.map(s -> s.substring(client.id().length() + 1).trim())
.map(s -> {
System.err.println(s);
return s;
})
.orElseGet(() -> map.get(CLIENT_SECRET));
String clientSecret = nullable(ex.getRequestHeaders().get(AUTHORIZATION)).map(list -> list.get(0)).filter(s -> s.startsWith("Basic ")).map(s -> s.substring(6)).map(s -> Base64.getDecoder().decode(s)).map(bytes -> new String(bytes, StandardCharsets.UTF_8)).filter(s -> s.startsWith("%s:".formatted(client.id()))).map(s -> s.substring(client.id().length() + 1).trim()).orElseGet(() -> map.get(CLIENT_SECRET));
if (clientSecret == null) return sendContent(ex, HTTP_BAD_REQUEST, Map.of(ERROR, "client secret missing"));
if (!client.secret().equals(clientSecret)) return sendContent(ex, HTTP_BAD_REQUEST, Map.of(ERROR, "client secret mismatch"));
}

View File

@@ -43,7 +43,7 @@ public class UserController extends Controller {
var optUser = getBearer(ex).flatMap(users::forToken);
if (optUser.isEmpty()) return sendEmptyResponse(HTTP_UNAUTHORIZED, ex);
var user = optUser.get();
var map = Map.of("sub",user.uuid(),"email",user.email());
var map = Map.of("sub", user.uuid(), "email", user.email());
return sendContent(ex, new JSONObject(map));
}