JWT available after restart
This commit is contained in:
@@ -1,36 +1,96 @@
|
||||
package SuMoC.backend.security.utils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
@Component
|
||||
public class RSAKeyProperties {
|
||||
|
||||
private RSAPublicKey publicKey;
|
||||
private RSAPrivateKey privateKey;
|
||||
private final RSAPublicKey publicKey;
|
||||
private final RSAPrivateKey privateKey;
|
||||
|
||||
public RSAKeyProperties(){
|
||||
KeyPair pair = KeyGeneratorUtility.generateRsaKey();
|
||||
this.publicKey = (RSAPublicKey) pair.getPublic();
|
||||
this.privateKey = (RSAPrivateKey) pair.getPrivate();
|
||||
public RSAKeyProperties(
|
||||
@Value("${RSA_PUBLIC_KEY_PATH:/data/keys/jwt-public.pem}") String publicKeyPath,
|
||||
@Value("${RSA_PRIVATE_KEY_PATH:/data/keys/jwt-private.pem}") String privateKeyPath
|
||||
) {
|
||||
try {
|
||||
Path pubPath = Paths.get(publicKeyPath);
|
||||
Path privPath = Paths.get(privateKeyPath);
|
||||
|
||||
if (pubPath.getParent() != null) Files.createDirectories(pubPath.getParent());
|
||||
if (privPath.getParent() != null) Files.createDirectories(privPath.getParent());
|
||||
|
||||
if (Files.exists(pubPath) && Files.exists(privPath)) {
|
||||
this.publicKey = readPublicKey(pubPath);
|
||||
this.privateKey = readPrivateKey(privPath);
|
||||
return;
|
||||
}
|
||||
|
||||
KeyPair pair = KeyGeneratorUtility.generateRsaKey();
|
||||
RSAPublicKey pub = (RSAPublicKey) pair.getPublic();
|
||||
RSAPrivateKey priv = (RSAPrivateKey) pair.getPrivate();
|
||||
|
||||
writePublicKey(pubPath, pub);
|
||||
writePrivateKey(privPath, priv);
|
||||
|
||||
this.publicKey = pub;
|
||||
this.privateKey = priv;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Failed to load or generate RSA keys", e);
|
||||
}
|
||||
}
|
||||
|
||||
public RSAPublicKey getPublicKey() {
|
||||
return publicKey;
|
||||
public RSAPublicKey getPublicKey() { return publicKey; }
|
||||
public RSAPrivateKey getPrivateKey() { return privateKey; }
|
||||
|
||||
// ---------- Read ----------
|
||||
private static RSAPublicKey readPublicKey(Path path) throws Exception {
|
||||
String pem = Files.readString(path, StandardCharsets.UTF_8)
|
||||
.replace("-----BEGIN PUBLIC KEY-----", "")
|
||||
.replace("-----END PUBLIC KEY-----", "")
|
||||
.replaceAll("\\s", "");
|
||||
byte[] der = Base64.getDecoder().decode(pem);
|
||||
return (RSAPublicKey) KeyFactory.getInstance("RSA")
|
||||
.generatePublic(new X509EncodedKeySpec(der));
|
||||
}
|
||||
|
||||
public void setPublicKey(RSAPublicKey publicKey) {
|
||||
this.publicKey = publicKey;
|
||||
private static RSAPrivateKey readPrivateKey(Path path) throws Exception {
|
||||
String pem = Files.readString(path, StandardCharsets.UTF_8)
|
||||
.replace("-----BEGIN PRIVATE KEY-----", "")
|
||||
.replace("-----END PRIVATE KEY-----", "")
|
||||
.replaceAll("\\s", "");
|
||||
byte[] der = Base64.getDecoder().decode(pem);
|
||||
return (RSAPrivateKey) KeyFactory.getInstance("RSA")
|
||||
.generatePrivate(new PKCS8EncodedKeySpec(der));
|
||||
}
|
||||
|
||||
public RSAPrivateKey getPrivateKey() {
|
||||
return privateKey;
|
||||
// ---------- Write ----------
|
||||
private static void writePublicKey(Path path, RSAPublicKey key) throws Exception {
|
||||
writePem(path, "PUBLIC KEY", key.getEncoded());
|
||||
}
|
||||
|
||||
public void setPrivateKey(RSAPrivateKey privateKey) {
|
||||
this.privateKey = privateKey;
|
||||
private static void writePrivateKey(Path path, RSAPrivateKey key) throws Exception {
|
||||
writePem(path, "PRIVATE KEY", key.getEncoded());
|
||||
}
|
||||
|
||||
private static void writePem(Path path, String type, byte[] derBytes) throws Exception {
|
||||
String base64 = Base64.getMimeEncoder(64, new byte[]{'\n'}).encodeToString(derBytes);
|
||||
String pem = "-----BEGIN " + type + "-----\n" +
|
||||
base64 + "\n" +
|
||||
"-----END " + type + "-----\n";
|
||||
Path tmp = Paths.get(path.toString() + ".tmp");
|
||||
Files.writeString(tmp, pem, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||
Files.move(tmp, path, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user