Skip to content

Commit 7ba4402

Browse files
rraystpredic8coderabbitai[bot]t-burch
authored
Fixes #1922: add command to convert private jwk to public jwk (#1945)
* Fixes #1922: add command to convert private jwk to public jwk * Update core/src/main/java/com/predic8/membrane/core/cli/RouterCLI.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: Thomas Bayer <bayer@predic8.de> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: t-burch <119930761+t-burch@users.noreply.github.com>
1 parent 33e368c commit 7ba4402

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

core/src/main/java/com/predic8/membrane/core/cli/MembraneCommandLine.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ private static Options getRootOptions() {
6868
addOption(builder("overwrite").desc("Overwrite the output file, if it exists.").build());
6969
}});
7070

71+
addSubcommand(new CliCommand("private-jwk-to-public", "Convert a private JWK to a public JWK.") {{
72+
addOption(builder("i").longOpt("input").argName("file").hasArg().desc("Input file (JWK, private).").build());
73+
addOption(builder("o").longOpt("output").argName("file").hasArg().desc("Output file (JWK, public).").build());
74+
}});
75+
7176
}};
7277
}
7378

core/src/main/java/com/predic8/membrane/core/cli/RouterCLI.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ public static void main(String[] args) {
8383
System.exit(0);
8484
}
8585

86+
if (commandLine.getCommand().getName().equals("private-jwk-to-public")) {
87+
String input = commandLine.getCommand().getOptionValue("i");
88+
String output = commandLine.getCommand().getOptionValue("o");
89+
if (input == null || output == null) {
90+
log.error("Both input (-i) and output (-o) files must be specified.");
91+
System.exit(1);
92+
}
93+
privateJWKtoPublic(
94+
input,
95+
output);
96+
System.exit(0);
97+
}
98+
8699
try {
87100
getRouter(commandLine).waitFor();
88101
} catch (InterruptedException e) {
@@ -190,6 +203,17 @@ private static void generateJWK(MembraneCommandLine commandLine) {
190203
}
191204
}
192205

206+
private static void privateJWKtoPublic(String input, String output) {
207+
try {
208+
Map map = new ObjectMapper().readValue(new File(input), Map.class);
209+
RsaJsonWebKey rsa = new RsaJsonWebKey(map);
210+
Files.writeString(Paths.get(output), rsa.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY));
211+
} catch (IOException | JoseException e) {
212+
log.error(e.getMessage());
213+
System.exit(1);
214+
}
215+
}
216+
193217
private static @NotNull APIProxy getApiProxy(MembraneCommandLine commandLine) {
194218
APIProxy api = new APIProxy();
195219
api.setPort(commandLine.getCommand().isOptionSet("p") ?

0 commit comments

Comments
 (0)